diff options
Diffstat (limited to 'Demos')
-rw-r--r-- | Demos/Pong/Pong.hs | 29 |
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 | ||
27 | padSize = vec2 0.07 0.02 | 27 | padSize = vec2 0.07 0.02 |
28 | ballSize = 0.012 :: Float | 28 | ballSize = 0.012 :: Float |
29 | ballSpeed = 0.6 :: 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) |
32 | playerSpeed = 1.0 :: Float | 32 | playerSpeed = 1.0 :: Float |
33 | enemySpeed = 3.0 :: Float | 33 | enemySpeed = 7.0 :: Float |
34 | enemyMomentum = 0.1 :: Float | ||
34 | initialEnemyPos = vec2 0.5 0.9 | 35 | initialEnemyPos = vec2 0.5 0.9 |
35 | initialPlayerPos = vec2 0.5 0.1 | 36 | initialPlayerPos = vec2 0.5 0.1 |
36 | initialBallPos = vec2 0.5 0.5 | 37 | initialBallPos = 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 | ||
149 | stepEnemy = movePad | 150 | stepEnemy = movePad 0 .> clamp |
150 | 151 | ||
151 | movePad :: Step s e GameObject GameObject | 152 | movePad :: Float -> Step [GameObject] e GameObject GameObject |
152 | movePad = step $ \elapsed _ _ _ pad -> | 153 | movePad 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 | |
161 | sign :: Float -> Float | ||
162 | sign x = if x >= 0 then 1 else -1 | ||
160 | 163 | ||
161 | -- Player stepper | 164 | -- Player stepper |
162 | 165 | ||