pituicat/src/Affection/Animation.hs

50 lines
1.1 KiB
Haskell

{-# LANGUAGE RecordWildCards #-}
module Affection.Animation
( SpriteAnimation(..)
, runAnimation
) where
import Affection.Actor
import Affection.Types
import Affection.Property
import qualified GEGL as G
import Control.Monad.State
data SpriteAnimation = SpriteAnimation
{ sanimCurrentFrame :: Int
, sanimSprites :: [FilePath]
, sanimFrameDuration :: Double
, sanimLoop :: Bool
, sanimLastChange :: Double
, sanimPropName :: String
}
runAnimation
:: (Show a, Ord a)
=> Actor a
-> SpriteAnimation
-> Affection us (SpriteAnimation, Actor a)
runAnimation act anim@SpriteAnimation{..} = do
ad <- get
let elapsed = elapsedTime ad
if elapsed - sanimLastChange > sanimFrameDuration
then do
let nframe =
if sanimCurrentFrame + 1 > length sanimSprites
then
if sanimLoop then 1 else sanimCurrentFrame
else sanimCurrentFrame + 1
nact =
updateProperties
(props $ prop sanimPropName $ sanimSprites !! (nframe - 1))
act
return (anim
{ sanimCurrentFrame = nframe
, sanimLastChange = elapsed
}, nact)
else
return (anim, act)