module Dodge.Machine where
import Dodge.Data
import Dodge.Wall.Delete
import Dodge.Machine.Damage
import LensHelp
import Sound.Data
import Dodge.SoundLogic
import Geometry
import Dodge.Base

import qualified Data.IntMap.Strict as IM

basicMachineUpdate :: (Machine -> World -> World) -> Machine -> World -> World
basicMachineUpdate f mc = basicMachineApplyDamage mc . f mc

machineUpdateLiveDieEff 
    :: (Machine -> World -> World)  -- | effect when hp >= 1
    -> (Machine -> World -> World)  -- | effect (once) when hp < 1
    -> Machine -> World -> World
machineUpdateLiveDieEff livef dief mc
    | _mcHP mc < 1 = dief mc 
        . (machines %~ IM.delete mcid)
        . deleteWallIDs (_mcWallIDs mc)
    | otherwise = livef mc . (machines . ix mcid %~ ( (mcDamage .~ []) . (mcHP -~ dams) ))
  where
    dams = sum $ map _dmAmount $ _mcDamage mc 
    mcid = _mcID mc

machineUpdateDeathEff :: (Machine -> World -> World)
    -> Machine -> World -> World
machineUpdateDeathEff f mc
    | _mcHP mc < 1 = f mc 
        . (machines %~ IM.delete mcid)
        . deleteWallIDs (_mcWallIDs mc)
    | otherwise = machines . ix mcid %~ ( (mcDamage .~ []) . (mcHP -~ dams) )
  where
    dams = sum $ map _dmAmount $ _mcDamage mc 
    mcid = _mcID mc

machineAddSound :: SoundID -> (Machine -> World -> World) -> Machine -> World -> World
machineAddSound sid f mc w
    | d < 200   = soundContinueVol (1-0.005*d) (MachineSound mid) (_mcPos mc) sid (Just 2) $ f mc w
    | otherwise = f mc w
  where
    d = dist (_crPos $ you w) (_mcPos mc)
    mid = _mcID mc
