summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-03-09 07:30:42 +0100
committerMatthias Schiffer <matthias@gamezock.de>2010-03-09 07:30:42 +0100
commit9d34024718835132b45c586fc97d75839badf355 (patch)
tree31721a7e67ba0378212ce777f2e67de90a4e27e1 /src
parent4993b2ef07ad22f299c60eeec1d96f94cfe4a47c (diff)
downloadhtanks-9d34024718835132b45c586fc97d75839badf355.tar
htanks-9d34024718835132b45c586fc97d75839badf355.zip
Raise number of left bullets after a bullet exploded
Diffstat (limited to 'src')
-rw-r--r--src/HTanks.hs25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/HTanks.hs b/src/HTanks.hs
index 4b66944..88ac4e4 100644
--- a/src/HTanks.hs
+++ b/src/HTanks.hs
@@ -147,7 +147,7 @@ updateTank angle move aangle = do
modify $ \tank -> tank {tankMoving = False}
-updateBullet :: GameState -> State Bullet ()
+updateBullet :: GameState -> State Bullet Bool
updateBullet game = do
bullet <- get
let angle = (fromRational . toRational . bulletDir $ bullet)*pi/180
@@ -159,11 +159,14 @@ updateBullet game = do
lw = fromIntegral . levelWidth . level $ game
lh = fromIntegral . levelHeight . level $ game
dir = bulletDir bullet
+ bounces = bulletBouncesLeft 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)
+ (newx, dir2, bounces2) = if x < 0 then (-x, (signum dir)*180 - dir, bounces-1) else if x > lw then (2*lw-x, (signum dir)*180 - dir, bounces-1) else (x, dir, bounces)
+ (newy, dir3, bounces3) = if y < 0 then (-y, -dir2, bounces2-1) else if y > lh then (2*lh-y, -dir2, bounces2-1) else (y, dir2, bounces2)
- put bullet {bulletX = newx, bulletY = newy, bulletDir = dir3}
+ put bullet {bulletX = newx, bulletY = newy, bulletDir = dir3, bulletBouncesLeft = bounces3}
+
+ return (bounces3 >= 0)
simulationStep :: Main ()
@@ -173,8 +176,8 @@ simulationStep = do
let (p, t, s) = unzip3 $ map updateTank' $ zip oldplayers oldtanks
ts = zip3 t s [0..]
- 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
+ shootingtanks = map (\(tank, _, n) -> (tank, n)) $ filter (\(tank, shoot, _) -> shoot && (tankBulletsLeft tank) > 0) $ ts
+ newtanks = map (\(tank, shoot, _) -> if (shoot && (tankBulletsLeft tank) > 0) then tank {tankBulletsLeft = (tankBulletsLeft tank) - 1} else tank) $ ts
newbullets = map (\(tank, n) -> Bullet
{ bulletX = tankX tank
, bulletY = tankY tank
@@ -182,15 +185,19 @@ simulationStep = do
, bulletSpeed = tankBulletSpeed tank
, bulletBouncesLeft = tankBulletBounces tank
, bulletTank = n
- }) bulletingtanks
-
+ }) shootingtanks
modify $ \state -> state {players = p}
- lift $ modify $ \state -> state {tanks = newtanks, bullets = map (execState $ updateBullet state) (newbullets ++ bullets state)}
+ lift $ modify $ \state ->
+ let thebullets = map (runState $ updateBullet state) $ newbullets ++ bullets state
+ thetanks = map (\(tank, n) -> tank {tankBulletsLeft = (tankBulletsLeft tank) + (countLostTankBullets n thebullets)}) $ zip newtanks [0..]
+ in state {tanks = thetanks, bullets = map snd . filter fst $ thebullets}
where
updateTank' (player, tank) = let (p, angle, move, aangle, bullet) = playerUpdate player tank
t = execState (updateTank angle move aangle) tank
in (p, t, bullet)
+ countLostTankBullets n (x:xs) = (if ((not . fst $ x) && (n == (bulletTank . snd $ x))) then 1 else 0) + (countLostTankBullets n xs)
+ countLostTankBullets n [] = 0
handleEvents :: Main ()