aztecs-0.16.0: A modular game engine and Entity-Component-System (ECS) for Haskell.
Safe HaskellNone
LanguageHaskell2010

Aztecs.ECS

Description

Aztecs is a type-safe and friendly ECS for games and more.

An ECS is a modern approach to organizing your application state as a database, providing patterns for data-oriented design and parallel processing.

The ECS architecture is composed of three main concepts:

Entities

An entity is an object comprised of zero or more components. In Aztecs, entities are represented by their EntityID, a unique identifier.

Components

A Component holds the data for a particular aspect of an entity. For example, a zombie entity might have a Health and a Transform component.

newtype Position = Position Int deriving (Show)
instance Component Position

newtype Velocity = Velocity Int deriving (Show)
instance Component Velocity

Systems

A System is a pipeline that processes entities and their components. Systems in Aztecs either run in sequence or in parallel automatically based on the components they access.

Systems can access game state in two ways:

Access

An Access can be queued for full access to the World, after a system is complete. Access allows for spawning, inserting, and removing components.

setup :: System  () ()
setup = S.queue . const . A.spawn_ $ bundle (Position 0) <> bundle (Velocity 1)

Queries

A Query can read and write matching components.

move :: System  () ()
move =
 S.map
   ( proc () -> do
       Velocity v <- Q.query -< ()
       Position p <- Q.query -< ()
       Q.set -< Position $ p + v
   )
   >>> S.run print

Finally, systems can be run on a World to produce a result.

main :: IO ()
main = runSystem_ $ setup >>> S.forever move
Synopsis

Documentation

type Bundle = BundleT Identity Source #

Pure bundle of components.

data BundleT (m :: Type -> Type) Source #

Bundle of components.

Instances

Instances details
Monad m => MonoidDynamicBundle m (BundleT m) Source # 
Instance details

Defined in Aztecs.ECS.World.Bundle

Monad m => Monoid (BundleT m) Source # 
Instance details

Defined in Aztecs.ECS.World.Bundle

Methods

mempty :: BundleT m #

mappend :: BundleT m -> BundleT m -> BundleT m #

mconcat :: [BundleT m] -> BundleT m #

Monad m => Semigroup (BundleT m) Source # 
Instance details

Defined in Aztecs.ECS.World.Bundle

Methods

(<>) :: BundleT m -> BundleT m -> BundleT m #

sconcat :: NonEmpty (BundleT m) -> BundleT m #

stimes :: Integral b => b -> BundleT m -> BundleT m #

bundle :: forall (m :: Type -> Type) a. Component m a => a -> BundleT m Source #

data DynamicBundle (m :: Type -> Type) Source #

Dynamic bundle of components.

class MonoidDynamicBundle (m :: Type -> Type) a where Source #

Monoid bundle of dynamic components.

Methods

dynBundle :: Component m c => ComponentID -> c -> a Source #

Add a component to the bundle by its ComponentID.

dynBundleUntracked :: Component m c => ComponentID -> c -> a Source #

Add a component to the bundle by its ComponentID without running lifecycle hooks.

Instances

Instances details
Monad m => MonoidDynamicBundle m (BundleT m) Source # 
Instance details

Defined in Aztecs.ECS.World.Bundle

Monad m => MonoidDynamicBundle m (DynamicBundle m) Source # 
Instance details

Defined in Aztecs.ECS.World.Bundle.Dynamic

class (Monad m, Typeable a, Storage a (StorageT a)) => Component (m :: Type -> Type) a where Source #

Component that can be stored in the World.

Minimal complete definition

Nothing

Associated Types

type StorageT a Source #

Storage of this component.

type StorageT a = Vector a

Methods

componentOnInsert :: EntityID -> a -> Access m () Source #

Lifecycle hook called when a component is inserted.

componentOnChange :: EntityID -> a -> Access m () Source #

Lifecycle hook called when a component is changed.

componentOnRemove :: EntityID -> a -> Access m () Source #

Lifecycle hook called when a component is removed.

Instances

Instances details
Monad m => Component m ChildState Source # 
Instance details

Defined in Aztecs.Hierarchy

Associated Types

type StorageT ChildState 
Instance details

Defined in Aztecs.Hierarchy

Monad m => Component m Children Source # 
Instance details

Defined in Aztecs.Hierarchy

Associated Types

type StorageT Children 
Instance details

Defined in Aztecs.Hierarchy

type StorageT Children = Vector Children
Monad m => Component m Parent Source # 
Instance details

Defined in Aztecs.Hierarchy

Associated Types

type StorageT Parent 
Instance details

Defined in Aztecs.Hierarchy

type StorageT Parent = Vector Parent
Monad m => Component m ParentState Source # 
Instance details

Defined in Aztecs.Hierarchy

Associated Types

type StorageT ParentState 
Instance details

Defined in Aztecs.Hierarchy

(Monad m, Typeable m, Event e) => Component m (Observer m e) Source # 
Instance details

Defined in Aztecs.ECS.Observer

Associated Types

type StorageT (Observer m e) 
Instance details

Defined in Aztecs.ECS.Observer

type StorageT (Observer m e) = Vector (Observer m e)

data EntityID Source #

Unique entity identifier.

Instances

Instances details
Generic EntityID Source # 
Instance details

Defined in Aztecs.ECS.Entity

Associated Types

type Rep EntityID 
Instance details

Defined in Aztecs.ECS.Entity

type Rep EntityID = D1 ('MetaData "EntityID" "Aztecs.ECS.Entity" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "EntityID" 'PrefixI 'True) (S1 ('MetaSel ('Just "unEntityId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))

Methods

from :: EntityID -> Rep EntityID x #

to :: Rep EntityID x -> EntityID #

Show EntityID Source # 
Instance details

Defined in Aztecs.ECS.Entity

Eq EntityID Source # 
Instance details

Defined in Aztecs.ECS.Entity

Ord EntityID Source # 
Instance details

Defined in Aztecs.ECS.Entity

type Rep EntityID Source # 
Instance details

Defined in Aztecs.ECS.Entity

type Rep EntityID = D1 ('MetaData "EntityID" "Aztecs.ECS.Entity" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "EntityID" 'PrefixI 'True) (S1 ('MetaSel ('Just "unEntityId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))

data Query (m :: Type -> Type) a Source #

Query for matching entities.

Instances

Instances details
Monad m => DynamicQueryF m (Query m) Source # 
Instance details

Defined in Aztecs.ECS.Query

Methods

entity :: Query m EntityID Source #

queryDyn :: Component m a => ComponentID -> Query m a Source #

queryMaybeDyn :: Component m a => ComponentID -> Query m (Maybe a) Source #

queryMapDyn :: Component m a => (a -> a) -> ComponentID -> Query m a Source #

queryMapDyn_ :: Component m a => (a -> a) -> ComponentID -> Query m () Source #

queryMapDynM :: (Monad m, Component m a) => (a -> m a) -> ComponentID -> Query m a Source #

queryMapDynWith :: Component m b => (a -> b -> b) -> ComponentID -> Query m a -> Query m b Source #

queryMapDynWith_ :: Component m b => (a -> b -> b) -> ComponentID -> Query m a -> Query m () Source #

queryMapDynWithM :: (Monad m, Component m b) => (a -> b -> m b) -> ComponentID -> Query m a -> Query m b Source #

queryMapDynWithAccum :: Component m c => (b -> c -> (a, c)) -> ComponentID -> Query m b -> Query m (a, c) Source #

queryMapDynWithAccumM :: (Monad m, Component m c) => (b -> c -> m (a, c)) -> ComponentID -> Query m b -> Query m (a, c) Source #

queryFilterMap :: (a -> Maybe b) -> Query m a -> Query m b Source #

queryFilter :: (a -> Bool) -> Query m a -> Query m a Source #

queryUntracked :: Query m a -> Query m a Source #

Monad m => Applicative (Query m) Source # 
Instance details

Defined in Aztecs.ECS.Query

Methods

pure :: a -> Query m a #

(<*>) :: Query m (a -> b) -> Query m a -> Query m b #

liftA2 :: (a -> b -> c) -> Query m a -> Query m b -> Query m c #

(*>) :: Query m a -> Query m b -> Query m b #

(<*) :: Query m a -> Query m b -> Query m a #

Functor m => Functor (Query m) Source # 
Instance details

Defined in Aztecs.ECS.Query

Methods

fmap :: (a -> b) -> Query m a -> Query m b #

(<$) :: a -> Query m b -> Query m a #

query :: forall (m :: Type -> Type) a. (Monad m, Component m a) => Query m a Source #

Query a component.

queryMaybe :: forall (m :: Type -> Type) a. (Monad m, Component m a) => Query m (Maybe a) Source #

Optionally query a component, returning Nothing if it does not exist.

queryMap :: forall (m :: Type -> Type) a. (Monad m, Component m a) => (a -> a) -> Query m a Source #

Query a component and update it.

queryMapM :: (Monad m, Component m a) => (a -> m a) -> Query m a Source #

Query a component and update it with a monadic action.

queryMapWith :: forall (m :: Type -> Type) a b. (Monad m, Component m b) => (a -> b -> b) -> Query m a -> Query m b Source #

Query a component with input and update it.

queryMapWith_ :: forall (m :: Type -> Type) a b. (Monad m, Component m b) => (a -> b -> b) -> Query m a -> Query m () Source #

Query a component with input and update it, ignoring any output.

queryMapWithM :: forall m a b. (Monad m, Component m b) => (a -> b -> m b) -> Query m a -> Query m b Source #

Query a component with input and update it with a monadic action.

class (Monad m, Functor f) => DynamicQueryF (m :: Type -> Type) (f :: Type -> Type) | f -> m Source #

Dynamic query functor.

Instances

Instances details
Monad m => DynamicQueryF m (Query m) Source # 
Instance details

Defined in Aztecs.ECS.Query

Methods

entity :: Query m EntityID Source #

queryDyn :: Component m a => ComponentID -> Query m a Source #

queryMaybeDyn :: Component m a => ComponentID -> Query m (Maybe a) Source #

queryMapDyn :: Component m a => (a -> a) -> ComponentID -> Query m a Source #

queryMapDyn_ :: Component m a => (a -> a) -> ComponentID -> Query m () Source #

queryMapDynM :: (Monad m, Component m a) => (a -> m a) -> ComponentID -> Query m a Source #

queryMapDynWith :: Component m b => (a -> b -> b) -> ComponentID -> Query m a -> Query m b Source #

queryMapDynWith_ :: Component m b => (a -> b -> b) -> ComponentID -> Query m a -> Query m () Source #

queryMapDynWithM :: (Monad m, Component m b) => (a -> b -> m b) -> ComponentID -> Query m a -> Query m b Source #

queryMapDynWithAccum :: Component m c => (b -> c -> (a, c)) -> ComponentID -> Query m b -> Query m (a, c) Source #

queryMapDynWithAccumM :: (Monad m, Component m c) => (b -> c -> m (a, c)) -> ComponentID -> Query m b -> Query m (a, c) Source #

queryFilterMap :: (a -> Maybe b) -> Query m a -> Query m b Source #

queryFilter :: (a -> Bool) -> Query m a -> Query m a Source #

queryUntracked :: Query m a -> Query m a Source #

Monad m => DynamicQueryF m (DynamicQuery m) Source # 
Instance details

Defined in Aztecs.ECS.Query.Dynamic

with :: forall (m :: Type -> Type) a. Component m a => QueryFilter Source #

Filter for entities containing this component.

without :: forall (m :: Type -> Type) a. Component m a => QueryFilter Source #

Filter out entities containing this component.

data World (m :: Type -> Type) Source #

World of entities and their components.

Events

class Typeable e => Event e Source #

An event in the ECS.

Instances

Instances details
Typeable a => Event (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Typeable a => Event (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Typeable a => Event (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

newtype OnInsert a Source #

Event triggered when a component is inserted.

Constructors

OnInsert 

Fields

Instances

Instances details
Typeable a => Event (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Generic (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Associated Types

type Rep (OnInsert a) 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnInsert a) = D1 ('MetaData "OnInsert" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnInsert" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnInsert") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Methods

from :: OnInsert a -> Rep (OnInsert a) x #

to :: Rep (OnInsert a) x -> OnInsert a #

Show a => Show (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

showsPrec :: Int -> OnInsert a -> ShowS #

show :: OnInsert a -> String #

showList :: [OnInsert a] -> ShowS #

Eq a => Eq (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

(==) :: OnInsert a -> OnInsert a -> Bool #

(/=) :: OnInsert a -> OnInsert a -> Bool #

type Rep (OnInsert a) Source # 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnInsert a) = D1 ('MetaData "OnInsert" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnInsert" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnInsert") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

newtype OnChange a Source #

Event triggered when a component is changed.

Constructors

OnChange 

Fields

Instances

Instances details
Typeable a => Event (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Generic (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Associated Types

type Rep (OnChange a) 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnChange a) = D1 ('MetaData "OnChange" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnChange" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnChange") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Methods

from :: OnChange a -> Rep (OnChange a) x #

to :: Rep (OnChange a) x -> OnChange a #

Show a => Show (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

showsPrec :: Int -> OnChange a -> ShowS #

show :: OnChange a -> String #

showList :: [OnChange a] -> ShowS #

Eq a => Eq (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

(==) :: OnChange a -> OnChange a -> Bool #

(/=) :: OnChange a -> OnChange a -> Bool #

type Rep (OnChange a) Source # 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnChange a) = D1 ('MetaData "OnChange" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnChange" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnChange") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

newtype OnRemove a Source #

Event triggered when a component is removed.

Constructors

OnRemove 

Fields

Instances

Instances details
Typeable a => Event (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Generic (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Associated Types

type Rep (OnRemove a) 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnRemove a) = D1 ('MetaData "OnRemove" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnRemove" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnRemove") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Methods

from :: OnRemove a -> Rep (OnRemove a) x #

to :: Rep (OnRemove a) x -> OnRemove a #

Show a => Show (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

showsPrec :: Int -> OnRemove a -> ShowS #

show :: OnRemove a -> String #

showList :: [OnRemove a] -> ShowS #

Eq a => Eq (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

Methods

(==) :: OnRemove a -> OnRemove a -> Bool #

(/=) :: OnRemove a -> OnRemove a -> Bool #

type Rep (OnRemove a) Source # 
Instance details

Defined in Aztecs.ECS.Event

type Rep (OnRemove a) = D1 ('MetaData "OnRemove" "Aztecs.ECS.Event" "aztecs-0.16.0-1t3LRuVnzUqKpg58E4sSVq" 'True) (C1 ('MetaCons "OnRemove" 'PrefixI 'True) (S1 ('MetaSel ('Just "unOnRemove") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Observers

data Observer (m :: Type -> Type) e Source #

Observer component

Instances

Instances details
(Monad m, Typeable m, Event e) => Component m (Observer m e) Source # 
Instance details

Defined in Aztecs.ECS.Observer

Associated Types

type StorageT (Observer m e) 
Instance details

Defined in Aztecs.ECS.Observer

type StorageT (Observer m e) = Vector (Observer m e)
Show (Observer m e) Source # 
Instance details

Defined in Aztecs.ECS.Observer

Methods

showsPrec :: Int -> Observer m e -> ShowS #

show :: Observer m e -> String #

showList :: [Observer m e] -> ShowS #

type StorageT (Observer m e) Source # 
Instance details

Defined in Aztecs.ECS.Observer

type StorageT (Observer m e) = Vector (Observer m e)

observer :: forall (m :: Type -> Type) e. Event e => EntityID -> (EntityID -> e -> Access m ()) -> Observer m e Source #

Create an observer for a single entity.

observerFor :: forall (m :: Type -> Type) e. Event e => Set EntityID -> (EntityID -> e -> Access m ()) -> Observer m e Source #

Create an observer for specific entities.

observerGlobal :: forall (m :: Type -> Type) e. Event e => (e -> Access m ()) -> Observer m e Source #

Create a global observer (observes all events of this type).