module Dodge.Item.Weapon.Bezier
    where
import Dodge.Data
import Dodge.Base
import Dodge.Default.Weapon
import Dodge.Item.Data
import Dodge.Item.Draw
import Dodge.Item.Weapon.Targeting
import Dodge.Item.Weapon.TriggerType
import Dodge.Item.Weapon.ExtraEffect
import Dodge.Item.Attachment.Data
import Dodge.Default
import Dodge.Particle.Bullet.Spawn
import Dodge.Particle.Bullet.HitEffect
import Dodge.WorldEvent.HitEffect
import Dodge.RandomHelp
import Dodge.SoundLogic.LoadSound
import Picture
import Geometry
import ShapePicture
import Shape

import Data.Maybe
import Control.Lens
import Control.Monad.State
bezierGun :: Item
bezierGun = defaultGun
    { _itName = "B-GUN"
    , _itUse = \_ ->  useTargetPos $ \p -> shootBezier $ fromJust p    -- <- the start point
    , _itUseModifiers =
        [ useTimeCheckI
        , withSoundStart tap2S
        , useAmmo 1
        , withMuzFlareI 
        . withRecoilI 40
        . torqueBefore 0.05
        ]
    , _itFloorPict = bezierGunSPic
    , _itEquipPict = pictureWeaponOnAim bezierGunSPic
    , _itAttachment = NoItAttachment
    , _itScroll   = \_ _ -> removeItTarget
    , _itHammer = HammerUp
    , _itEffect = rbSetTarget
    , _itZoom = defaultItZoom
    , _itAimingRange = 0
    , _wpMaxAmmo = 50
    , _itUseRate = 6
    , _itAimStance = TwoHandTwist
    }
bezierGunSPic :: SPic
bezierGunSPic = 
    ( colorSH red $ upperPrismPoly 5 $ rectNESW 4 12 (-4) (-12)
    , mempty
    )

shootBezier :: Point2 -> Creature -> World -> World
shootBezier targetp cr w = w & particles %~ (theBullet :)
  where
    theBullet = aCurveBulAt 
        (Just cid) 
        (V4 200 200 200 1) 
        startp 
        (controlp +.+ randPos) 
        (targetp +.+ randPos')
        (destroyOnImpact bulHitCr bulHitWall bulHitFF') 
        5
    controlp = mouseWorldPos w
    cid = _crID cr
    dir = _crDir cr
    startp = _crPos cr +.+ rotateV dir (V2 (_crRad cr + 1) 0)
    (randPos,randPos') = flip evalState (_randGen w) $ do 
        a <- randInCirc 10
        b <- randInCirc 20
        return (a,b)
