diff options
Diffstat (limited to 'Demos/Pong/Pong.hs')
-rw-r--r-- | Demos/Pong/Pong.hs | 133 |
1 files changed, 73 insertions, 60 deletions
diff --git a/Demos/Pong/Pong.hs b/Demos/Pong/Pong.hs index b9661ee..0df05ea 100644 --- a/Demos/Pong/Pong.hs +++ b/Demos/Pong/Pong.hs | |||
@@ -3,12 +3,11 @@ | |||
3 | {-# LANGUAGE TypeSynonymInstances #-} | 3 | {-# LANGUAGE TypeSynonymInstances #-} |
4 | 4 | ||
5 | module Pong | 5 | module Pong |
6 | ( GameEvent (..), | 6 | ( GameEvent (..) |
7 | GameObject, | 7 | , GameObject |
8 | newWorld, | 8 | , newWorld |
9 | stepWorld, | 9 | , stepWorld |
10 | aabb, | 10 | ) |
11 | ) | ||
12 | where | 11 | where |
13 | 12 | ||
14 | import Spear.Math.AABB | 13 | import Spear.Math.AABB |
@@ -16,16 +15,17 @@ import Spear.Math.Algebra | |||
16 | import Spear.Math.Spatial | 15 | import Spear.Math.Spatial |
17 | import Spear.Math.Spatial2 | 16 | import Spear.Math.Spatial2 |
18 | import Spear.Math.Vector | 17 | import Spear.Math.Vector |
18 | import Spear.Physics.Collision | ||
19 | import Spear.Prelude | 19 | import Spear.Prelude |
20 | import Spear.Step | 20 | import Spear.Step |
21 | 21 | ||
22 | import Data.Monoid (mconcat) | 22 | import Data.Monoid (mconcat) |
23 | 23 | ||
24 | 24 | ||
25 | -- Configuration | 25 | -- Configuration |
26 | 26 | ||
27 | padSize = vec2 0.07 0.015 | 27 | padSize = vec2 0.070 0.015 |
28 | ballSize = 0.012 :: Float | 28 | ballSize = vec2 0.012 0.012 |
29 | ballSpeed = 0.7 :: Float | 29 | ballSpeed = 0.7 :: Float |
30 | initialBallVelocity = vec2 1 1 | 30 | initialBallVelocity = vec2 1 1 |
31 | maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) | 31 | maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) |
@@ -41,14 +41,22 @@ initialBallPos = vec2 0.5 0.5 | |||
41 | data GameEvent | 41 | data GameEvent |
42 | = MoveLeft | 42 | = MoveLeft |
43 | | MoveRight | 43 | | MoveRight |
44 | deriving (Eq, Ord, Show) | 44 | | Collision GameObjectId GameObjectId |
45 | deriving (Eq, Show) | ||
45 | 46 | ||
46 | -- Game objects | 47 | -- Game objects |
47 | 48 | ||
49 | data GameObjectId | ||
50 | = Ball | ||
51 | | Enemy | ||
52 | | Player | ||
53 | deriving (Eq, Show) | ||
54 | |||
48 | data GameObject = GameObject | 55 | data GameObject = GameObject |
49 | { aabb :: AABB2, | 56 | { gameObjectId :: !GameObjectId |
50 | basis :: Transform2, | 57 | , gameObjectSize :: {-# UNPACK #-} !Vector2 |
51 | gostep :: Step [GameObject] [GameEvent] GameObject GameObject | 58 | , basis :: {-# UNPACK #-} !Transform2 |
59 | , gostep :: Step [GameObject] [GameEvent] GameObject GameObject | ||
52 | } | 60 | } |
53 | 61 | ||
54 | 62 | ||
@@ -78,76 +86,83 @@ instance Spatial GameObject Vector2 Angle Transform2 where | |||
78 | transform = basis | 86 | transform = basis |
79 | 87 | ||
80 | 88 | ||
81 | ballBox, padBox :: AABB2 | 89 | instance Bounded2 GameObject where |
82 | ballBox = AABB2 (vec2 (-s) (-s)) (vec2 s s) where s = ballSize | 90 | boundingVolume obj = aabb2Volume $ translate (position obj) (AABB2 (-size) size) |
83 | padBox = AABB2 (-padSize) padSize | 91 | where size = gameObjectSize obj |
92 | |||
84 | 93 | ||
85 | newWorld = | 94 | newWorld = |
86 | [ GameObject ballBox (makeAt initialBallPos) $ stepBall initialBallVelocity, | 95 | [ GameObject Ball ballSize (makeAt initialBallPos) $ stepBall initialBallVelocity, |
87 | GameObject padBox (makeAt initialEnemyPos) stepEnemy, | 96 | GameObject Enemy padSize (makeAt initialEnemyPos) stepEnemy, |
88 | GameObject padBox (makeAt initialPlayerPos) stepPlayer | 97 | GameObject Player padSize (makeAt initialPlayerPos) stepPlayer |
89 | ] | 98 | ] |
90 | where makeAt = newTransform2 unitx2 unity2 | 99 | where makeAt = newTransform2 unitx2 unity2 |
91 | 100 | ||
92 | 101 | ||
102 | -- Step the game world: | ||
103 | -- 1. Simulate physics. | ||
104 | -- 2. Collide objects and clip -> produce collision events. | ||
105 | -- 3. Update game objects <- input collision events. | ||
93 | stepWorld :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> [GameObject] | 106 | stepWorld :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> [GameObject] |
94 | stepWorld elapsed dt evts gos = map (update elapsed dt evts gos) gos | 107 | stepWorld elapsed dt events gos@[ball, enemy, player] = |
108 | let | ||
109 | collisions = collide [ball] [enemy, player] | ||
110 | collisionEvents = (\(x,y) -> Collision (gameObjectId x) (gameObjectId y)) <$> collisions | ||
111 | events' = events ++ collisionEvents | ||
112 | in | ||
113 | map (update elapsed dt events' gos) gos | ||
95 | 114 | ||
96 | update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject | 115 | update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject |
97 | update elapsed dt evts gos go = | 116 | update elapsed dt events gos go = |
98 | let (go', s') = runStep (gostep go) elapsed dt gos evts go | 117 | let (go', s') = runStep (gostep go) elapsed dt gos events go |
99 | in go' {gostep = s'} | 118 | in go' { gostep = s' } |
119 | |||
100 | 120 | ||
101 | -- Ball steppers | 121 | -- Ball steppers |
102 | 122 | ||
103 | stepBall vel = collideBall vel .> moveBall | 123 | stepBall vel = bounceBall vel .> moveBall -- .> clamp |
104 | 124 | ||
105 | collideBall :: Vector2 -> Step [GameObject] e GameObject (Vector2, GameObject) | 125 | bounceBall :: Vector2 -> Step [GameObject] [GameEvent] GameObject (Vector2, GameObject) |
106 | collideBall vel = step $ \_ dt gos _ ball -> | 126 | bounceBall vel = step $ \_ dt gos events ball -> |
107 | let (AABB2 pmin pmax) = translate (position ball) (aabb ball) | 127 | let (AABB2Volume (AABB2 pmin pmax)) = boundingVolume ball |
108 | sideCollision = x pmin < 0 || x pmax > 1 | 128 | sideCollision = x pmin < 0 || x pmax > 1 |
109 | backCollision = y pmin < 0 || y pmax > 1 | 129 | backCollision = y pmin < 0 || y pmax > 1 |
110 | flipX v@(Vector2 x y) = if sideCollision then vec2 (-x) y else v | 130 | flipX v@(Vector2 x y) = if sideCollision then vec2 (-x) y else v |
111 | flipY v@(Vector2 x y) = if backCollision then vec2 x (-y) else v | 131 | flipY v@(Vector2 x y) = if backCollision then vec2 x (-y) else v |
112 | vel' = normalise . (\v -> foldl (paddleBounce ball) v (tail gos)) . flipX . flipY $ vel | 132 | collideWithPaddles vel = foldl (paddleBounce ball events) vel (tail gos) |
133 | vel' = normalise | ||
134 | . collideWithPaddles | ||
135 | . flipX | ||
136 | . flipY | ||
137 | $ vel | ||
113 | collision = vel' /= vel | 138 | collision = vel' /= vel |
114 | -- Apply offset when collision occurs to avoid sticky collisions. | 139 | -- Apply offset when collision occurs to avoid sticky collisions. |
115 | delta = (1::Float) + if collision then (3::Float)*dt else (0::Float) | 140 | delta = (1::Float) + if collision then (3::Float)*dt else (0::Float) |
116 | in ((ballSpeed * delta * vel', ball), collideBall vel') | 141 | in ((ballSpeed * delta * vel', ball), bounceBall vel') |
117 | 142 | ||
118 | paddleBounce :: GameObject -> Vector2 -> GameObject -> Vector2 | 143 | paddleBounce :: GameObject -> [GameEvent] -> Vector2 -> GameObject -> Vector2 |
119 | paddleBounce ball v paddle = | 144 | paddleBounce ball events vel paddle = |
120 | if collide ball paddle | 145 | let collision = Collision Ball (gameObjectId paddle) `elem` events |
146 | in if collision | ||
121 | then | 147 | then |
122 | let (AABB2 pmin pmax) = translate (position paddle) (aabb paddle) | 148 | let (AABB2Volume (AABB2 pmin pmax)) = boundingVolume paddle |
123 | center = (x pmin + x pmax) / (2::Float) | 149 | center = (x pmin + x pmax) / (2::Float) |
124 | -- Normalized offset of the ball from the paddle's center, [-1, +1]. | 150 | -- Normalized offset of the ball from the paddle's center, [-1, +1]. |
125 | -- It's outside the [-1, +1] range if there is no collision. | 151 | -- It's outside the [-1, +1] range if there is no collision. |
126 | offset = (x (position ball) - center) / ((x pmax - x pmin) / (2::Float)) | 152 | offset = (x (position ball) - center) / ((x pmax - x pmin) / (2::Float)) |
127 | angle = offset * maxBounceAngle | 153 | angle = offset * maxBounceAngle |
128 | -- When it bounces off of a paddle, y vel is flipped. | 154 | -- When it bounces off of a paddle, y vel is flipped. |
129 | ysign = -(signum (y v)) | 155 | ysign = -(signum (y vel)) |
130 | in vec2 (sin angle) (ysign * cos angle) | 156 | in vec2 (sin angle) (ysign * cos angle) |
131 | else v | 157 | else vel |
132 | |||
133 | collide :: GameObject -> GameObject -> Bool | ||
134 | collide go1 go2 = | ||
135 | let (AABB2 (Vector2 xmin1 ymin1) (Vector2 xmax1 ymax1)) = | ||
136 | translate (position go1) (aabb go1) | ||
137 | (AABB2 (Vector2 xmin2 ymin2) (Vector2 xmax2 ymax2)) = | ||
138 | translate (position go2) (aabb go2) | ||
139 | in not $ | ||
140 | xmax1 < xmin2 || | ||
141 | xmin1 > xmax2 || | ||
142 | ymax1 < ymin2 || | ||
143 | ymin1 > ymax2 | ||
144 | 158 | ||
145 | moveBall :: Step s e (Vector2, GameObject) GameObject | 159 | moveBall :: Step s e (Vector2, GameObject) GameObject |
146 | moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall) | 160 | moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall) |
147 | 161 | ||
162 | |||
148 | -- Enemy stepper | 163 | -- Enemy stepper |
149 | 164 | ||
150 | stepEnemy = movePad 0 .> clamp | 165 | stepEnemy = movePad 0 .> spure clamp |
151 | 166 | ||
152 | movePad :: Float -> Step [GameObject] e GameObject GameObject | 167 | movePad :: Float -> Step [GameObject] e GameObject GameObject |
153 | movePad previousMomentumVector = step $ \_ dt gos _ pad -> | 168 | movePad previousMomentumVector = step $ \_ dt gos _ pad -> |
@@ -158,28 +173,26 @@ movePad previousMomentumVector = step $ \_ dt gos _ pad -> | |||
158 | vx = chaseVector * dt + momentumVector | 173 | vx = chaseVector * dt + momentumVector |
159 | in (translate (vec2 vx 0) pad, movePad momentumVector) | 174 | in (translate (vec2 vx 0) pad, movePad momentumVector) |
160 | 175 | ||
161 | sign :: Float -> Float | ||
162 | sign x = if x >= 0 then 1 else -1 | ||
163 | 176 | ||
164 | -- Player stepper | 177 | -- Player stepper |
165 | 178 | ||
166 | stepPlayer = sfold moveGO .> clamp | 179 | stepPlayer = sfold movePlayer .> spure clamp |
167 | 180 | ||
168 | moveGO = mconcat | 181 | movePlayer = mconcat |
169 | [ swhen MoveLeft $ moveGO' (vec2 (-playerSpeed) 0) | 182 | [ swhen MoveLeft $ movePlayer' (vec2 (-playerSpeed) 0) |
170 | , swhen MoveRight $ moveGO' (vec2 playerSpeed 0) | 183 | , swhen MoveRight $ movePlayer' (vec2 playerSpeed 0) |
171 | ] | 184 | ] |
172 | 185 | ||
173 | moveGO' :: Vector2 -> Step s e GameObject GameObject | 186 | movePlayer' :: Vector2 -> Step s e GameObject GameObject |
174 | moveGO' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, moveGO' dir) | 187 | movePlayer' dir = step $ \_ dt _ _ go -> (translate (dir * dt) go, movePlayer' dir) |
175 | 188 | ||
176 | clamp :: Step s e GameObject GameObject | 189 | clamp :: GameObject -> GameObject |
177 | clamp = spure $ \go -> | 190 | clamp go = |
178 | let p' = vec2 (clamp' x s (1 - s)) y | 191 | let p' = vec2 (clamp' x sx (1 - sx)) y |
179 | (Vector2 x y) = position go | 192 | (Vector2 x y) = position go |
180 | clamp' x a b | 193 | clamp' x a b |
181 | | x < a = a | 194 | | x < a = a |
182 | | x > b = b | 195 | | x > b = b |
183 | | otherwise = x | 196 | | otherwise = x |
184 | (Vector2 s _) = padSize | 197 | (Vector2 sx _) = gameObjectSize go |
185 | in setPosition p' go | 198 | in setPosition p' go |