summaryrefslogtreecommitdiffstats
path: root/src/Simulation.hs
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-06-26 23:41:53 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-06-26 23:41:53 +0200
commitcfa9cf94568fbe1564e5b4e386a8170910bc552e (patch)
tree4775bfc3d4436431af43fc9fd45792f5cbf1f0b6 /src/Simulation.hs
parentae4a69415050b61f4488255dad0b1c3d045e6de5 (diff)
downloadhtanks-master.tar
htanks-master.zip
Refactored to/fromVector functionsHEADmaster
Diffstat (limited to 'src/Simulation.hs')
-rw-r--r--src/Simulation.hs22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/Simulation.hs b/src/Simulation.hs
index ca335f2..2d5a0fa 100644
--- a/src/Simulation.hs
+++ b/src/Simulation.hs
@@ -48,8 +48,8 @@ approx :: Rotation -> Rotation -> Bool
approx r1 r2 = c1 `approx'` c2 && s1 `approx'` s2
where
approx' a b = (abs (a-b)) < 0.000001
- Vector c1 s1 = toVector r1
- Vector c2 s2 = toVector r2
+ Vector c1 s1 = toVector 1 r1
+ Vector c2 s2 = toVector 1 r2
updateTank :: GameState -> Maybe Rotation -> Bool -> Maybe Rotation -> State Tank ()
updateTank game dir move aim = do
@@ -57,7 +57,7 @@ updateTank game dir move aim = do
modify $ updateAngle $ fromJust dir
when (isJust aim) $
- modify $ \tank -> tank {tankAim = fromJust aim}
+ modify $ \tank -> tank { tankAim = fromJust aim }
when move $ do
tank <- get
@@ -66,10 +66,10 @@ updateTank game dir move aim = do
moved = tankMoving tank
when (isNothing dir || (isJust dir && (tdir `approx` fromJust dir) || moved)) $
- put $ ((toVector tdir) ^* (tspeed/100)) >< tank {tankMoving = True}
+ put $ toVector (tspeed/100) tdir >< tank {tankMoving = True}
when (not move) $ do
- modify $ \tank -> tank {tankMoving = False}
+ modify $ \tank -> tank {tankMoving = False}
let lw = fromIntegral . levelWidth . level $ game
lh = fromIntegral . levelHeight . level $ game
@@ -77,17 +77,19 @@ updateTank game dir move aim = do
updateBullet :: GameState -> Bullet -> (Bullet, Bool)
-updateBullet game bullet = (bullet {bulletPos = Vertex x' y', bulletDir = fromVector $ Vector dx' dy', bulletBouncesLeft = bounces3}, bounces3 >= 0)
+updateBullet game bullet = (bullet {bulletPos = Vertex x' y', bulletDir = dir'', bulletBouncesLeft = bounces3}, bounces3 >= 0)
where
+ rot180 = fromAngle pi
+
speed = bulletSpeed bullet
- d@(Vector dx dy) = toVector $ bulletDir bullet
- Vertex x y = (d ^* (speed/100)) >< bulletPos bullet
+ dir = bulletDir bullet
+ Vertex x y = toVector (speed/100) dir >< bulletPos bullet
bounces = bulletBouncesLeft bullet
lw = fromIntegral . levelWidth . level $ game
lh = fromIntegral . levelHeight . level $ game
- (x', dx', bounces2) = if x < 0 then (-x, -dx, bounces-1) else if x > lw then (2*lw-x, -dx, bounces-1) else (x, dx, bounces)
- (y', dy', bounces3) = if y < 0 then (-y, -dy, bounces2-1) else if y > lh then (2*lh-y, -dy, bounces2-1) else (y, dy, bounces2)
+ (x', dir', bounces2) = if x < 0 then (-x, negateV dir, bounces-1) else if x > lw then (2*lw-x, negateV dir, bounces-1) else (x, dir, bounces)
+ (y', dir'', bounces3) = if y < 0 then (-y, rot180 ^-^ dir', bounces2-1) else if y > lh then (2*lh-y, rot180 ^-^ dir', bounces2-1) else (y, dir', bounces2)
gameStep :: [(Tank, Bool)] -> GameState -> GameState
gameStep tanksshoot state = state {tanks = thetanks, bullets = newbullets ++ (map snd . filter fst $ leftbullets2)}