module Projectile.BulletMkI ( BulletMkI(..) , 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 = 700.0 rangeC = 1000.0 data BulletMkI = BulletMkI { velocity :: Velocity , center :: WrapPoint , rangeLeft :: Double , wrapMap :: WrapMap , idealNewCenter :: Maybe WrapPoint , impacted :: Bool , clock :: Time } -- angle is radians new :: WrapMap -> Angle -> WrapPoint -> Velocity -> BulletMkI new wmap angle center' (vpx, vpy) = let x = cos angle * velocityC in let y = sin angle * velocityC in BulletMkI { velocity = (x + vpx, y + vpy) , center = center' , rangeLeft = rangeC , wrapMap = wmap , idealNewCenter = Nothing , impacted = False , clock = 0.0 } instance Animation BulletMkI 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 = blue | x < 0.20 = cyan | x < 0.30 = blue | x < 0.40 = cyan | x < 0.50 = blue | x < 0.60 = cyan | x < 0.70 = blue | x < 0.80 = cyan | x < 0.90 = blue | otherwise = cyan instance M.Colliding BulletMkI where collisionRadius b = 1.0 instance M.Moving BulletMkI where velocity b = Projectile.BulletMkI.velocity b instance M.Locatable BulletMkI where center b = Projectile.BulletMkI.center b instance SimpleTransient BulletMkI where expired b = rangeLeft b <= 0.0 instance InternallyUpdating BulletMkI 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 -> BulletMkI -> BulletMkI updateIdealTargetCenter t self = let newLoc = M.idealNewLocation (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 BulletMkI where damageEnergy b = 1.0 instance Transient BulletMkI where expired' self = if impacted self || rangeLeft self <= 0.0 then Just [] else Nothing instance Damageable BulletMkI where inflictDamage self d = if d > 0 then self { impacted = True } else self