edge-0.8.18: Projectile/BulletSI.hs
module Projectile.BulletSI ( BulletSI(..)
, new
, prj
) where
import Combat ( Projectile(..), Damaging(..), Damageable(..) )
import Animation ( Animation(..) )
import Updating
( Transient(..), SimpleTransient(..), InternallyUpdating(..) )
import Graphics.Gloss.Data.Picture ( Picture(Circle, Color) )
import Graphics.Gloss.Data.Color ( yellow, red )
import Data.WrapAround ( WP, WM, distance )
import qualified Moving as M
( Moving(..), Locatable(..), Colliding(..), newLocation )
import Common ( Velocity, Time, Angle )
import Math ( appPair, remF, zeroOrLess, moreThanZero )
velocityC = 400.0
rangeC = 500.0
damE = 1
data BulletSI =
BulletSI { velocity :: Velocity
, center :: WP
, rangeLeft :: Double
, wrapMap :: WM
, idealNewCenter :: Maybe WP
, impacted :: Bool
, clock :: Time
}
new a b c (d, e) =
BulletSI { velocity = (x + d, y + e)
, center = c
, rangeLeft = rangeC
, wrapMap = a
, idealNewCenter = Nothing
, impacted = False
, clock = 0.0
}
where (x, y) = appPair ((* velocityC) . ($ b)) (cos, sin)
instance Animation BulletSI where
image s t = (Color (c r) . Circle) 2.0
where c x | x < 0.10 = red
| otherwise = yellow
r = remF (clock s) 0.2
instance M.Colliding BulletSI where
collisionRadius _ = 2.0
instance M.Moving BulletSI where
velocity = velocity
instance M.Locatable BulletSI where
center = center
instance SimpleTransient BulletSI where
expired = zeroOrLess . rangeLeft
instance InternallyUpdating BulletSI where
preUpdate s t = s { clock = clock s + t }
postUpdate s t = s { center = a, rangeLeft = r }
where a = M.newLocation (wrapMap s) (center s) (velocity s) t
r = max 0 (rangeLeft s - distance (wrapMap s) (center s) a)
instance Damaging BulletSI where
damageEnergy _ = damE
instance Transient BulletSI where
expired' s = if impacted s || zeroOrLess (rangeLeft s) then Just [] else Nothing
instance Damageable BulletSI where
inflictDamage s d = if moreThanZero d then s { impacted = True } else s
-- | Func abstracting construction of 'BulletSI' projectile
prj :: (M.Moving a)
=> a -- ^ object receiving projectile
-> (a -> WM) -- ^ func which retrieves WM from object
-> Angle -- ^ firing angle
-> Projectile
prj a f b = Projectile (new (f a) b (M.center a) (M.velocity a))