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.hs29
1 files changed, 16 insertions, 13 deletions
diff --git a/Demos/Pong/Pong.hs b/Demos/Pong/Pong.hs
index e834c5d..b05bb24 100644
--- a/Demos/Pong/Pong.hs
+++ b/Demos/Pong/Pong.hs
@@ -26,11 +26,12 @@ import Data.Monoid (mconcat)
26 26
27padSize = vec2 0.07 0.02 27padSize = vec2 0.07 0.02
28ballSize = 0.012 :: Float 28ballSize = 0.012 :: Float
29ballSpeed = 0.6 :: Float 29ballSpeed = 0.7 :: Float
30initialBallVelocity = vec2 1 1 30initialBallVelocity = vec2 1 1
31maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) 31maxBounceAngle = (65::Float) * (pi::Float)/(180::Float)
32playerSpeed = 1.0 :: Float 32playerSpeed = 1.0 :: Float
33enemySpeed = 3.0 :: Float 33enemySpeed = 7.0 :: Float
34enemyMomentum = 0.1 :: Float
34initialEnemyPos = vec2 0.5 0.9 35initialEnemyPos = vec2 0.5 0.9
35initialPlayerPos = vec2 0.5 0.1 36initialPlayerPos = vec2 0.5 0.1
36initialBallPos = vec2 0.5 0.5 37initialBallPos = vec2 0.5 0.5
@@ -146,17 +147,19 @@ moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall)
146 147
147-- Enemy stepper 148-- Enemy stepper
148 149
149stepEnemy = movePad 150stepEnemy = movePad 0 .> clamp
150 151
151movePad :: Step s e GameObject GameObject 152movePad :: Float -> Step [GameObject] e GameObject GameObject
152movePad = step $ \elapsed _ _ _ pad -> 153movePad previousMomentum = step $ \_ dt gos _ pad ->
153 let enemyY = 0.9 154 let ball = head gos
154 p = vec2 px enemyY 155 offset = (x . position $ ball) - (x . position $ pad)
155 px = 156 chaseVector = enemySpeed * offset
156 (sin (enemySpeed * elapsed) * (0.5::Float) + (0.5::Float)) 157 momentum = previousMomentum + enemyMomentum * chaseVector
157 * ((1::Float) - (2::Float) * x padSize) 158 vx = chaseVector + momentum
158 + x padSize 159 in (translate (vec2 (vx * dt) 0) pad, movePad momentum)
159 in (setPosition p pad, movePad) 160
161sign :: Float -> Float
162sign x = if x >= 0 then 1 else -1
160 163
161-- Player stepper 164-- Player stepper
162 165