summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs16
-rw-r--r--src/HTanks.hs39
2 files changed, 32 insertions, 23 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 9aef422..248287b 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -17,13 +17,13 @@ import qualified Data.Map as M
data Tank = Tank
- { tankX :: !Micro
- , tankY :: !Micro
- , tankDir :: !Micro
- , tankAim :: !Micro
- , tankSpeed :: !Micro
- , tankTurnspeed :: !Micro
- , tankMoving :: !Bool
+ { tankX :: !Micro
+ , tankY :: !Micro
+ , tankDir :: !Micro
+ , tankAim :: !Micro
+ , tankSpeed :: !Micro
+ , tankTurnspeed :: !Micro
+ , tankMoving :: !Bool
, tankBulletSpeed :: !Micro
, tankBulletBounces :: !Int
, tankBulletsLeft :: !Int
@@ -41,7 +41,7 @@ data Bullet = Bullet
data GameState = GameState
{ level :: !Level
, tanks :: ![Tank]
- , bullets :: ![Bullet]
+ , bullets :: ![Bullet]
, textures :: !(M.Map Texture TextureObject)
} deriving (Show)
diff --git a/src/HTanks.hs b/src/HTanks.hs
index 4557809..4b66944 100644
--- a/src/HTanks.hs
+++ b/src/HTanks.hs
@@ -147,14 +147,23 @@ updateTank angle move aangle = do
modify $ \tank -> tank {tankMoving = False}
-updateBullet :: State Bullet ()
-updateBullet = do
- angle <- gets bulletDir >>= return . (/180) . (*pi) . fromRational . toRational
- speed <- gets bulletSpeed
- let dx = speed * fromRational (round ((cos angle)*1000)%1000000)
+updateBullet :: GameState -> State Bullet ()
+updateBullet game = do
+ bullet <- get
+ let angle = (fromRational . toRational . bulletDir $ bullet)*pi/180
+ speed = bulletSpeed bullet
+ dx = speed * fromRational (round ((cos angle)*1000)%1000000)
dy = speed * fromRational (round ((sin angle)*1000)%1000000)
-
- modify $ \bullet -> bullet {bulletX = dx + bulletX bullet, bulletY = dy + bulletY bullet}
+ x = dx + bulletX bullet
+ y = dy + bulletY bullet
+ lw = fromIntegral . levelWidth . level $ game
+ lh = fromIntegral . levelHeight . level $ game
+ dir = bulletDir bullet
+
+ (newx, dir2) = if x < 0 then (-x, (signum dir)*180 - dir) else if x > lw then (2*lw-x, (signum dir)*180 - dir) else (x, dir)
+ (newy, dir3) = if y < 0 then (-y, -dir2) else if y > lh then (2*lh-y, -dir2) else (y, dir2)
+
+ put bullet {bulletX = newx, bulletY = newy, bulletDir = dir3}
simulationStep :: Main ()
@@ -167,17 +176,17 @@ simulationStep = do
bulletingtanks = map (\(tank, _, n) -> (tank, n)) $ filter (\(tank, bullet, _) -> bullet && (tankBulletsLeft tank) > 0) $ ts
newtanks = map (\(tank, bullet, _) -> if bullet then tank {tankBulletsLeft = (tankBulletsLeft tank) - 1} else tank) $ ts
newbullets = map (\(tank, n) -> Bullet
- { bulletX = tankX tank
- , bulletY = tankY tank
- , bulletDir = tankAim tank
- , bulletSpeed = tankBulletSpeed tank
- , bulletBouncesLeft = tankBulletBounces tank
- , bulletTank = n
- }) bulletingtanks
+ { bulletX = tankX tank
+ , bulletY = tankY tank
+ , bulletDir = tankAim tank
+ , bulletSpeed = tankBulletSpeed tank
+ , bulletBouncesLeft = tankBulletBounces tank
+ , bulletTank = n
+ }) bulletingtanks
modify $ \state -> state {players = p}
- lift $ modify $ \state -> state {tanks = newtanks, bullets = map (execState updateBullet) (newbullets ++ bullets state)}
+ lift $ modify $ \state -> state {tanks = newtanks, bullets = map (execState $ updateBullet state) (newbullets ++ bullets state)}
where
updateTank' (player, tank) = let (p, angle, move, aangle, bullet) = playerUpdate player tank
t = execState (updateTank angle move aangle) tank