diff options
author | 3gg <3gg@shellblade.net> | 2025-01-02 14:39:54 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-01-02 14:39:54 -0800 |
commit | 9fcbd69c0e2ffc13d092db2656b5f28692980dd9 (patch) | |
tree | 049e765b61f8c21b9202c091e55eb27183843c7d /Demos | |
parent | 41c9d71be98eac9b0131aad14cb8f7352eedac44 (diff) |
Minor tweaks.
Diffstat (limited to 'Demos')
-rw-r--r-- | Demos/Pong/Pong.hs | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/Demos/Pong/Pong.hs b/Demos/Pong/Pong.hs index f1b3c74..0df05ea 100644 --- a/Demos/Pong/Pong.hs +++ b/Demos/Pong/Pong.hs | |||
@@ -56,7 +56,6 @@ data GameObject = GameObject | |||
56 | { gameObjectId :: !GameObjectId | 56 | { gameObjectId :: !GameObjectId |
57 | , gameObjectSize :: {-# UNPACK #-} !Vector2 | 57 | , gameObjectSize :: {-# UNPACK #-} !Vector2 |
58 | , basis :: {-# UNPACK #-} !Transform2 | 58 | , basis :: {-# UNPACK #-} !Transform2 |
59 | -- TODO: Think about storing steppers separately. | ||
60 | , gostep :: Step [GameObject] [GameEvent] GameObject GameObject | 59 | , gostep :: Step [GameObject] [GameEvent] GameObject GameObject |
61 | } | 60 | } |
62 | 61 | ||
@@ -110,9 +109,8 @@ stepWorld elapsed dt events gos@[ball, enemy, player] = | |||
110 | collisions = collide [ball] [enemy, player] | 109 | collisions = collide [ball] [enemy, player] |
111 | collisionEvents = (\(x,y) -> Collision (gameObjectId x) (gameObjectId y)) <$> collisions | 110 | collisionEvents = (\(x,y) -> Collision (gameObjectId x) (gameObjectId y)) <$> collisions |
112 | events' = events ++ collisionEvents | 111 | events' = events ++ collisionEvents |
113 | gos' = map (update elapsed dt events' gos) gos | ||
114 | in | 112 | in |
115 | gos' | 113 | map (update elapsed dt events' gos) gos |
116 | 114 | ||
117 | update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject | 115 | update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject |
118 | update elapsed dt events gos go = | 116 | update elapsed dt events gos go = |
@@ -164,7 +162,7 @@ moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall) | |||
164 | 162 | ||
165 | -- Enemy stepper | 163 | -- Enemy stepper |
166 | 164 | ||
167 | stepEnemy = movePad 0 .> clamp | 165 | stepEnemy = movePad 0 .> spure clamp |
168 | 166 | ||
169 | movePad :: Float -> Step [GameObject] e GameObject GameObject | 167 | movePad :: Float -> Step [GameObject] e GameObject GameObject |
170 | movePad previousMomentumVector = step $ \_ dt gos _ pad -> | 168 | movePad previousMomentumVector = step $ \_ dt gos _ pad -> |
@@ -175,13 +173,10 @@ movePad previousMomentumVector = step $ \_ dt gos _ pad -> | |||
175 | vx = chaseVector * dt + momentumVector | 173 | vx = chaseVector * dt + momentumVector |
176 | in (translate (vec2 vx 0) pad, movePad momentumVector) | 174 | in (translate (vec2 vx 0) pad, movePad momentumVector) |
177 | 175 | ||
178 | sign :: Float -> Float | ||
179 | sign x = if x >= 0 then 1 else -1 | ||
180 | |||
181 | 176 | ||
182 | -- Player stepper | 177 | -- Player stepper |
183 | 178 | ||
184 | stepPlayer = sfold movePlayer .> clamp | 179 | stepPlayer = sfold movePlayer .> spure clamp |
185 | 180 | ||
186 | movePlayer = mconcat | 181 | movePlayer = mconcat |
187 | [ swhen MoveLeft $ movePlayer' (vec2 (-playerSpeed) 0) | 182 | [ swhen MoveLeft $ movePlayer' (vec2 (-playerSpeed) 0) |
@@ -191,8 +186,8 @@ movePlayer = mconcat | |||
191 | movePlayer' :: Vector2 -> Step s e GameObject GameObject | 186 | movePlayer' :: Vector2 -> Step s e GameObject GameObject |
192 | movePlayer' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, movePlayer' dir) | 187 | movePlayer' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, movePlayer' dir) |
193 | 188 | ||
194 | clamp :: Step s e GameObject GameObject | 189 | clamp :: GameObject -> GameObject |
195 | clamp = spure $ \go -> | 190 | clamp go = |
196 | let p' = vec2 (clamp' x sx (1 - sx)) y | 191 | let p' = vec2 (clamp' x sx (1 - sx)) y |
197 | (Vector2 x y) = position go | 192 | (Vector2 x y) = position go |
198 | clamp' x a b | 193 | clamp' x a b |