packages feed

mischief-assets-0.1.0.0: examples/FirstAsset.hs

module Main where

import Control.Monad (void)
import Mischief.Assets
import Mischief.ECS
import Mischief.ECS.Interval qualified as Interval

newtype Heal = Heal {amount :: Int}
  deriving (Generic, KdlAsset, Component, Show)

instance Asset Heal where
  type Saveable Heal = False
  loadAsset = loadKdl "heal"

main :: IO ()
main = do
  app <- newApp
  setAssetSource "mischief-assets/assets/" app
  addPlugin @MyPlugin app
  runApp app

data MyPlugin

instance Plugin MyPlugin where
  init = do
    systems spawnPlayers
      & schedule @Startup

    spawn_ $ Observer onLoadHeal

    void $ Interval.start 1000000 healPlayers

  deps = [dep @FileWatcherPlugin]

loadHeal :: System ()
loadHeal = load_ @Heal "heal.kdl"

onLoadHeal :: OnLoad Heal -> System ()
onLoadHeal (OnLoad heal) =
  [q|heal. Heal|]
    & qinfo (\h -> [i|Loaded #{h}|])
    & query_

newtype Health = Health Int deriving (Component)

spawnPlayers :: System ()
spawnPlayers = do
  spawn_ (Name "A", Health 20)
  spawn_ (Name "B", Health 30)
  spawn_ (Name "C", Health 60)

healPlayers :: System ()
healPlayers = query_ $ do
  (player, name, Health hp) <- [q|Entity, Name, Health|]
  Heal heal <- qasset @Heal "heal.kdl"
  qlift $ do
    insert (Health $ hp + heal) player
    info [i|Player #{name} is now at #{hp + heal} hp|]