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