module Dodge.Item.Weapon.SprayGuns
    ( poisonSprayer
    , flamer
    )
    where
import Dodge.Data
--import Dodge.Data.SoundOrigin
--import Dodge.Base
--import Dodge.Zone
--import Dodge.Picture.Layer
--import Dodge.SoundLogic
import Dodge.SoundLogic.LoadSound
--import Dodge.Creature.Action
import Dodge.WorldEvent
import Dodge.Default
--import Dodge.Item.Weapon.BulletGuns
--import Dodge.Item.Weapon.InventoryDisplay
import Dodge.Item.Weapon.TriggerType
--import Dodge.Item.Weapon.ExtraEffect
--import Dodge.Item.Weapon.UseEffect
import Dodge.Item.Weapon.AmmoParams
import Dodge.Default.Weapon
--import Dodge.Item.Attachment
import Geometry
--import Geometry.Vector3D
import Picture
import Shape
import ShapePicture

import Data.Function
import Control.Lens
import System.Random
poisonSprayer :: Item
poisonSprayer = defaultAutoGun 
    { _itName = "POISON"
    , _itIdentity = PoisonSprayer
    , _wpMaxAmmo = 500
    , _wpLoadedAmmo = 500
    , _wpReloadTime = 100
    , _wpReloadState = 0
    , _itUseRate = 0
    , _itUseTime = 0
    , _itUse = const aGasCloud 
    , _itUseModifiers =
        [ ammoCheckI
        , useTimeCheckI
        , withSoundForI foamSprayLoopS 5
        , useAmmo 1
        , spreadNumI
        ]
    , _wpRange = 8
    , _itFloorPict = flamerPic
    , _itAimingSpeed = 0.2
    , _itAimingRange = 0
    , _itZoom = defaultItZoom
    , _wpSpread = 0.1
    , _wpNumBarrels = 3
    }
flamer :: Item
flamer = defaultAutoGun 
    { _itName = "FLAMER"
    , _itIdentity = Flamethrower
    , _wpMaxAmmo = 250
    , _wpLoadedAmmo = 250
    , _wpReloadTime = 100
    , _wpReloadState = 0
    , _itUseRate = 0
    , _itUseTime = 0
    , _itUse = \_ -> randWalkAngle 0.2 0.01 aFlame
    , _itUseModifiers =
        [ ammoUseCheckI
        , withSidePushI 5
        --, withTempLight 1 100 (V3 1 0 0)
        , withSidePushAfterI 20
        ]
    , _wpSpread = 0
    , _wpRange = 8
    , _itFloorPict = flamerPic
    , _itAimingSpeed = 0.5
    , _itAimingRange = 0
    , _itZoom       = defaultItZoom
    , _itAimZoom    = defaultItZoom {_itZoomMax = 5, _itZoomMin = 1.5}
    , _itAttachment = NoItAttachment
    }
flamerPic :: Item -> SPic
flamerPic it = 
    ( colorSH yellow $ 
        translateSHf tx ty (upperPrismPoly tz $ polyCirc 3 r)
        ++ upperPrismPoly 5 (rectNESW 2 9 (-2) (-9))
    , color black $ translate3 (V3 tx ty (tz+0.01)) $ circleSolid (r * am)
    )
  where
    tx = - 5
    ty = - 6
    tz = 3
    r = 5
    am = fractionLoadedAmmo2 it
aGasCloud :: Creature -> World -> World
aGasCloud cr w = insertCloud $ set randGen g w
  where 
    (a,g) = randomR (-0.1,0.1) (_randGen w)
    dir = _crDir cr + a
    pos = _crPos cr +.+ ((_crRad cr + 2.9) *.* unitVectorAtAngle (_crDir cr))
    vel = (_crPos cr -.- _crOldPos cr) +.+ 10 *.* unitVectorAtAngle dir
    insertCloud = makeGasCloud pos vel

aFlame :: Creature -> World -> World
aFlame cr w = w & particles %~ (aFlameParticle t pos vel (Just cid) :)
  where 
    (t,_) = randomR (99,101) (_randGen w)
    cid = _crID cr
    dir = _crDir cr
    pos = _crPos cr +.+ ((_crRad cr + 2.9) *.* unitVectorAtAngle (_crDir cr))
    vel = (_crPos cr -.- _crOldPos cr) +.+ 4 *.* unitVectorAtAngle dir
