module Dodge.Spark ( moveSpark , colSpark , colSparkRandDir , createBarrelSpark , randColDirSpark , randSpark ) where import Dodge.Data import Dodge.WorldEvent.ThingsHit import Geometry import LensHelp import Color import qualified IntMapHelp as IM import Control.Monad.State import System.Random moveSpark :: World -> Spark -> (World, Maybe Spark) moveSpark w sk | magV (_skVel sk) < 1 = (w,Nothing) | otherwise = case thingHit sp ep w of Nothing -> (w, Just $ sk & skPos .~ ep & skVel .*.*~ 0.9 & skOldPos .~ sp) Just (hp,hthing) -> (sparkDam sk sp ep (hp,hthing) w , Just $ sk & skPos .~ hp & skOldPos .~ sp & skVel .~ 0) where sp = _skPos sk ep = sp +.+ _skVel sk sparkDam :: Spark -> Point2 -> Point2 -> (Point2, Either Creature Wall) -> World -> World sparkDam sk sp ep mayEiCrWl = case mayEiCrWl of (hitp,Left cr) -> creatures . ix (_crID cr) . crState . csDamage .:~ thedam hitp (hitp,Right wl) -> wallDamages %~ IM.insertWith (++) (_wlID wl) [thedam hitp] where thedam hitp = Damage (_skDamageType sk) 1 sp hitp ep NoDamageEffect createBarrelSpark :: Point2 -> Float -> Int -> World -> World createBarrelSpark pos dir colid = colSparkRandDir 0.1 (numColor colid) pos dir colSpark :: Color -> Point2 -> Float -> World -> World colSpark = colSparkRandDir 0.7 randColDirSpark :: State StdGen Color -> State StdGen Float -> Point2 -> World -> World randColDirSpark randcol randdir pos w = w & sparks .:~ spark & randGen .~ g where ((col,dir),g) = (`runState` _randGen w) $ do c <- randcol d <- randdir return (c,d) spark = Spark { _skVel = rotateV dir (V2 5 0) , _skColor = col , _skPos = pos , _skOldPos = pos , _skWidth = 1 , _skDamageType = SPARKING } randSpark :: DamageType -> State StdGen Float -> State StdGen Color -> State StdGen Float -> Point2 -> World -> World randSpark dt randspeed randcol randdir pos w = w & randGen .~ g & sparks .:~ Spark { _skVel = rotateV dir (V2 speed 0) , _skColor = col , _skPos = pos , _skOldPos = pos , _skWidth = 1 , _skDamageType = dt } where ((col,dir,speed),g) = (`runState` _randGen w) $ do c <- randcol d <- randdir x <- randspeed return (c,d,x) colSparkRandDir :: Float -> Color -> Point2 -> Float -> World -> World colSparkRandDir a col pos dir = randColDirSpark (return col) (state $ randomR (dir - a, dir + a)) pos