From 87065dc4abe2e987da18e6735b00ef88a940372a Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 20 Aug 2024 19:33:43 -0700 Subject: Basic enemy AI. --- Demos/Pong/Pong.hs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'Demos/Pong/Pong.hs') 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) padSize = vec2 0.07 0.02 ballSize = 0.012 :: Float -ballSpeed = 0.6 :: Float +ballSpeed = 0.7 :: Float initialBallVelocity = vec2 1 1 maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) playerSpeed = 1.0 :: Float -enemySpeed = 3.0 :: Float +enemySpeed = 7.0 :: Float +enemyMomentum = 0.1 :: Float initialEnemyPos = vec2 0.5 0.9 initialPlayerPos = vec2 0.5 0.1 initialBallPos = vec2 0.5 0.5 @@ -146,17 +147,19 @@ moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall) -- Enemy stepper -stepEnemy = movePad - -movePad :: Step s e GameObject GameObject -movePad = step $ \elapsed _ _ _ pad -> - let enemyY = 0.9 - p = vec2 px enemyY - px = - (sin (enemySpeed * elapsed) * (0.5::Float) + (0.5::Float)) - * ((1::Float) - (2::Float) * x padSize) - + x padSize - in (setPosition p pad, movePad) +stepEnemy = movePad 0 .> clamp + +movePad :: Float -> Step [GameObject] e GameObject GameObject +movePad previousMomentum = step $ \_ dt gos _ pad -> + let ball = head gos + offset = (x . position $ ball) - (x . position $ pad) + chaseVector = enemySpeed * offset + momentum = previousMomentum + enemyMomentum * chaseVector + vx = chaseVector + momentum + in (translate (vec2 (vx * dt) 0) pad, movePad momentum) + +sign :: Float -> Float +sign x = if x >= 0 then 1 else -1 -- Player stepper -- cgit v1.2.3