packages feed

mischief-render-0.1.0.0: src/Mischief/Animation/Animations.hs

module Mischief.Animation.Animations where

import Data.Functor ((<&>))
import Data.Text (Text)
import KDL qualified
import Mischief.Animation.Spritesheet
import Mischief.Assets
import Mischief.ECS (Component)

data Animations = Animations {spritesheet :: KdlRef Spritesheet, collection :: KdlCollection Animation} deriving (Show, Component)

instance KdlAsset Animations where
  decodeKdl path = KDL.children $ do
    spritesheet <- KDL.nodeWith "spritesheet" $ decodeKdl path
    collection <- KDL.nodeWith "collection" $ KDL.children $ colDecodeWith $ decodeAnimationWithSpritesheet spritesheet

    pure $ Animations {spritesheet, collection}

instance Asset Animations where
  type Saveable Animations = False
  loadAsset = loadKdl "animations"

decodeAnimationWithSpritesheet :: KdlRef Spritesheet -> KDL.NodeDecoder Animation
decodeAnimationWithSpritesheet spritesheet = KDL.children $ do
  frames <- animationFramesDecoder
  time <- animationTimeDecoder
  loop <- animationLoopDecoder
  pure $ Animation {dummy = False, time, frames, loop, spritesheet}

selAnim :: Text -> Text -> AssetRef Animation
selAnim file anim = Ref @Animations file <&> (colGetU anim . (.collection))