aboutsummaryrefslogtreecommitdiff
path: root/Demos
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-01-02 14:39:54 -0800
committer3gg <3gg@shellblade.net>2025-01-02 14:39:54 -0800
commit9fcbd69c0e2ffc13d092db2656b5f28692980dd9 (patch)
tree049e765b61f8c21b9202c091e55eb27183843c7d /Demos
parent41c9d71be98eac9b0131aad14cb8f7352eedac44 (diff)
Minor tweaks.
Diffstat (limited to 'Demos')
-rw-r--r--Demos/Pong/Pong.hs15
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
117update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject 115update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject
118update elapsed dt events gos go = 116update 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
167stepEnemy = movePad 0 .> clamp 165stepEnemy = movePad 0 .> spure clamp
168 166
169movePad :: Float -> Step [GameObject] e GameObject GameObject 167movePad :: Float -> Step [GameObject] e GameObject GameObject
170movePad previousMomentumVector = step $ \_ dt gos _ pad -> 168movePad 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
178sign :: Float -> Float
179sign x = if x >= 0 then 1 else -1
180
181 176
182-- Player stepper 177-- Player stepper
183 178
184stepPlayer = sfold movePlayer .> clamp 179stepPlayer = sfold movePlayer .> spure clamp
185 180
186movePlayer = mconcat 181movePlayer = mconcat
187 [ swhen MoveLeft $ movePlayer' (vec2 (-playerSpeed) 0) 182 [ swhen MoveLeft $ movePlayer' (vec2 (-playerSpeed) 0)
@@ -191,8 +186,8 @@ movePlayer = mconcat
191movePlayer' :: Vector2 -> Step s e GameObject GameObject 186movePlayer' :: Vector2 -> Step s e GameObject GameObject
192movePlayer' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, movePlayer' dir) 187movePlayer' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, movePlayer' dir)
193 188
194clamp :: Step s e GameObject GameObject 189clamp :: GameObject -> GameObject
195clamp = spure $ \go -> 190clamp 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