module Dodge.Particle.HitEffect.ExpireAndDamage where
import Dodge.Data
import Dodge.Particle.Update
--import Dodge.Wall.Damage
import Geometry
import LensHelp
import qualified Data.IntMap.Strict as IM
import Streaming
import qualified Streaming.Prelude as S

expireAndDamage :: (Particle -> Point2 -> [Damage])
    -> Particle
    -> Stream (Of (Point2, Either Creature Wall)) Identity ()
    -> World
    -> (World, Maybe Particle)
expireAndDamage fdm bt things w = case runIdentity $ S.head_ things of
    Nothing -> (w, mvPt bt)
    Just x -> (doDamages fdm x bt w, destroyAt (fst x) bt)

doDamages :: (Particle -> Point2 -> [Damage])
    -> (Point2, Either Creature Wall)
    -> Particle
    -> World
    -> World
doDamages fdm (p,thhit) bt = case thhit of
    Left cr -> creatures . ix (_crID cr) . crState . csDamage .++~ dams
    Right wl -> wallDamages %~ IM.insertWith (++) (_wlID wl) dams
  where
    dams = fdm bt p
