module Main where import Changes qualified import Control.Applicative import Data.Foldable import Mischief.ECS import Mischief.ECS.Time qualified as Time import Mischief.ECS.World.Query.TH (p) import Utils main :: IO () main = do runTests [ test1, test2, test3, test4, test5 ] Changes.main data P instance Plugin P where init = systems s & schedule @FixedUpdate deps = [dep @FixedPlugin] s :: System () s = do delta <- Time.delta elapsed <- Time.elapsed info [i|#{delta} -- #{elapsed}|] test1 :: System () test1 = do a <- spawn (Name "A", Comp1) b <- spawn (Name "B", Comp1) c <- spawn (Name "C", Comp1, Comp2) d <- spawn (Name "D", Comp2) e <- spawn (Name "E", Comp2) f <- spawn (Name "F", Comp1, Comp3) g <- spawn (Name "G", Comp2, Comp3) assertqWith [q|E, Name / With Comp1|] [(a, Name "A"), (b, Name "B"), (c, Name "C"), (f, Name "F")] assertqWith [q|E, Name / With Comp2|] [(c, Name "C"), (d, Name "D"), (e, Name "E"), (g, Name "G")] assertqWith [q|E, Name / With Comp3|] [(f, Name "F"), (g, Name "G")] assertqWith [q|E, Name / With Comp3, Without Comp2|] [(f, Name "F")] assertqWith [q|E, Name / With Comp1, With Comp2|] [(c, Name "C")] test2 :: System () test2 = do a <- spawn (Pos 3, Velocity 2, Comp1) b <- spawn (Pos 4, Velocity 2, Comp2, Comp3) c <- spawn (Pos 5, Velocity 3) query_ $ do (e, Pos p, Velocity v) <- [q|E, Pos, Velocity / With Comp1 || (With Comp2, With Comp3)|] qlift $ insert (Pos $ p + v) e assertqWith [q|E, Pos|] [(a, Pos 5), (b, Pos 6), (c, Pos 5)] test3 :: System () test3 = do a <- spawn (Pos 3, Velocity 5) insert (Rel (Likes 1) a) a b <- spawn (Pos 4, Velocity 2, Rel (Likes 5) a) c <- spawn (Pos 2, Velocity 1, Rel (Likes 1) a, Rel (Likes 2) b) d <- spawn (Pos 3, Rel (Likes 2) a, Rel (Likes 3) c) query_ $ do (e, Pos p, r) <- [q|Entity, Pos, Likes -> *|] rs <- qcollect $ do (entity, Velocity v) <- [q|Entity, Velocity|] case find ((== entity) . (.target)) r of Nothing -> empty Just (Rel (Likes l) _) -> pure $ l * v qlift $ insert (Pos $ p + sum rs) e assertqWith [q|E, Pos|] [(a, Pos 8), (b, Pos 29), (c, Pos 11), (d, Pos 16)] test4 :: System () test4 = do a <- spawn (Name "A", Comp1) b <- spawn (Name "B", Rel ChildOf a, Comp1) c <- spawn (Name "C", Rel ChildOf a, Comp1) d <- spawn (Name "D", Comp1) e <- spawn (Name "E", Rel ChildOf d, Comp1) query_ $ do (e, name) <- [q|Entity, Name / With Comp1|] e <- [q|E / With ChildOf -> e|] qlift $ insert name e assertqWith [q|E, Name / With Comp1|] [(a, Name "A"), (b, Name "A"), (c, Name "A"), (d, Name "D"), (e, Name "D")] test5 :: System () test5 = do a <- spawn (Name "A", Comp1) b <- spawn (Name "B", Rel ChildOf a, Comp1) c <- spawn (Name "C", Rel ChildOf a, Comp1) d <- spawn (Name "D", Comp1) e <- spawn (Name "E", Rel ChildOf d, Comp1) query_ $ do (e, n) <- [q|E, ChildOf -> (Name) / With Comp1|] qlift $ insert n e assertqWith [q|E, Name / With Comp1|] [(a, Name "A"), (b, Name "A"), (c, Name "A"), (d, Name "D"), (e, Name "D")]