Browse Source

Fixes boostfuel refill calculation.

Abhinav Sarkar 5 years ago
parent
commit
8631d00bcc
2 changed files with 71 additions and 71 deletions
  1. 7
    10
      src/Hastron/Game/Engine.hs
  2. 64
    61
      src/Hastron/Game/Types.hs

+ 7
- 10
src/Hastron/Game/Engine.hs View File

@@ -34,7 +34,7 @@ stepGameEngine gameSettings gameMap player =
34 34
   . (\rwst -> execRWST rwst gameSettings (gameMap, player))
35 35
   . _runGameEngine
36 36
 
37
-move :: Int -> GameEngine ()
37
+move :: TimeInterval -> GameEngine ()
38 38
 move timeElapsed = do
39 39
   state    <- get
40 40
   settings <- ask
@@ -48,7 +48,7 @@ move timeElapsed = do
48 48
       where
49 49
         (x, y)               = playerPosition
50 50
         (Velocity speed dir) = playerVelocity
51
-        PlayerBoost{ .. }      = playerBoost
51
+        PlayerBoost{ .. }    = playerBoost
52 52
         dist                 =
53 53
           timeElapsed * speed * (if boostActive && boostFuel > 0 then gameBoostFactor else 1)
54 54
 
@@ -95,9 +95,6 @@ rightTurn Right = Down
95 95
 rightTurn Up    = Right
96 96
 rightTurn Down  = Left
97 97
 
98
-noTurn :: Direction -> Direction
99
-noTurn = id
100
-
101 98
 turn :: (Direction -> Direction) -> GameEngine ()
102 99
 turn turnFn = modify . second $ \player@Player{ playerVelocity = Velocity speed dir } ->
103 100
   player { playerVelocity = Velocity speed $ turnFn dir }
@@ -106,21 +103,21 @@ changeBoost :: Bool -> GameEngine ()
106 103
 changeBoost boostActive = modify . second $ \player@Player{ .. } ->
107 104
   player { playerBoost = playerBoost { boostActive = boostActive } }
108 105
 
109
-refillBoost :: Int -> GameEngine ()
106
+refillBoost :: Timestamp -> GameEngine ()
110 107
 refillBoost timeElapsed = do
111 108
   (gameMap, player@Player{ .. }) <- get
112 109
   GameSettings{ .. }             <- ask
113 110
   let boostFuel'   = floor (fromIntegral timeElapsed * gameBoostRefillFactor)
114
-      playerBoost' = playerBoost { boostFuel = boostFuel' }
111
+      playerBoost' = playerBoost { boostFuel = boostFuel playerBoost + boostFuel' }
115 112
   put (gameMap, player { playerBoost = playerBoost' })
116 113
   tell $ DList.fromList [OutPlayerBoostChange playerId playerBoost' | playerBoost /= playerBoost']
117 114
 
118
-stepGame :: Game -> Int -> InEvent -> (Game, [OutEvent])
115
+stepGame :: Game -> Timestamp -> InEvent -> (Game, [OutEvent])
119 116
 stepGame game@Game{ gameMap = gameMap@GameMap{ .. }, .. } time = stepGame'
120 117
   where
121 118
     stepGame' (InPlayerTurnLeft playerId)                = stepEvent playerId $ turn leftTurn
122 119
     stepGame' (InPlayerTurnRight playerId)               = stepEvent playerId $ turn rightTurn
123
-    stepGame' (InPlayerIdle playerId)                    = stepEvent playerId $ turn noTurn
120
+    stepGame' (InPlayerIdle playerId)                    = stepEvent playerId $ return ()
124 121
     stepGame' (InPlayerBoostChange playerId boostActive) = stepEvent playerId $ changeBoost boostActive
125 122
 
126 123
     stepEvent pId step =
@@ -144,7 +141,7 @@ stepGame game@Game{ gameMap = gameMap@GameMap{ .. }, .. } time = stepGame'
144 141
 
145 142
     score (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
146 143
 
147
-runGame :: Game -> [(Int, InEvent)] -> (Game, [OutEvent])
144
+runGame :: Game -> [(Timestamp, InEvent)] -> (Game, [OutEvent])
148 145
 runGame initialGame =
149 146
   foldl (\(game, outEvents) (time, inEvent) ->
150 147
             fmap (outEvents ++) $ stepGame game time inEvent)

+ 64
- 61
src/Hastron/Game/Types.hs View File

@@ -7,75 +7,78 @@ import           Data.HashSet        (HashSet)
7 7
 import qualified Data.HashSet        as Set
8 8
 import           Data.Tuple          (swap)
9 9
 
10
-type Point = (Int, Int)
10
+type Point          = (Int, Int)
11 11
 
12
-data Direction = Left | Right | Up | Down deriving (Show, Eq, Ord, Enum)
12
+type Timestamp      = Int
13 13
 
14
-data Velocity = Velocity Int Direction deriving (Show, Eq, Ord)
14
+type TimeInterval   = Int
15 15
 
16
-type PlayerId = Int
16
+data Direction      = Left | Right | Up | Down deriving (Show, Eq, Ord, Enum)
17 17
 
18
-data PlayerState = PlayerAlive
19
-                 | PlayerDead
20
-                 | PlayerDisconnected
21
-                 | PlayerLeft
22
-                 deriving (Show, Eq, Ord, Enum)
18
+data Velocity       = Velocity Int Direction deriving (Show, Eq, Ord)
23 19
 
24
-type PlayerTrail = [Point]
20
+type PlayerId       = Int
25 21
 
26
-data PlayerBoost = PlayerBoost { boostActive :: Bool
27
-                               , boostFuel   :: Int
28
-                               } deriving (Show, Eq)
22
+data PlayerState    = PlayerAlive
23
+                    | PlayerDead
24
+                    | PlayerDisconnected
25
+                    | PlayerLeft
26
+                    deriving (Show, Eq, Ord, Enum)
29 27
 
30
-type PlayerScore = Int
28
+type PlayerTrail    = [Point]
31 29
 
32
-data Player = Player { playerId            :: PlayerId
33
-                     , playerState         :: PlayerState
34
-                     , playerPosition      :: Point
35
-                     , playerVelocity      :: Velocity
36
-                     , playerTrail         :: PlayerTrail
37
-                     , playerBoost         :: PlayerBoost
38
-                     , playerScore         :: PlayerScore
39
-                     , playerLastEventTime :: Int
40
-                     } deriving (Show, Eq)
30
+data PlayerBoost    = PlayerBoost { boostActive :: Bool
31
+                                  , boostFuel   :: Int
32
+                                  } deriving (Show, Eq)
33
+
34
+type PlayerScore    = Int
35
+
36
+data Player         = Player { playerId            :: PlayerId
37
+                             , playerState         :: PlayerState
38
+                             , playerPosition      :: Point
39
+                             , playerVelocity      :: Velocity
40
+                             , playerTrail         :: PlayerTrail
41
+                             , playerBoost         :: PlayerBoost
42
+                             , playerScore         :: PlayerScore
43
+                             , playerLastEventTime :: Int
44
+                             } deriving (Show, Eq)
41 45
 
42 46
 data PlayerEndState = PlayerWinner | PlayerLoser | PlayerDropped
43 47
                       deriving (Show, Eq, Ord, Enum)
44 48
 
45
-data GameState = GameStarted | GameInit | GameFinished
46
-                 deriving (Show, Eq, Ord, Enum)
47
-
48
-data GameMap = GameMap { size                 :: Int
49
-                       , gameMapBlockedPoints :: HashSet Point }
50
-               deriving (Show, Eq)
49
+data GameState      = GameStarted | GameInit | GameFinished
50
+                      deriving (Show, Eq, Ord, Enum)
51 51
 
52
-data GameSettings = GameSettings { gameBoostFactor       :: Int
53
-                                 , gameBoostRefillFactor :: Double
54
-                                 }
52
+data GameMap        = GameMap { size                 :: Int
53
+                              , gameMapBlockedPoints :: HashSet Point
54
+                              } deriving (Show, Eq)
55
+
56
+data GameSettings   = GameSettings { gameBoostFactor       :: Int
57
+                                   , gameBoostRefillFactor :: Double
58
+                                   } deriving (Show, Eq)
59
+
60
+data Game           = Game { gamePlayers  :: HashMap PlayerId Player
61
+                           , gameState    :: GameState
62
+                           , gameSettings :: GameSettings
63
+                           , gameMap      :: GameMap
64
+                           } deriving (Show, Eq)
65
+
66
+type GameResult     = HashMap PlayerId (PlayerScore, PlayerEndState)
67
+
68
+data InEvent        = InPlayerTurnLeft PlayerId
69
+                    | InPlayerTurnRight PlayerId
70
+                    | InPlayerBoostChange PlayerId Bool
71
+                    | InPlayerStateChange PlayerId PlayerState
72
+                    | InPlayerIdle PlayerId
73
+                    deriving (Show, Eq, Ord)
74
+
75
+data OutEvent       = OutPlayerPosition PlayerId Point Direction
76
+                    | OutPlayerStateChange PlayerId PlayerState
77
+                    | OutPlayerBoostChange PlayerId PlayerBoost
78
+                    | OutGameStateChange GameState
79
+                    | OutGameOver GameResult
55 80
                     deriving (Show, Eq)
56 81
 
57
-data Game = Game { gamePlayers  :: HashMap PlayerId Player
58
-                 , gameState    :: GameState
59
-                 , gameSettings :: GameSettings
60
-                 , gameMap      :: GameMap
61
-                 } deriving (Show, Eq)
62
-
63
-type GameResult = HashMap PlayerId (PlayerScore, PlayerEndState)
64
-
65
-data InEvent = InPlayerTurnLeft PlayerId
66
-             | InPlayerTurnRight PlayerId
67
-             | InPlayerBoostChange PlayerId Bool
68
-             | InPlayerStateChange PlayerId PlayerState
69
-             | InPlayerIdle PlayerId
70
-             deriving (Show, Eq, Ord)
71
-
72
-data OutEvent = OutPlayerPosition PlayerId Point Direction
73
-              | OutPlayerStateChange PlayerId PlayerState
74
-              | OutPlayerBoostChange PlayerId PlayerBoost
75
-              | OutGameStateChange GameState
76
-              | OutGameOver GameResult
77
-              deriving (Show, Eq)
78
-
79 82
 newGameMap :: Int -> GameMap
80 83
 newGameMap size = GameMap size $ Set.fromList borderPoints
81 84
   where
@@ -83,18 +86,18 @@ newGameMap size = GameMap size $ Set.fromList borderPoints
83 86
                    in xs ++ map swap xs
84 87
 
85 88
 newGame :: Int -> GameSettings -> Game
86
-newGame size gameSettings = Game { gamePlayers   = Map.empty
87
-                                  , gameState    = GameInit
88
-                                  , gameSettings = gameSettings
89
-                                  , gameMap      = newGameMap size
90
-                                  }
89
+newGame size gameSettings = Game { gamePlayers  = Map.empty
90
+                                 , gameState    = GameInit
91
+                                 , gameSettings = gameSettings
92
+                                 , gameMap      = newGameMap size
93
+                                 }
91 94
 
92 95
 newPlayer :: Int -> Point -> Velocity -> Int -> Player
93 96
 newPlayer pId pos velocity boost =
94 97
   Player pId PlayerAlive pos velocity [pos] (PlayerBoost False boost) 0 0
95 98
 
96 99
 addPlayer :: Game -> Player -> Game
97
-addPlayer game@Game{gameMap = gameMap@GameMap{..}, ..} player@Player{..} =
100
+addPlayer game@Game{ gameMap = gameMap@GameMap{ .. }, .. } player@Player{ .. } =
98 101
   game { gamePlayers = Map.insert playerId player gamePlayers
99 102
        , gameMap     = gameMap { gameMapBlockedPoints =
100
-                                   Set.insert playerPosition gameMapBlockedPoints }}
103
+                                   Set.insert playerPosition gameMapBlockedPoints } }

Loading…
Cancel
Save