packages feed

mischief-live-0.1.0.0: examples/shader/main/Main.hs

module Main where

import Data.Foldable
import Mischief.Animation
import Mischief.Assets
import Mischief.ECS
import Mischief.Live
import Mischief.Materials.Sprite qualified as S
import Mischief.Math
import Mischief.Render
import Mischief.Render.Camera (CameraBackground (..), CameraZoom (..))
import Mischief.Render.Material.Pipeline (updateFragment)
import ShaderCommon (CustomMaterial)

main :: IO ()
main = do
  app <- newApp
  setAssetSource "assets/" app
  attachLive "./mischief-live/examples/shader/live" ["Live.hs"] def app
  addPlugin @P app
  runApp app

data P

instance Plugin P where
  init = do
    schedule @Startup $ systems setup
    schedule @Update $ systems update
    schedule @RenderUpdate $ systems $ renderSortedMaterial @CustomMaterial def {defaultSampler = sampler @PixelSampler} pickY

  deps = [dep @RenderPlugin, dep @AnimationPlugin]

pickY :: Entity -> Query System Float
pickY e = do
  t <- qget e [Mischief.ECS.qd|Transform|]
  pure (-t.translation.y)

setup :: System ()
setup = do
  window <- spawn Window
  spawn_ (Camera, Rel {comp = OutputTo, target = window}, CameraZoom 8, CameraBackground 0.01, def {translation = V3 0 7 0})
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial)
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial, def {translation = V3 (-7) 17 0}, SpriteFlip {x = False, y = True})
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial, def {translation = V3 23 (-10) 0})
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial, def {translation = V3 (-15) (-12) 0}, SpriteFlip {x = False, y = True})
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial, def {translation = V3 (-25) 5 0}, SpriteFlip {x = False, y = True})
  spawn_ (Animator RatAnimator, WithMaterial @CustomMaterial, def {translation = V3 18 10 0})

  query_ $ do
    e <- [q|E / With Animator|]
    qlift $ insert (Anchor $ V2 0.5 1) e

update :: System ()
update = do
  x <- liveIfChanged "Live" "customFrag"
  for_ x $ \x -> updateFragment @CustomMaterial $ \i -> do x i.uv

  -- [q|CameraZoom|] & qinsert (const $ CameraZoom 15) & query_

  s <- liveIfChanged @(System ()) "Live" "zoom"
  for_ s id

-- for_ y $ \y -> [q|CameraZoom|] & qinsert (const $ CameraZoom y) & query_

data RatAnimator = RatAnimator

instance AnimatorTag RatAnimator where
  data State RatAnimator = State
  data Input RatAnimator = Input deriving (Generic, Default)
  data Shift RatAnimator = Shift

  animation State = selAnim "rats/rat-animations.kdl" "rat3"

  start = State