Fixes boostfuel refill calculation.
This commit is contained in:
parent
359f1b83c8
commit
8631d00bcc
|
@ -34,7 +34,7 @@ stepGameEngine gameSettings gameMap player =
|
||||||
. (\rwst -> execRWST rwst gameSettings (gameMap, player))
|
. (\rwst -> execRWST rwst gameSettings (gameMap, player))
|
||||||
. _runGameEngine
|
. _runGameEngine
|
||||||
|
|
||||||
move :: Int -> GameEngine ()
|
move :: TimeInterval -> GameEngine ()
|
||||||
move timeElapsed = do
|
move timeElapsed = do
|
||||||
state <- get
|
state <- get
|
||||||
settings <- ask
|
settings <- ask
|
||||||
|
@ -95,9 +95,6 @@ rightTurn Right = Down
|
||||||
rightTurn Up = Right
|
rightTurn Up = Right
|
||||||
rightTurn Down = Left
|
rightTurn Down = Left
|
||||||
|
|
||||||
noTurn :: Direction -> Direction
|
|
||||||
noTurn = id
|
|
||||||
|
|
||||||
turn :: (Direction -> Direction) -> GameEngine ()
|
turn :: (Direction -> Direction) -> GameEngine ()
|
||||||
turn turnFn = modify . second $ \player@Player{ playerVelocity = Velocity speed dir } ->
|
turn turnFn = modify . second $ \player@Player{ playerVelocity = Velocity speed dir } ->
|
||||||
player { playerVelocity = Velocity speed $ turnFn dir }
|
player { playerVelocity = Velocity speed $ turnFn dir }
|
||||||
|
@ -106,21 +103,21 @@ changeBoost :: Bool -> GameEngine ()
|
||||||
changeBoost boostActive = modify . second $ \player@Player{ .. } ->
|
changeBoost boostActive = modify . second $ \player@Player{ .. } ->
|
||||||
player { playerBoost = playerBoost { boostActive = boostActive } }
|
player { playerBoost = playerBoost { boostActive = boostActive } }
|
||||||
|
|
||||||
refillBoost :: Int -> GameEngine ()
|
refillBoost :: Timestamp -> GameEngine ()
|
||||||
refillBoost timeElapsed = do
|
refillBoost timeElapsed = do
|
||||||
(gameMap, player@Player{ .. }) <- get
|
(gameMap, player@Player{ .. }) <- get
|
||||||
GameSettings{ .. } <- ask
|
GameSettings{ .. } <- ask
|
||||||
let boostFuel' = floor (fromIntegral timeElapsed * gameBoostRefillFactor)
|
let boostFuel' = floor (fromIntegral timeElapsed * gameBoostRefillFactor)
|
||||||
playerBoost' = playerBoost { boostFuel = boostFuel' }
|
playerBoost' = playerBoost { boostFuel = boostFuel playerBoost + boostFuel' }
|
||||||
put (gameMap, player { playerBoost = playerBoost' })
|
put (gameMap, player { playerBoost = playerBoost' })
|
||||||
tell $ DList.fromList [OutPlayerBoostChange playerId playerBoost' | playerBoost /= playerBoost']
|
tell $ DList.fromList [OutPlayerBoostChange playerId playerBoost' | playerBoost /= playerBoost']
|
||||||
|
|
||||||
stepGame :: Game -> Int -> InEvent -> (Game, [OutEvent])
|
stepGame :: Game -> Timestamp -> InEvent -> (Game, [OutEvent])
|
||||||
stepGame game@Game{ gameMap = gameMap@GameMap{ .. }, .. } time = stepGame'
|
stepGame game@Game{ gameMap = gameMap@GameMap{ .. }, .. } time = stepGame'
|
||||||
where
|
where
|
||||||
stepGame' (InPlayerTurnLeft playerId) = stepEvent playerId $ turn leftTurn
|
stepGame' (InPlayerTurnLeft playerId) = stepEvent playerId $ turn leftTurn
|
||||||
stepGame' (InPlayerTurnRight playerId) = stepEvent playerId $ turn rightTurn
|
stepGame' (InPlayerTurnRight playerId) = stepEvent playerId $ turn rightTurn
|
||||||
stepGame' (InPlayerIdle playerId) = stepEvent playerId $ turn noTurn
|
stepGame' (InPlayerIdle playerId) = stepEvent playerId $ return ()
|
||||||
stepGame' (InPlayerBoostChange playerId boostActive) = stepEvent playerId $ changeBoost boostActive
|
stepGame' (InPlayerBoostChange playerId boostActive) = stepEvent playerId $ changeBoost boostActive
|
||||||
|
|
||||||
stepEvent pId step =
|
stepEvent pId step =
|
||||||
|
@ -144,7 +141,7 @@ stepGame game@Game{ gameMap = gameMap@GameMap{ .. }, .. } time = 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 -> [(Int, InEvent)] -> (Game, [OutEvent])
|
runGame :: Game -> [(Timestamp, InEvent)] -> (Game, [OutEvent])
|
||||||
runGame initialGame =
|
runGame initialGame =
|
||||||
foldl (\(game, outEvents) (time, inEvent) ->
|
foldl (\(game, outEvents) (time, inEvent) ->
|
||||||
fmap (outEvents ++) $ stepGame game time inEvent)
|
fmap (outEvents ++) $ stepGame game time inEvent)
|
||||||
|
|
|
@ -9,6 +9,10 @@ import Data.Tuple (swap)
|
||||||
|
|
||||||
type Point = (Int, Int)
|
type Point = (Int, Int)
|
||||||
|
|
||||||
|
type Timestamp = Int
|
||||||
|
|
||||||
|
type TimeInterval = Int
|
||||||
|
|
||||||
data Direction = Left | Right | Up | Down deriving (Show, Eq, Ord, Enum)
|
data Direction = Left | Right | Up | Down deriving (Show, Eq, Ord, Enum)
|
||||||
|
|
||||||
data Velocity = Velocity Int Direction deriving (Show, Eq, Ord)
|
data Velocity = Velocity Int Direction deriving (Show, Eq, Ord)
|
||||||
|
@ -46,13 +50,12 @@ data GameState = GameStarted | GameInit | GameFinished
|
||||||
deriving (Show, Eq, Ord, Enum)
|
deriving (Show, Eq, Ord, Enum)
|
||||||
|
|
||||||
data GameMap = GameMap { size :: Int
|
data GameMap = GameMap { size :: Int
|
||||||
, gameMapBlockedPoints :: HashSet Point }
|
, gameMapBlockedPoints :: HashSet Point
|
||||||
deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
data GameSettings = GameSettings { gameBoostFactor :: Int
|
data GameSettings = GameSettings { gameBoostFactor :: Int
|
||||||
, gameBoostRefillFactor :: Double
|
, gameBoostRefillFactor :: Double
|
||||||
}
|
} deriving (Show, Eq)
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
data Game = Game { gamePlayers :: HashMap PlayerId Player
|
data Game = Game { gamePlayers :: HashMap PlayerId Player
|
||||||
, gameState :: GameState
|
, gameState :: GameState
|
||||||
|
|
Loading…
Reference in New Issue