的問題是,我不知道如何創建類型Behavior t GameState
功能香蕉旅行者 - 組建行爲牛逼的遊戲狀態
的行爲我有更多的代碼,但我想只顯示我認爲時,只需要談關於這個問題。讓我知道,如果有空格中填寫以下是我有:
data GameState = GameState {agent :: Agent
,universe :: Universe
}
type Universe = Gr Planet()
data Command = Move PlanetName
| Look
| Quit
deriving Show
data PlayerCommand = PlayerCommand Command PID
| Null
deriving Show
updateGS :: PlayerCommand -> GameState -> GameState
updateGS (PlayerCommand (Move planet) pid) gs =
let agent = getAgent pid gs
nodes = labNodes $ universe gs
current = location agent
Just fromP = lookup (fromEnum current) nodes
Just toP = lookup (fromEnum planet) nodes
fromNode = fromEnum current
toNode = fromEnum planet
uPlayer = Player pid (getPlanetName toP) (Location planet)
mData = MoveData uPlayer (toNode,toP) (fromNode,fromP) nodes
uPlanets = updateLNodeList mData
in GameState uPlayer (mkGraph uPlanets $ labUEdges gates
initialGS :: GameState
initialGS = GameState initPlayer (makeUniverse makePlanetNodes)
和事件網絡
makeNetworkDescription :: AddHandler PlayerCommand -> IO EventNetwork
makeNetworkDescription addCommandEvent = compile $ do
eInput <- fromAddHandler addCommandEvent
let bCommand = stepper Null eInput
eCommandChanged <- changes bCommand
let bGameState :: Behavior t GameState
bGameState = stepper initialGS
reactimate $ (\n -> appendFile "output.txt" ("Command is " ++ show n)) <$> eCommandChanged
我相信bGameState需要使用eCommandChange,但我遇到一個問題類型
stepper :: a -> Event t a -> Behavior t a
這使我相信,我需要改造eInput :: Event t PlayerCommand
成 eGameState :: Event t GameState
,我可以使用stepper
使Behavior t GameState
所以,我的問題是,我的思路是否正確?如果不是,我可以重新定向嗎?如果是這樣,eGameState :: Event t GameState
會是什麼樣子?
針對下面的迴應。當我最初考慮accumB
時,我看到了製作中的類型錯誤。當我嘗試你的建議時發生了什麼。
let bGameState :: Behavior t GameState
bGameState = accumB initialGS $ updateGS <$ eInput
產生錯誤
Couldn't match expected type `GameState'
with actual type `PlayerCommand'
Expected type: GameState -> GameState
Actual type: PlayerCommand -> GameState -> GameState
In the first argument of `(<$)', namely `updateGS'
In the second argument of `($)', namely `updateGS <$ eInput'
不知道該怎麼做了一番。我會看看你的例子,看看答案是否清晰。感謝您的排除accumB
是正確的路要走,因爲我專注於stepper
我研究建議的代碼越多,我越感到困惑的類型錯誤。
我改變了'(<$)' to '(<$>)'但它需要被批准。 –
謝謝。我沒有收到任何有關您的編輯的消息,但我自己就改變了它。 –