Haskell 如何将一个游戏分开';从它的更新逻辑中得到什么输入?
我正在使用Haskell编写一个简单的游戏/游戏引擎 以下是我的游戏循环的一个迭代:Haskell 如何将一个游戏分开';从它的更新逻辑中得到什么输入?,haskell,input,game-engine,frp,Haskell,Input,Game Engine,Frp,我正在使用Haskell编写一个简单的游戏/游戏引擎 以下是我的游戏循环的一个迭代: input <- getInput -- where input is some list of pressed keys let world' = update world input -- ... continue with the next iteration update函数(在World.hs文件中)如下所示: getInput = do w <- getKey $ CharKe
input <- getInput -- where input is some list of pressed keys
let world' = update world input
-- ... continue with the next iteration
update
函数(在World.hs
文件中)如下所示:
getInput = do
w <- getKey $ CharKey 'W'
q <- getKey $ CharKey 'Q'
-- ...
return [("W", w), ...]
update world input = world'
where world' = -- if w, then do something, if q then do something, etc.
如您所见,我的输入逻辑分为实际的input.hs
文件和我的World.hs
文件。更新功能实际上不需要测试按键(在我看来)
如何从更新逻辑中分离输入
我想我正在寻找类似回调的东西,可以随意注册和取消注册。将
moveForward
注册到W
键将是理想的选择
我也研究过netwire,但我很难将头脑集中在函数式反应式编程上。如果这是FRP的情况,我也会很感激使用它的任何示例。您可以将一个动作声明为改变世界的函数:
type Action = Word -> World
然后为独立于要绑定的键的对象创建操作:
moveForward :: Action
moveForward world = ...
然后,您可以将(键、动作)
对的列表传递给doInput
函数:
doInput :: [(CharKey, Action)] -> World -> IO World
doInput bindings world = foldM checkKey world bindings where
checkKey world (key, action) = do
pressed <- getKey key
return $ if pressed then action world else world
doInput::[(CharKey,Action)]->世界->IO世界
doInput bindings world=foldM checkKey world绑定,其中
checkKey世界(键,动作)=do
按下
mainloop world = do
doRender world
world' <- doInput [(CharKey 'W', moveForward)] world
mainloop world'