I’m really excited to announce the latest version of Aztecs, an ECS for Haskell. An ECS is a modern approach to organizing your application state as a database.
import Aztecs.ECS
import qualified Aztecs.ECS.Access as A
import qualified Aztecs.ECS.Query as Q
import qualified Aztecs.ECS.System as S
import Control.Monad.IO.Class
import Data.Function ((&))
newtype Position = Position Int deriving (Show)
instance Component Position
newtype Velocity = Velocity Int deriving (Show)
instance Component Velocity
move :: QueryT IO Position
move = Q.fetch & Q.adjust (\(Velocity v) (Position p) -> Position $ p + v)
run :: AccessT IO ()
run = do
positions <- S.map move
liftIO $ print positions
app :: AccessT IO ()
app = do
A.spawn_ $ bundle (Position 0) <> bundle (Velocity 1)
run
main :: IO ()
main = runAccessT_ app
This latest v0.15 reverts the lower-level design in the last few versions to the original archetype-based design, where entities with matching components are stored together. This enables queries to efficiently map over components stored in ordered Vectors as a zip.
With the latest Vector storages, lookups are now O(1) and queries are much faster, about 5x within Bevy (still some work to be done
)