aboutsummaryrefslogtreecommitdiff
path: root/demos/pong/Pong.hs
diff options
context:
space:
mode:
Diffstat (limited to 'demos/pong/Pong.hs')
-rw-r--r--demos/pong/Pong.hs141
1 files changed, 0 insertions, 141 deletions
diff --git a/demos/pong/Pong.hs b/demos/pong/Pong.hs
deleted file mode 100644
index accc75d..0000000
--- a/demos/pong/Pong.hs
+++ /dev/null
@@ -1,141 +0,0 @@
1module Pong
2 ( GameEvent (..),
3 GameObject,
4 newWorld,
5 stepWorld,
6 aabb,
7 )
8where
9
10import Data.Monoid (mconcat)
11import GHC.Float (double2Float)
12import Spear.Math.AABB
13import Spear.Math.Spatial2
14import Spear.Math.Vector
15import Spear.Step
16
17-- Configuration
18
19padSize = vec2 0.05 0.02
20
21ballSize = 0.01
22
23ballVelocity = vec2 0.3 0.3
24
25playerSpeed = 0.7
26
27initialEnemyPos = vec2 0.5 0.9
28
29initialPlayerPos = vec2 0.5 0.1
30
31initialBallPos = vec2 0.5 0.5
32
33-- Game events
34
35data GameEvent
36 = MoveLeft
37 | MoveRight
38 | StopLeft
39 | StopRight
40 deriving (Eq, Ord)
41
42-- Game objects
43
44data GameObject = GameObject
45 { aabb :: AABB2,
46 obj :: Obj2,
47 gostep :: Step [GameObject] [GameEvent] GameObject GameObject
48 }
49
50instance Spatial2 GameObject where
51 getObj2 = obj
52 setObj2 s o = s {obj = o}
53
54stepWorld :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> [GameObject]
55stepWorld elapsed dt evts gos = map (update elapsed dt evts gos) gos
56
57update :: Elapsed -> Dt -> [GameEvent] -> [GameObject] -> GameObject -> GameObject
58update elapsed dt evts gos go =
59 let (go', s') = runStep (gostep go) elapsed dt gos evts go
60 in go' {gostep = s'}
61
62ballBox, padBox :: AABB2
63ballBox = AABB2 (vec2 (- s) (- s)) (vec2 s s) where s = ballSize
64padBox = AABB2 (- padSize) padSize
65
66obj2 = obj2FromVectors unitx2 unity2
67
68newWorld =
69 [ GameObject ballBox (obj2 initialBallPos) $ stepBall ballVelocity,
70 GameObject padBox (obj2 initialEnemyPos) stepEnemy,
71 GameObject padBox (obj2 initialPlayerPos) stepPlayer
72 ]
73
74-- Ball steppers
75
76stepBall vel = collideBall vel .> moveBall
77
78collideBall :: Vector2 -> Step [GameObject] e GameObject (Vector2, GameObject)
79collideBall vel = step $ \_ dt gos _ ball ->
80 let (AABB2 pmin pmax) = aabb ball `aabbAdd` pos ball
81 collideCol = x pmin < 0 || x pmax > 1
82 collideRow = y pmin < 0 || y pmax > 1 || any (collide ball) (tail gos)
83 negx v@(Vector2 x y) = if collideCol then vec2 (- x) y else v
84 negy v@(Vector2 x y) = if collideRow then vec2 x (- y) else v
85 vel' = negx . negy $ vel
86 delta = dt -- A small delta to apply when collision occurs.
87 adjustX = if collideCol then scale delta (vec2 (x vel) 0) else vec2 0 0
88 adjustY = if collideRow then scale delta (vec2 0 (y vel)) else vec2 0 0
89 in ((vel' + adjustX + adjustY, ball), collideBall vel')
90
91collide go1 go2 =
92 let (AABB2 (Vector2 xmin1 ymin1) (Vector2 xmax1 ymax1)) =
93 aabb go1 `aabbAdd` pos go1
94 (AABB2 (Vector2 xmin2 ymin2) (Vector2 xmax2 ymax2)) =
95 aabb go2 `aabbAdd` pos go2
96 in not $
97 xmax1 < xmin2 || xmin1 > xmax2
98 || ymax1 < ymin2
99 || ymin1 > ymax2
100
101aabbAdd (AABB2 pmin pmax) p = AABB2 (p + pmin) (p + pmax)
102
103moveBall :: Step s e (Vector2, GameObject) GameObject
104moveBall = step $ \_ dt _ _ (vel, ball) -> (move (scale dt vel) ball, moveBall)
105
106-- Enemy stepper
107
108stepEnemy = movePad
109
110movePad :: Step s e GameObject GameObject
111movePad = step $ \elapsed _ _ _ pad ->
112 let p = vec2 px 0.9
113 px =
114 double2Float (sin elapsed * 0.5 + 0.5)
115 * (1 - 2 * x padSize)
116 + x padSize
117 in (setPos p pad, movePad)
118
119-- Player stepper
120
121stepPlayer = sfold moveGO .> clamp
122
123moveGO =
124 mconcat
125 [ switch StopLeft sid MoveLeft (moveGO' $ vec2 (- playerSpeed) 0),
126 switch StopRight sid MoveRight (moveGO' $ vec2 playerSpeed 0)
127 ]
128
129moveGO' :: Vector2 -> Step s e GameObject GameObject
130moveGO' dir = step $ \_ dt _ _ go -> (move (scale dt dir) go, moveGO' dir)
131
132clamp :: Step s e GameObject GameObject
133clamp = spure $ \go ->
134 let p' = vec2 (clamp' x s (1 - s)) y
135 (Vector2 x y) = pos go
136 clamp' x a b
137 | x < a = a
138 | x > b = b
139 | otherwise = x
140 (Vector2 s _) = padSize
141 in setPos p' go