edge-0.8.16: Projectile/BulletSI.hs
module Projectile.BulletSI ( BulletSI(..)
, new
) where
import Combat
import Animation
import Updating
import Graphics.Gloss.Data.Picture
import Graphics.Gloss.Data.Color
import Data.WrapAround
import qualified Moving as M
import Common
velocityC = 400.0
rangeC = 500.0
data BulletSI =
BulletSI { velocity :: Velocity
, center :: WrapPoint
, rangeLeft :: Double
, wrapMap :: WrapMap
, idealNewCenter :: Maybe WrapPoint
, impacted :: Bool
, clock :: Time
}
-- angle is radians
new :: WrapMap -> Angle -> WrapPoint -> Velocity -> BulletSI
new wmap angle center' (vpx, vpy) =
let x = cos angle * velocityC in
let y = sin angle * velocityC in
BulletSI { velocity = (x + vpx, y + vpy)
, center = center'
, rangeLeft = rangeC
, wrapMap = wmap
, idealNewCenter = Nothing
, impacted = False
, clock = 0.0
}
instance Animation BulletSI where
image self t = let r = fromInteger (ceiling (clock self)) - clock self in
Color (c r)
(Rotate 45.0
(Circle 2.0))
where c x | x < 0.10 = red
| x < 0.20 = yellow
| x < 0.30 = red
| x < 0.40 = yellow
| x < 0.50 = red
| x < 0.60 = yellow
| x < 0.70 = red
| x < 0.80 = yellow
| x < 0.90 = red
| otherwise = yellow
instance M.Colliding BulletSI where
collisionRadius b = 1.0
instance M.Moving BulletSI where
velocity b = Projectile.BulletSI.velocity b
instance M.Locatable BulletSI where
center b = Projectile.BulletSI.center b
instance SimpleTransient BulletSI where
expired b = rangeLeft b <= 0.0
instance InternallyUpdating BulletSI where
preUpdate self t = let s' = updateIdealTargetCenter t self in
s' { clock = clock self + t }
postUpdate self t =
let center' = case idealNewCenter self of
Nothing -> center self
Just x -> x in
self { center = center'
, idealNewCenter = Nothing
}
updateIdealTargetCenter :: Time -> BulletSI -> BulletSI
updateIdealTargetCenter t self =
let newLoc = M.newLocation (wrapMap self)
(center self)
(velocity self)
t in
self { idealNewCenter = Just (newLoc)
, rangeLeft = max 0.0 $ rangeLeft self - distance (wrapMap self)
(center self)
(newLoc)
}
instance Damaging BulletSI where
damageEnergy b = 1.0
instance Transient BulletSI where
expired' self = if impacted self || rangeLeft self <= 0.0
then Just []
else Nothing
instance Damageable BulletSI where
inflictDamage self d = if d > 0 then self { impacted = True }
else self