module Dodge.Creature.State.WalkCycle
    where
import Dodge.Data
import Dodge.Data.SoundOrigin
import Dodge.Creature.Stance.Data
import Dodge.SoundLogic
import Dodge.SoundLogic.LoadSound
import Geometry.Data
import Sound.Data

import Control.Lens

updateWalkCycle :: Creature -> Creature
updateWalkCycle cr = case cr ^? crStance . carriage of
    Just (Walking x ff) | x > maxStride -> cr & crStance . carriage .~ Walking 0 (normalGait ff)
    _ -> cr
  where
    maxStride = _strideLength $ _crStance cr

normalGait :: FootForward -> FootForward
normalGait LeftForward = RightForward
normalGait WasLeftForward = RightForward
normalGait RightForward = LeftForward
normalGait WasRightForward = LeftForward

footstepSideEffect :: Creature -> World -> World
footstepSideEffect cr w = case cr ^? crStance . carriage of
    Just (Walking x ff) -> makeFootstepSound x maxStride ff (_crPos cr) w
    _ -> w
  where
    maxStride = _strideLength (_crStance cr)


makeFootstepSound :: Int -> Int -> FootForward -> Point2 -> World -> World
makeFootstepSound currentStride maxStride ff p
    | currentStride > maxStride = soundMultiFrom footor p (chooseFootSound ff) Nothing
    | otherwise = id
  where
    footor = [FootstepSound 0,FootstepSound 1]

chooseFootSound :: FootForward -> SoundID
chooseFootSound LeftForward = foot1S
chooseFootSound _ = foot2S
