module Dodge.Magnet where
import Dodge.Data
import Geometry

import Control.Lens

dampField :: Magnet -> Particle -> Particle
dampField mg pt = case pt of
    BulletPt{} | dist (head $ _ptTrail pt) (_mgPos mg) < 100 -> pt & ptVel *~ 0.5
    _ -> pt

curveLeftField :: Magnet -> Particle -> Particle
curveLeftField mg pt = case pt of
    BulletPt{} | dist (head $ _ptTrail pt) (_mgPos mg) < 500 -> pt & ptVel %~ rotateV 0.2
    _ -> pt

curveAroundField :: Float -> Float -> Magnet -> Particle -> Particle
curveAroundField minrad maxrad mg pt = case pt of
    BulletPt{} | thedist < maxrad -> pt & ptVel %~ rotateToCircle
      where
        thedist = dist btpos mgpos
        mgpos = _mgPos mg
        btpos = head $ _ptTrail pt
        rotateToCircle vel 
            | (isLHS mgpos btpos (btpos +.+ vel) && isRHS mgpos btpos (mgpos +.+ vNormal vel) )
              || (isRHS mgpos btpos (btpos +.+ vel) && isLHS mgpos btpos (mgpos +.+ vNormal vel) )
                = rotateV rot vel
            | otherwise = rotateV (negate rot) vel
        rot | thedist < minrad = 0.5
            | otherwise        = 0.5 * ((maxrad - thedist) / (maxrad - minrad) ) ** 2
    _ -> pt
