module Dodge.WorldEvent
    ( module Dodge.WorldEvent
    , module Dodge.WorldEvent.Flash
    , module Dodge.WorldEvent.ThingsHit
    , module Dodge.WorldEvent.Cloud
    , module Dodge.WorldEvent.HitEffect
    , module Dodge.WorldEvent.Explosion
    , module Dodge.WorldEvent.SpawnParticle
    )
    where
import Dodge.Particle.Bullet.Draw
import Dodge.Particle.Bullet.Update
import Dodge.WorldEvent.Flash
import Dodge.WorldEvent.ThingsHit
import Dodge.WorldEvent.Cloud
import Dodge.WorldEvent.HitEffect
import Dodge.WorldEvent.Explosion
import Dodge.WorldEvent.SpawnParticle
import Dodge.LightSource
import Dodge.Creature.State.Data
import Dodge.Data
import Geometry
import Color

import Control.Lens
import System.Random
import qualified Data.IntMap.Strict as IM

createBarrelSpark :: Point2 -> Float -> Maybe Int -> Int -> Int -> World -> World
createBarrelSpark pos dir maycid time colid w 
    = over worldEvents (( over particles (spark :) . sparkFlashAt pos') . ) w
  where 
    spark = BulletPt  
        { _ptDraw = drawBul
        , _ptUpdate = mvBullet
        , _btVel' = rotateV dir (V2 5 0)
        , _btColor' = numColor colid
        , _btTrail' = [pos]
        , _btPassThrough' = maycid
        , _btWidth' = 1
        , _btTimer' = time
        , _btHitEffect' = destroyOnImpact sparkEff noEff
        }
    x = fst $ randomR (0,20) $ _randGen w
    pos' = pos +.+ rotateV dir (V2 x 0)
    sparkEff bt p cr = over (creatures . ix (_crID cr) . crState . crDamage)
                            ((:) $ SparkDam 1 sp p ep)
      where sp = head (_btTrail' bt)
            ep = sp +.+ _btVel' bt

damCrsOnLine :: Int -> Point2 -> Point2 -> World -> World
damCrsOnLine dam p1 p2 = over creatures (IM.map damIfOnLine)
    where damIfOnLine cr | circOnSeg p1 p2 (_crPos cr) (_crRad cr) 
                                     = over crHP (\hp -> hp - dam) cr
                         | otherwise = cr

makeTLight :: Int -> Float -> Point3 -> Point3 -> World -> World
makeTLight i rad col p = tempLightSources %~ (tLight i rad col p :)
