| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Apecs
Description
This module forms the apecs Prelude. It selectively re-exports the user-facing functions from the submodules.
Synopsis
- module Data.Proxy
- newtype System w a = System {}
- class Elem (Storage c) ~ c => Component c where
- type Storage c
- newtype Entity = Entity {}
- class Component c => Has w c where
- data Not a = Not
- type Get w c = (Has w c, ExplGet (Storage c))
- type Set w c = (Has w c, ExplSet (Storage c))
- type Destroy w c = (Has w c, ExplDestroy (Storage c))
- type Members w c = (Has w c, ExplMembers (Storage c))
- data Map c
- data Unique c
- data Global c
- data Cache (n :: Nat) s
- explInit :: ExplInit s => IO s
- get :: forall w c. Get w c => Entity -> System w c
- set :: forall w c. Set w c => Entity -> c -> System w ()
- getAll :: forall w c. (Get w c, Members w c) => System w [c]
- cmap :: forall w cx cy. (Get w cx, Members w cx, Set w cy) => (cx -> cy) -> System w ()
- cmapM :: forall w cx cy. (Get w cx, Set w cy, Members w cx) => (cx -> System w cy) -> System w ()
- cmapM_ :: forall w c a. (Get w c, Members w c) => (c -> System w a) -> System w ()
- cfold :: forall w c a. (Members w c, Get w c) => (a -> c -> a) -> a -> System w a
- cfoldM :: forall w c a. (Members w c, Get w c) => (a -> c -> System w a) -> a -> System w a
- cfoldM_ :: forall w c a. (Members w c, Get w c) => (a -> c -> System w a) -> a -> System w ()
- modify :: forall w c. (Get w c, Set w c) => Entity -> (c -> c) -> System w ()
- destroy :: forall w c. Destroy w c => Entity -> Proxy c -> System w ()
- exists :: forall w c. Get w c => Entity -> Proxy c -> System w Bool
- runSystem :: System w a -> w -> IO a
- runWith :: w -> System w a -> IO a
- runGC :: System w ()
- data EntityCounter
- newEntity :: (Set w c, Get w EntityCounter, Set w EntityCounter) => c -> System w Entity
- global :: Entity
- makeWorld :: String -> [Name] -> Q [Dec]
- makeWorldAndComponents :: String -> [Name] -> Q [Dec]
- asks :: MonadReader r m => (r -> a) -> m a
- ask :: MonadReader r m => m r
- liftIO :: MonadIO m => IO a -> m a
- lift :: (MonadTrans t, Monad m) => m a -> t m a
Documentation
module Data.Proxy
Core types
A System is a newtype around `ReaderT w IO a`, where w is the game world variable.
Systems mainly serve to
- Lift side effects into the IO Monad.
- Allow type-based lookup of a component's store through
getStore.
class Elem (Storage c) ~ c => Component c #
A component is defined by specifying how it is stored. The constraint ensures that stores and components are mapped one-to-one.
Instances
| Component () # | |
Defined in Apecs.Core | |
| Component Entity # | |
Defined in Apecs.Core | |
| Component EntityCounter # | |
Defined in Apecs.Util Associated Types type Storage EntityCounter :: * # | |
| Component c => Component (Maybe c) # | |
Defined in Apecs.Core | |
| Component c => Component (Identity c) # | Identity component/store. |
Defined in Apecs.Core | |
| Component c => Component (Filter c) # | |
Defined in Apecs.Core | |
| Component c => Component (Not c) # | |
Defined in Apecs.Core | |
| (Component ca, Component cb) => Component (Either ca cb) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1) => Component (t_0, t_1) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2) => Component (t_0, t_1, t_2) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2, Component t_3) => Component (t_0, t_1, t_2, t_3) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2, Component t_3, Component t_4) => Component (t_0, t_1, t_2, t_3, t_4) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2, Component t_3, Component t_4, Component t_5) => Component (t_0, t_1, t_2, t_3, t_4, t_5) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2, Component t_3, Component t_4, Component t_5, Component t_6) => Component (t_0, t_1, t_2, t_3, t_4, t_5, t_6) # | |
Defined in Apecs.Core | |
| (Component t_0, Component t_1, Component t_2, Component t_3, Component t_4, Component t_5, Component t_6, Component t_7) => Component (t_0, t_1, t_2, t_3, t_4, t_5, t_6, t_7) # | |
Defined in Apecs.Core | |
An Entity is just an integer, used to index into a component store.
In general, use newEntity, cmap, and component tags instead of manipulating these directly.
For performance reasons, negative values like (-1) are reserved for stores to represent special values, so avoid using these.
class Component c => Has w c where #
Has w c means that world w can produce a Storage c.
Minimal complete definition
Instances
| Has w Entity # | |
| Has w () # | |
Defined in Apecs.Core | |
| Has w c => Has w (Identity c) # | |
| Has w c => Has w (Filter c) # | |
| Has w c => Has w (Maybe c) # | |
| Has w c => Has w (Not c) # | |
| (Has w ca, Has w cb) => Has w (Either ca cb) # | |
| (Has w t_0, Has w t_1) => Has w (t_0, t_1) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2) => Has w (t_0, t_1, t_2) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2, Has w t_3) => Has w (t_0, t_1, t_2, t_3) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2, Has w t_3, Has w t_4) => Has w (t_0, t_1, t_2, t_3, t_4) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2, Has w t_3, Has w t_4, Has w t_5) => Has w (t_0, t_1, t_2, t_3, t_4, t_5) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2, Has w t_3, Has w t_4, Has w t_5, Has w t_6) => Has w (t_0, t_1, t_2, t_3, t_4, t_5, t_6) # | |
Defined in Apecs.Core | |
| (Has w t_0, Has w t_1, Has w t_2, Has w t_3, Has w t_4, Has w t_5, Has w t_6, Has w t_7) => Has w (t_0, t_1, t_2, t_3, t_4, t_5, t_6, t_7) # | |
Defined in Apecs.Core | |
Psuedocomponent indicating the absence of a.
Mainly used as e.g. cmap $ (a, Not b) -> c to iterate over entities with an a but no b.
Can also be used to delete components, like cmap $ a -> (Not :: Not a) to delete every a component.
Constructors
| Not |
type Destroy w c = (Has w c, ExplDestroy (Storage c)) #
type Members w c = (Has w c, ExplMembers (Storage c)) #
Stores
A map based on Data.Intmap.Strict. O(log(n)) for most operations.
Instances
| ExplMembers (Map c) # | |
Defined in Apecs.Stores | |
| ExplDestroy (Map c) # | |
Defined in Apecs.Stores Methods explDestroy :: Map c -> Int -> IO () # | |
| ExplSet (Map c) # | |
| ExplGet (Map c) # | |
| ExplInit (Map c) # | |
Defined in Apecs.Stores | |
| Cachable (Map s) # | |
Defined in Apecs.Stores | |
| type Elem (Map c) # | |
Defined in Apecs.Stores | |
A Unique contains zero or one component.
Writing to it overwrites both the previous component and its owner.
Its main purpose is to be a Map optimized for when only ever one component inhabits it.
Instances
| ExplMembers (Unique c) # | |
Defined in Apecs.Stores | |
| ExplDestroy (Unique c) # | |
Defined in Apecs.Stores Methods explDestroy :: Unique c -> Int -> IO () # | |
| ExplSet (Unique c) # | |
| ExplGet (Unique c) # | |
| ExplInit (Unique c) # | |
Defined in Apecs.Stores | |
| type Elem (Unique c) # | |
Defined in Apecs.Stores | |
A Global contains exactly one component.
The initial value is mempty from the component's Monoid instance.
When operating on a global, any entity arguments are ignored.
For example, we can get a global component with get 0 or get 1 or even get undefined.
A cache around another store. Caches store their members in a fixed-size vector, so operations run in O(1). Caches can provide huge performance boosts, especially for large numbers of components. The cache size is given as a type-level argument.
Note that iterating over a cache is linear in cache size, so sparsely populated caches might actually decrease performance. In general, the exact size of the cache does not matter as long as it reasonably approximates the number of components present.
The cache uses entity (-1) to internally represent missing entities, so be wary when manually manipulating entities.
Instances
| ExplMembers s => ExplMembers (Cache n s) # | |
Defined in Apecs.Stores | |
| ExplDestroy s => ExplDestroy (Cache n s) # | |
Defined in Apecs.Stores Methods explDestroy :: Cache n s -> Int -> IO () # | |
| ExplSet s => ExplSet (Cache n s) # | |
| ExplGet s => ExplGet (Cache n s) # | |
| (ExplInit s, KnownNat n, Cachable s) => ExplInit (Cache n s) # | |
Defined in Apecs.Stores | |
| (KnownNat n, Cachable s) => Cachable (Cache n s) # | |
Defined in Apecs.Stores | |
| type Elem (Cache n s) # | |
Defined in Apecs.Stores | |
Systems
set :: forall w c. Set w c => Entity -> c -> System w () #
Writes a component to a given entity. Will overwrite existing components. The type was originally 'Entity c -> c -> System w ()', but is relaxed to 'Entity e' so you don't always have to write 'set . cast'
getAll :: forall w c. (Get w c, Members w c) => System w [c] #
Get all components c.
Call as [(c,Entity)] to also read the entity index.
cmap :: forall w cx cy. (Get w cx, Members w cx, Set w cy) => (cx -> cy) -> System w () #
Maps a function over all entities with a cx, and writes their cy.
cmapM :: forall w cx cy. (Get w cx, Set w cy, Members w cx) => (cx -> System w cy) -> System w () #
Monadically iterates over all entites with a cx, and writes their cy.
cmapM_ :: forall w c a. (Get w c, Members w c) => (c -> System w a) -> System w () #
Monadically iterates over all entites with a cx
cfold :: forall w c a. (Members w c, Get w c) => (a -> c -> a) -> a -> System w a #
Fold over the game world; for example, cfold max (minBound :: Foo) will find the maximum value of Foo.
Strict in the accumulator.
cfoldM :: forall w c a. (Members w c, Get w c) => (a -> c -> System w a) -> a -> System w a #
Monadically fold over the game world. Strict in the accumulator.
cfoldM_ :: forall w c a. (Members w c, Get w c) => (a -> c -> System w a) -> a -> System w () #
Monadically fold over the game world. Strict in the accumulator.
modify :: forall w c. (Get w c, Set w c) => Entity -> (c -> c) -> System w () #
Applies a function, if possible.
destroy :: forall w c. Destroy w c => Entity -> Proxy c -> System w () #
Destroys component c for the given entity.
Note that c is a phantom argument, used only to convey the type of the entity to be destroyed.
exists :: forall w c. Get w c => Entity -> Proxy c -> System w Bool #
Returns whether the given entity has component c
Note that c is a phantom argument, used only to convey the type of the entity to be queried.
Other
data EntityCounter #
Component used by newEntity to track the number of issued entities.
Automatically added to any world created with makeWorld
Instances
| Eq EntityCounter # | |
Defined in Apecs.Util Methods (==) :: EntityCounter -> EntityCounter -> Bool # (/=) :: EntityCounter -> EntityCounter -> Bool # | |
| Show EntityCounter # | |
Defined in Apecs.Util Methods showsPrec :: Int -> EntityCounter -> ShowS # show :: EntityCounter -> String # showList :: [EntityCounter] -> ShowS # | |
| Semigroup EntityCounter # | |
Defined in Apecs.Util Methods (<>) :: EntityCounter -> EntityCounter -> EntityCounter # sconcat :: NonEmpty EntityCounter -> EntityCounter # stimes :: Integral b => b -> EntityCounter -> EntityCounter # | |
| Monoid EntityCounter # | |
Defined in Apecs.Util Methods mempty :: EntityCounter # mappend :: EntityCounter -> EntityCounter -> EntityCounter # mconcat :: [EntityCounter] -> EntityCounter # | |
| Component EntityCounter # | |
Defined in Apecs.Util Associated Types type Storage EntityCounter :: * # | |
| type Storage EntityCounter # | |
Defined in Apecs.Util | |
newEntity :: (Set w c, Get w EntityCounter, Set w EntityCounter) => c -> System w Entity #
Writes the given components to a new entity, and yields that entity. The return value is often ignored.
Convenience entity, for use in places where the entity value does not matter, i.e. a global store. Its value is -2, to avoid potential conflicts with caches, which reserve -1.
makeWorld :: String -> [Name] -> Q [Dec] #
makeWorld "WorldName" [''Component1, ''Component2, ...]
turns into
data WorldName = WorldName Component1 Component2 ... EntityCounter instance WorldName `Has` Component1 where ... instance WorldName `Has` Component2 where ... ... instance WorldName `Has` EntityCounter where ... initWorldName :: IO WorldName initWorldName = WorldName <$> initStore <*> initStore <*> ... <*> initStore
|
makeWorldAndComponents :: String -> [Name] -> Q [Dec] #
Same as makeWorld, but also makes a component instance:
Re-exports
Arguments
| :: MonadReader r m | |
| => (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
ask :: MonadReader r m => m r #
Retrieves the monad environment.
lift :: (MonadTrans t, Monad m) => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.