affection/src/Affection/Animation.hs

50 lines
1.1 KiB
Haskell
Raw Normal View History

2017-04-17 10:40:17 +00:00
{-# 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
:: 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)