Renamed some Tank and Shoot accessors

This commit is contained in:
Matthias Schiffer 2010-03-09 03:19:34 +01:00
parent aa00912206
commit 2bb8561836
4 changed files with 37 additions and 35 deletions

View file

@ -21,8 +21,8 @@ instance Player DefaultPlayer where
playerUpdate (DefaultPlayer keys aimx aimy shoot) tank =
let x = (if (S.member KeyLeft keys) then (-1) else 0) + (if (S.member KeyRight keys) then 1 else 0)
y = (if (S.member KeyDown keys) then (-1) else 0) + (if (S.member KeyUp keys) then 1 else 0)
ax = aimx - (fromRational . toRational $ posx tank)
ay = aimy - (fromRational . toRational $ posy tank)
ax = aimx - (fromRational . toRational . tankX $ tank)
ay = aimy - (fromRational . toRational . tankY $ tank)
move = (x /= 0 || y /= 0)
angle = if move then Just $ fromRational $ round ((atan2 y x)*1000000*180/pi)%1000000 else Nothing
aangle = if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing

18
Game.hs
View file

@ -17,16 +17,16 @@ import qualified Data.Map as M
data Tank = Tank
{ posx :: !Micro
, posy :: !Micro
, dir :: !Micro
, aim :: !Micro
, speed :: !Micro
, turnspeed :: !Micro
, moving :: !Bool
{ tankX :: !Micro
, tankY :: !Micro
, tankDir :: !Micro
, tankAim :: !Micro
, tankSpeed :: !Micro
, tankTurnspeed :: !Micro
, tankMoving :: !Bool
, tankShootSpeed :: !Micro
, tankShootBounces :: !Int
, shootsLeft :: !Int
, tankShootsLeft :: !Int
} deriving Show
data Shoot = Shoot
@ -34,7 +34,7 @@ data Shoot = Shoot
, shootY :: !Micro
, shootDir :: !Micro
, shootSpeed :: !Micro
, bouncesLeft :: !Int
, shootBouncesLeft :: !Int
, shootTank :: !Int
} deriving Show

View file

@ -90,8 +90,8 @@ mainLoop = do
updateAngle :: Micro -> State Tank ()
updateAngle angle = do
oldangle <- gets dir
tspeed <- gets turnspeed >>= return . (/1000)
oldangle <- gets tankDir
tspeed <- gets tankTurnspeed >>= return . (/1000)
let diff = angle - oldangle
let diff360 = if (diff > 180)
@ -120,7 +120,7 @@ updateAngle angle = do
then (newangle+360)
else newangle
modify $ \tank -> tank {dir = newangle180}
modify $ \tank -> tank {tankDir = newangle180}
updateTank :: Maybe Micro -> Bool -> Maybe Micro -> State Tank ()
@ -129,30 +129,32 @@ updateTank angle move aangle = do
updateAngle $ fromJust angle
when (isJust aangle) $
modify $ \tank -> tank {aim = fromJust aangle}
modify $ \tank -> tank {tankAim = fromJust aangle}
when move $ do
tdir <- gets dir
tspeed <- gets speed
moved <- gets moving
tdir <- gets tankDir
tspeed <- gets tankSpeed
moved <- gets tankMoving
when (isNothing angle || (isJust angle && (tdir == fromJust angle)) || moved) $ do
let anglej = (fromRational . toRational $ tdir)*pi/180
x = tspeed * fromRational (round ((cos anglej)*1000)%1000000)
y = tspeed * fromRational (round ((sin anglej)*1000)%1000000)
modify $ \tank -> tank {posx = x + posx tank, posy = y + posy tank, moving = True}
modify $ \tank -> tank {tankX = x + tankX tank, tankY = y + tankY tank, tankMoving = True}
when (not move) $ do
modify $ \tank -> tank {moving = False}
modify $ \tank -> tank {tankMoving = False}
updateShoot :: State Shoot ()
updateShoot = modify $ \shoot ->
let angle = (fromRational . toRational . shootDir $ shoot)*pi/180
dx = (shootSpeed shoot) * fromRational (round ((cos angle)*1000)%1000000)
dy = (shootSpeed shoot) * fromRational (round ((sin angle)*1000)%1000000)
in shoot {shootX = dx + shootX shoot, shootY = dy + shootY shoot}
updateShoot = do
angle <- gets shootDir >>= return . (/180) . (*pi) . fromRational . toRational
speed <- gets shootSpeed
let dx = speed * fromRational (round ((cos angle)*1000)%1000000)
dy = speed * fromRational (round ((sin angle)*1000)%1000000)
modify $ \shoot -> shoot {shootX = dx + shootX shoot, shootY = dy + shootY shoot}
simulationStep :: Main ()
@ -162,14 +164,14 @@ simulationStep = do
let (p, t, s) = unzip3 $ map updateTank' $ zip oldplayers oldtanks
ts = zip3 t s [0..]
shootingtanks = map (\(tank, _, n) -> (tank, n)) $ filter (\(tank, shoot, _) -> shoot && (shootsLeft tank) > 0) $ ts
newtanks = map (\(tank, shoot, _) -> if shoot then tank {shootsLeft = (shootsLeft tank) - 1} else tank) $ ts
shootingtanks = map (\(tank, _, n) -> (tank, n)) $ filter (\(tank, shoot, _) -> shoot && (tankShootsLeft tank) > 0) $ ts
newtanks = map (\(tank, shoot, _) -> if shoot then tank {tankShootsLeft = (tankShootsLeft tank) - 1} else tank) $ ts
newshoots = map (\(tank, n) -> Shoot
{ shootX = posx tank
, shootY = posy tank
, shootDir = aim tank
{ shootX = tankX tank
, shootY = tankY tank
, shootDir = tankAim tank
, shootSpeed = tankShootSpeed tank
, bouncesLeft = tankShootBounces tank
, shootBouncesLeft = tankShootBounces tank
, shootTank = n
}) shootingtanks

View file

@ -99,10 +99,10 @@ render = do
vertex $ Vertex2 (0 :: GLfloat) (0 :: GLfloat)
forM_ tanklist $ \tank -> preservingMatrix $ do
let x = fromReal . posx $ tank
y = fromReal . posy $ tank
rotDir = fromReal . dir $ tank
rotAim = fromReal . aim $ tank
let x = fromReal . tankX $ tank
y = fromReal . tankY $ tank
rotDir = fromReal . tankDir $ tank
rotAim = fromReal . tankAim $ tank
translate $ Vector3 x y (0 :: GLfloat)
rotate rotDir $ Vector3 0 0 (1 :: GLfloat)