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 = 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) 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) 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) ax = aimx - (fromRational . toRational . tankX $ tank)
ay = aimy - (fromRational . toRational $ posy tank) ay = aimy - (fromRational . toRational . tankY $ tank)
move = (x /= 0 || y /= 0) move = (x /= 0 || y /= 0)
angle = if move then Just $ fromRational $ round ((atan2 y x)*1000000*180/pi)%1000000 else Nothing 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 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 data Tank = Tank
{ posx :: !Micro { tankX :: !Micro
, posy :: !Micro , tankY :: !Micro
, dir :: !Micro , tankDir :: !Micro
, aim :: !Micro , tankAim :: !Micro
, speed :: !Micro , tankSpeed :: !Micro
, turnspeed :: !Micro , tankTurnspeed :: !Micro
, moving :: !Bool , tankMoving :: !Bool
, tankShootSpeed :: !Micro , tankShootSpeed :: !Micro
, tankShootBounces :: !Int , tankShootBounces :: !Int
, shootsLeft :: !Int , tankShootsLeft :: !Int
} deriving Show } deriving Show
data Shoot = Shoot data Shoot = Shoot
@ -34,7 +34,7 @@ data Shoot = Shoot
, shootY :: !Micro , shootY :: !Micro
, shootDir :: !Micro , shootDir :: !Micro
, shootSpeed :: !Micro , shootSpeed :: !Micro
, bouncesLeft :: !Int , shootBouncesLeft :: !Int
, shootTank :: !Int , shootTank :: !Int
} deriving Show } deriving Show

View file

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

View file

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