module Dodge.Particle.Spark
    ( colSpark
    , colSpark'
    )
    where
import Dodge.Data
import Dodge.Particle.Bullet.Draw
import Dodge.Particle.Bullet.Update
import Dodge.WorldEvent.HitEffect
import Color
import Geometry

import System.Random
import Control.Lens

colSpark :: Int -> Color -> Point2 -> Float -> World -> World
colSpark = colSpark' 0.7
colSpark' :: Float -> Int -> Color -> Point2 -> Float -> World -> World
colSpark' randDir time col pos baseDir w = w 
    & worldEvents %~ ( over particles (spark :) . )
    & randGen .~ g
  where 
    (a,g)  = randomR (-randDir,randDir) $ _randGen w
    dir = a + baseDir
    spark = BulletPt
        { _ptDraw = drawBul
        , _ptUpdate = mvBullet
        , _btVel' = rotateV dir (V2 5 0)
        , _btColor' = col
        , _btTrail' = [pos]
        , _btPassThrough' = Nothing
        , _btWidth' = 1
        , _btTimer' = time
        , _btHitEffect' = destroyOnImpact sparkEff noEff
        }
    sparkEff bt p cr = creatures . ix (_crID cr) . crState . crDamage %~ ( SparkDam 1 sp p ep : )
      where 
        sp = head (_btTrail' bt)
        ep = sp +.+ _btVel' bt
