Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module forms the apecs Prelude. It selectively re-exports the user-facing functions from the submodules.
- newtype System w a = System {}
- class (Elem (Storage c) ~ c, Store (Storage c)) => Component c where
- type Storage c
- newtype Entity = Entity Int
- class Component c => Has w c where
- data Not a = Not
- data Map c
- data Unique c
- data Global c
- data Cache (n :: Nat) s
- initStore :: Store s => IO s
- get :: forall w c. Has w c => Entity -> System w c
- set :: forall w c. Has w c => Entity -> c -> System w ()
- getAll :: forall world c. Has world c => System world [c]
- cmap :: forall world cx cy. (Has world cx, Has world cy) => (cx -> cy) -> System world ()
- cmapM :: forall world c a. Has world c => (c -> System world a) -> System world [a]
- cmapM_ :: forall world c a. Has world c => (c -> System world a) -> System world ()
- modify :: forall w c. Has w c => Entity -> (c -> c) -> System w ()
- destroy :: forall w c. Has w c => Entity -> c -> System w ()
- exists :: forall w c. Has w c => Entity -> c -> System w Bool
- runSystem :: System w a -> w -> IO a
- runWith :: w -> System w a -> IO a
- runGC :: System w ()
- data EntityCounter
- newEntity :: (Store (Storage c), Has w c, Has w EntityCounter) => c -> System w Entity
- global :: Entity
- proxy :: forall t. t
- makeWorld :: String -> [Name] -> Q [Dec]
- asks :: MonadReader r m => (r -> a) -> m a
- ask :: MonadReader r m => m r
- liftIO :: MonadIO m => forall a. IO a -> m a
- lift :: MonadTrans t => forall (m :: * -> *) a. Monad m => m a -> t m a
Core types
A system is a newtype around `ReaderT w IO a`, where w
is the game world variable.
class (Elem (Storage c) ~ c, Store (Storage c)) => Component c Source #
A component is defined by the type of its storage The storage in turn supplies runtime types for the component. For the component to be valid, its Storage must be an instance of Store.
An Entity is really just an Int in a newtype, used to index into a component store.
class Component c => Has w c where Source #
A world Has
a component if it can produce its Storage
Has w Entity Source # | |
Has w c => Has w (Identity c) Source # | |
Has w c => Has w (Filter c) Source # | |
Has w c => Has w (Maybe c) Source # | |
Has w c => Has w (Not c) Source # | |
(Has w p, Has w q) => Has w (Either p q) Source # | |
(Has w t_0, Has w t_1) => Has w (t_0, t_1) Source # | |
(Has w t_0, Has w t_1, Has w t_2) => Has w (t_0, t_1, t_2) Source # | |
(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) Source # | |
(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) Source # | |
(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) Source # | |
(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) Source # | |
(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) Source # | |
Psuedocomponent indicating the absence of a
.
Stores
A map based on Data.Intmap.Strict
. O(log(n)) for most operations.
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.
A Global contains exactly one component.
Initialized with mempty
The store will return true for every existence check, but only ever gives (-1) as its inhabitant.
The entity argument is ignored when setting/getting a global.
data Cache (n :: Nat) s Source #
A cache around another store. Note that iterating over a cache is linear in cache size, so sparsely populated caches might actually decrease performance.
Systems
set :: forall w c. Has w c => Entity -> c -> System w () Source #
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 world c. Has world c => System world [c] Source #
Get all components c
.
Call as [(c,Entity)]
to read the entity/index.
cmap :: forall world cx cy. (Has world cx, Has world cy) => (cx -> cy) -> System world () Source #
Maps a function over all entities with a cx
, and writes their cy
cmapM :: forall world c a. Has world c => (c -> System world a) -> System world [a] Source #
Monadically iterates over all entites with a cx
cmapM_ :: forall world c a. Has world c => (c -> System world a) -> System world () Source #
Monadically iterates over all entites with a cx
modify :: forall w c. Has w c => Entity -> (c -> c) -> System w () Source #
Applies a function, if possible.
destroy :: forall w c. Has w c => Entity -> c -> System w () Source #
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. Has w c => Entity -> c -> System w Bool Source #
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 Source #
Component used by newEntity to track the number of issued entities.
Automatically added to any world created with makeWorld
newEntity :: (Store (Storage c), Has w c, Has w EntityCounter) => c -> System w Entity Source #
Writes the given components to a new entity, and yields that entity. The return value is often ignored.
Convenience entity (-1), used in places where the exact entity value does not matter, i.e. a global store.
makeWorld :: String -> [Name] -> Q [Dec] Source #
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
|
Re-exports
:: 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.