module Dodge.Item.Weapon.AmmoParams
    ( useAmmoParams
    , loadedAmmo
    , useAmmoParamsVelMod
    , fractionLoadedAmmo
    , fractionLoadedAmmo2
    ) where
import Dodge.Data
import Dodge.Particle.Bullet.Spawn
import Geometry

import Control.Lens

useAmmoParams :: Item -> Creature -> World -> World
useAmmoParams it = withVelWthHiteff (_amBulVel b) (_amBulWth b) (_amBulEff b) 
  where
    b = _wpAmmo it

useAmmoParamsVelMod :: Float -> Item -> Creature -> World -> World
useAmmoParamsVelMod vfact it = withDelayedVelWthHiteff vfact (_amBulVel b) (_amBulWth b) (_amBulEff b) 
  where
    b = _wpAmmo it

{- | Creates a bullet with a given velocity, width, and 'HitEffect' -}
withVelWthHiteff 
    :: Point2       -- ^ Velocity, x direction is forward with respect to the creature
    -> Float        -- ^ Bullet width
    -> HitEffect    -- ^ Bullet effect when hitting creature, wall etc
    -> Creature
    -> World 
    -> World
withVelWthHiteff vel width hiteff cr = over particles (newbul : ) 
  where 
    cid = _crID cr
    newbul = aGenBulAt (Just cid) pos (rotateV dir vel) hiteff width
    dir = _crDir cr
    pos = _crPos cr +.+ _crRad cr *.* unitVectorAtAngle (_crDir cr)

{- | Creates a bullet with a given velocity, width, and 'HitEffect' -}
withDelayedVelWthHiteff 
    :: Float        -- ^ Velocity factor for first step
    -> Point2       -- ^ Velocity, x direction is forward with respect to the creature
    -> Float        -- ^ Bullet width
    -> HitEffect    -- ^ Bullet effect when hitting creature, wall etc
    -> Creature          -- ^ Creature id
    -> World 
    -> World
withDelayedVelWthHiteff vfact vel width hiteff cr = over particles (newbul : ) 
  where 
    cid = _crID cr
    newbul = aDelayedBulAt vfact (Just cid) pos (rotateV dir vel) hiteff width
    dir = _crDir cr
    pos = _crPos cr +.+ _crRad cr *.* unitVectorAtAngle (_crDir cr)

loadedAmmo :: Item -> Int
loadedAmmo it
    | _wpReloadState it == 0 = _wpLoadedAmmo it
    | otherwise = 0

fractionLoadedAmmo :: Item -> Float
fractionLoadedAmmo it = fromIntegral (loadedAmmo it) / fromIntegral (_wpMaxAmmo it)

fractionLoadedAmmo2 :: Item -> Float
fractionLoadedAmmo2 it = 1 - (1 - fromIntegral (loadedAmmo it) / fromIntegral (_wpMaxAmmo it))**2
