Changes move to take time elapsed into account.
parent
75a7c0ce70
commit
a2e6bca7b2
|
@ -22,16 +22,20 @@ rightTurn Down = Left
|
||||||
noTurn :: Direction -> Direction
|
noTurn :: Direction -> Direction
|
||||||
noTurn = id
|
noTurn = id
|
||||||
|
|
||||||
move :: Point -> Velocity -> PlayerTrail
|
move :: Int -> Point -> Velocity -> PlayerTrail
|
||||||
move (x, y) (Velocity speed Left) = tail [(x', y) | x' <- [x, x - 1 .. x - speed]]
|
move timeElapsed (x, y) (Velocity speed dir) = move' dir
|
||||||
move (x, y) (Velocity speed Right) = tail [(x', y) | x' <- [x .. x + speed]]
|
where
|
||||||
move (x, y) (Velocity speed Up) = tail [(x, y') | y' <- [y, y - 1 .. y - speed]]
|
dist = timeElapsed * speed
|
||||||
move (x, y) (Velocity speed Down) = tail [(x, y') | y' <- [y .. y + speed]]
|
|
||||||
|
|
||||||
moveAfterTurn :: (Direction -> Direction) -> Point -> Velocity -> (PlayerTrail, Velocity)
|
move' Left = tail [(x', y) | x' <- [x, x - 1 .. x - dist]]
|
||||||
moveAfterTurn turn point (Velocity speed dir) =
|
move' Right = tail [(x', y) | x' <- [x .. x + dist]]
|
||||||
|
move' Up = tail [(x, y') | y' <- [y, y - 1 .. y - dist]]
|
||||||
|
move' Down = tail [(x, y') | y' <- [y .. y + dist]]
|
||||||
|
|
||||||
|
moveAfterTurn :: (Direction -> Direction) -> Int -> Point -> Velocity -> (PlayerTrail, Velocity)
|
||||||
|
moveAfterTurn turn timeElapsed point (Velocity speed dir) =
|
||||||
let vel' = Velocity speed $ turn dir
|
let vel' = Velocity speed $ turn dir
|
||||||
in (move point vel', vel')
|
in (move timeElapsed point vel', vel')
|
||||||
|
|
||||||
checkTrail :: GameMap -> PlayerTrail -> (PlayerTrail, PlayerState)
|
checkTrail :: GameMap -> PlayerTrail -> (PlayerTrail, PlayerState)
|
||||||
checkTrail GameMap{..} trail =
|
checkTrail GameMap{..} trail =
|
||||||
|
@ -40,12 +44,15 @@ checkTrail GameMap{..} trail =
|
||||||
then (trail, PlayerAlive)
|
then (trail, PlayerAlive)
|
||||||
else (trail', PlayerDead)
|
else (trail', PlayerDead)
|
||||||
|
|
||||||
stepGame :: Game -> InEvent -> (Game, [OutEvent])
|
stepGame :: Game -> Int -> InEvent -> (Game, [OutEvent])
|
||||||
stepGame game@Game{gameMap = gameMap@GameMap{..}, ..} = stepGame'
|
stepGame game@Game{gameMap = gameMap@GameMap{..}, ..} timeElapsed = stepGame'
|
||||||
where
|
where
|
||||||
stepGame' (InPlayerTurnLeft playerId) = stepTurnEvent playerId $ moveAfterTurn leftTurn
|
stepGame' (InPlayerTurnLeft playerId) =
|
||||||
stepGame' (InPlayerTurnRight playerId) = stepTurnEvent playerId $ moveAfterTurn rightTurn
|
stepTurnEvent playerId $ moveAfterTurn leftTurn timeElapsed
|
||||||
stepGame' (InPlayerIdle playerId) = stepTurnEvent playerId $ moveAfterTurn noTurn
|
stepGame' (InPlayerTurnRight playerId) =
|
||||||
|
stepTurnEvent playerId $ moveAfterTurn rightTurn timeElapsed
|
||||||
|
stepGame' (InPlayerIdle playerId) =
|
||||||
|
stepTurnEvent playerId $ moveAfterTurn noTurn timeElapsed
|
||||||
|
|
||||||
stepTurnEvent pId moveFn =
|
stepTurnEvent pId moveFn =
|
||||||
flip (maybe (game, [])) (Map.lookup pId gamePlayers) $ \player@Player{..} ->
|
flip (maybe (game, [])) (Map.lookup pId gamePlayers) $ \player@Player{..} ->
|
||||||
|
@ -72,8 +79,8 @@ stepGame game@Game{gameMap = gameMap@GameMap{..}, ..} = stepGame'
|
||||||
|
|
||||||
score (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
|
score (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
|
||||||
|
|
||||||
runGame :: Game -> [InEvent] -> (Game, [OutEvent])
|
runGame :: Game -> [(Int, InEvent)] -> (Game, [OutEvent])
|
||||||
runGame initialGame =
|
runGame initialGame inEvents =
|
||||||
foldl (\(game, outEvents) inEvent -> let (game', outEvents') = stepGame game inEvent
|
foldl (\(game, outEvents) (timeElapsed, inEvent) ->
|
||||||
in (game', outEvents ++ outEvents'))
|
fmap (outEvents ++) $ stepGame game timeElapsed inEvent)
|
||||||
(initialGame, [])
|
(initialGame, []) inEvents
|
||||||
|
|
Loading…
Reference in New Issue