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
|
2017-07-29 00:20:32 +00:00
|
|
|
:: (Show a, Ord a)
|
2017-04-17 10:40:17 +00:00
|
|
|
=> 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)
|