module Data.Animate
( Seconds
, DeltaSeconds
, Frame(..)
, Animations
, animations
, framesByAnimation
, Loop(..)
, Position(..)
, FrameStep(..)
, stepFrame
, stepAnimation
, isAnimationComplete
, positionHasLooped
) where
import qualified Data.Vector as V (Vector, (!), length, fromList)
type Seconds = Float
type DeltaSeconds = Seconds
data Frame loc = Frame
{ frameLocation :: loc
, frameDelay :: Seconds
} deriving (Show, Eq)
newtype Animations a loc = Animations { unAnimations :: V.Vector (V.Vector (Frame loc)) }
deriving (Show, Eq)
animations :: (Enum a, Bounded a) => (a -> [Frame loc]) -> Animations a loc
animations getFrames = Animations $ V.fromList $ map (V.fromList . getFrames) [minBound..maxBound]
framesByAnimation :: Enum a => Animations a loc -> a -> V.Vector (Frame loc)
framesByAnimation (Animations as) a = as V.! fromEnum a
data Loop
= Loop'Forever
| Loop'Count Int
deriving (Show, Eq)
data Position a = Position
{ positionAnimation :: a
, positionFrameIndex :: Int
, positionCounter :: Seconds
, positionLoop :: Loop
} deriving (Show, Eq)
data FrameStep
= FrameStep'Counter Seconds
| FrameStep'Delta DeltaSeconds
deriving (Show, Eq)
stepFrame :: Frame loc -> Position a -> DeltaSeconds -> FrameStep
stepFrame Frame{frameDelay} Position{positionCounter} delta =
if positionCounter + delta >= frameDelay
then FrameStep'Delta $ positionCounter + delta frameDelay
else FrameStep'Counter $ positionCounter + delta
stepAnimation :: Enum a => Animations a loc -> Position a -> DeltaSeconds -> Position a
stepAnimation as p d =
case frameStep of
FrameStep'Counter counter -> p{positionCounter = counter }
FrameStep'Delta delta -> stepAnimation as p' delta
where
frameStep = stepFrame f p d
fs = unAnimations as V.! fromEnum (positionAnimation p)
f = fs V.! positionFrameIndex p
p'= case positionLoop p of
Loop'Forever -> p{positionFrameIndex = (positionFrameIndex p + 1) `mod` V.length fs, positionCounter = 0}
Loop'Count n -> let
index = (positionFrameIndex p + 1) `mod` V.length fs
n' = if index == 0 then n 1 else n
in p
{ positionFrameIndex = if n' < 0 then positionFrameIndex p else index
, positionCounter = 0
, positionLoop = Loop'Count n' }
isAnimationComplete :: Enum a => Animations a loc -> Position a -> Bool
isAnimationComplete as p = case positionLoop p of
Loop'Forever -> False
Loop'Count n -> n < 0 && positionFrameIndex p == lastIndex && positionCounter p >= frameDelay lastFrame
where
frames = framesByAnimation as (positionAnimation p)
lastIndex = V.length frames 1
lastFrame = frames V.! lastIndex
positionHasLooped
:: Position a
-> Position a
-> Bool
positionHasLooped Position{ positionLoop = Loop'Count c } Position{ positionLoop = Loop'Count c' } = c > c'
positionHasLooped Position{ positionLoop = Loop'Forever } _ = False
positionHasLooped _ Position{ positionLoop = Loop'Forever } = False