Rev up your Haskell game engines with the latest Aztecs, an ECS and modular game-engine. The latest v0.17 adds new packages for working with OpenGL using a high-level, ECS-driven API.
This is all possible to the new features around reactivity for components, featuring component lifecycles that can trigger both direct hooks in the Component class and Observer components that target the component’s EntityID:
newtype Health = Health Int
deriving (Show)
instance Component IO Health
heal :: Query IO Health
heal = queryMap (\(Health h) -> Health (h + 10))
run :: Access IO ()
run = do
-- Spawn a player with health
player <- spawn . bundle $ Health 100
-- Spawn observers that react to lifecycle events on the player's Health component
_ <- spawn . bundle . observer @IO @(OnInsert Health) player $ \e (OnInsert h) ->
go "insert" e h
_ <- spawn . bundle . observer @IO @(OnChange Health) player $ \e (OnChange _ new) ->
go "change" e new
_ <- spawn . bundle . observer @IO @(OnRemove Health) player $ \e (OnRemove h) ->
go "remove" e h
-- Trigger events by inserting, changing, and removing the Health component
_ <- insert player . bundle $ Health 150
_ <- system $ runQuery heal
_ <- remove @_ @Health player
return ()
where
go s e h = liftIO . putStrLn $ s ++ " " ++ show e ++ ": " ++ show h
