summaryrefslogtreecommitdiffstats
path: root/src/Simulation.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Simulation.hs')
-rw-r--r--src/Simulation.hs20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/Simulation.hs b/src/Simulation.hs
index 65dc668..a1c193f 100644
--- a/src/Simulation.hs
+++ b/src/Simulation.hs
@@ -121,12 +121,22 @@ simulationStep = do
modify $ \state -> state {players = p}
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}
+ let thebullets = map (runState $ updateBullet state) $ bullets state
+ leftbullets = collideBullets $ zipWith (\(left, bullet') bullet -> (left, bullet, bullet')) thebullets $ bullets state
+ thetanks = map (\(tank, n) -> tank {tankBulletsLeft = (tankBulletsLeft tank) + (countLostTankBullets n leftbullets)}) $ zip newtanks [0..]
+ in state {tanks = thetanks, bullets = newbullets ++ (map snd . filter fst $ leftbullets)}
+
where
- updateTank' game (player, tank) = let (p, angle, move, aangle, bullet) = playerUpdate player tank
+ collideBullets [] = []
+ collideBullets ((left, bullet, bullet'):bs) = let (c, ls) = collideBullet bullet bullet' bs
+ in (left && not c, bullet'):(collideBullets ls)
+ collideBullet bullet bullet' bs = let cs = map (\(left, b, b') -> (left, collisionBulletBullet (bullet, bullet') (b, b'), b, b')) bs
+ collided = any (\(_,c,_,_) -> c) cs
+ left = map (\(left, c, b, b') -> (left && not c, b, b')) $ cs
+ in (collided, left)
+
+ updateTank' game (player, tank) = let (p, angle, move, aangle, shoot) = playerUpdate player tank
t = execState (updateTank game angle move aangle) tank
- in (p, t, bullet)
+ in (p, t, shoot)
countLostTankBullets n (x:xs) = (if ((not . fst $ x) && (n == (bulletTank . snd $ x))) then 1 else 0) + (countLostTankBullets n xs)
countLostTankBullets n [] = 0