apecs 0.7.0 → 0.7.1
raw patch · 5 files changed
+43/−45 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Apecs: getAll :: forall w m c. (Get w m c, Members w m c) => SystemT w m [c]
- Apecs.System: count :: forall w m c. Members w m c => Proxy c -> SystemT w m Int
- Apecs.System: getAll :: forall w m c. (Get w m c, Members w m c) => SystemT w m [c]
+ Apecs: ($=) :: forall w m c. Set w m c => Entity -> c -> SystemT w m ()
+ Apecs: ($~) :: forall w m c. (Get w m c, Set w m c) => Entity -> (c -> c) -> SystemT w m ()
+ Apecs: infixr 2 $~
+ Apecs.System: ($=) :: forall w m c. Set w m c => Entity -> c -> SystemT w m ()
+ Apecs.System: ($~) :: forall w m c. (Get w m c, Set w m c) => Entity -> (c -> c) -> SystemT w m ()
+ Apecs.System: infixr 2 $~
Files
- CHANGELOG.md +7/−1
- apecs.cabal +1/−1
- src/Apecs.hs +4/−3
- src/Apecs/System.hs +29/−40
- test/Main.hs +2/−0
CHANGELOG.md view
@@ -1,4 +1,10 @@-## [0.6.1]+## [0.7.1]+## Added+- `$=` and `$~` operators as synonyms for `set` and `get` respectively+## Removed+- `getAll` and `count`, which were made redundant by `cfold`.++## [0.7.0] ### Added - The `Reactive` store and module is a redesign of the `Register` store, and provides a more general solution for 'stores that perform additional actions when written to'. - The `Apecs.Stores.Extra` submodule, which contains the `Pushdown` and `ReadOnly` stores. `Pushdown` adds pushdown semantics to stores, and `ReadOnly` hides the `ExplSet` instances of whatever it wraps.
apecs.cabal view
@@ -1,5 +1,5 @@ name: apecs-version: 0.7.0+version: 0.7.1 homepage: https://github.com/jonascarpay/apecs#readme license: BSD3 license-file: LICENSE
src/Apecs.hs view
@@ -13,10 +13,11 @@ explInit, -- * Systems- get, set, getAll,- cmap, cmapM, cmapM_,+ get, set, ($=),+ destroy, exists,+ modify, ($~), + cmap, cmapM, cmapM_, cfold, cfoldM, cfoldM_,- modify, destroy, exists, -- * Other runSystem, runWith,
src/Apecs/System.hs view
@@ -13,31 +13,34 @@ import Apecs.Core import Apecs.Components () --- | Run a system with a game world+-- | Run a system in a game world {-# INLINE runSystem #-} runSystem :: SystemT w m a -> w -> m a runSystem sys = runReaderT (unSystem sys) --- | Run a system with a game world+-- | Run a system in a game world {-# INLINE runWith #-} runWith :: w -> SystemT w m a -> m a runWith = flip runSystem +-- | Read a Component {-# INLINE get #-} get :: forall w m c. Get w m c => Entity -> SystemT w m c get (Entity ety) = do s :: Storage c <- getStore lift$ explGet s ety --- | Writes a component to a given entity. Will overwrite existing components.--- The type was originally 'Entity c -> c -> SystemT w m ()', but is relaxed to 'Entity e'--- so you don't always have to write 'set . cast'+-- | Writes a Component to a given Entity. Will overwrite existing Components. {-# INLINE set #-}-set :: forall w m c. Set w m c => Entity -> c -> SystemT w m ()+set, ($=) :: forall w m c. Set w m c => Entity -> c -> SystemT w m () set (Entity ety) x = do s :: Storage c <- getStore lift$ explSet s ety x +-- | @set@ operator+($=) = set+infixr 2 $=+ -- | Returns whether the given entity has component @c@ {-# INLINE exists #-} exists :: forall w m c. Get w m c => Entity -> Proxy c -> SystemT w m Bool@@ -45,6 +48,26 @@ s :: Storage c <- getStore lift$ explExists s ety +-- | Destroys component @c@ for the given entity.+{-# INLINE destroy #-}+destroy :: forall w m c. Destroy w m c => Entity -> Proxy c -> SystemT w m ()+destroy (Entity ety) ~_ = do+ s :: Storage c <- getStore+ lift$ explDestroy s ety++-- | Applies a function, if possible.+{-# INLINE modify #-}+modify, ($~) :: forall w m c. (Get w m c, Set w m c) => Entity -> (c -> c) -> SystemT w m ()+modify (Entity ety) f = do+ s :: Storage c <- getStore+ lift$ do+ x <- explGet s ety+ explSet s ety (f x)++-- | @modify@ operator+($~) = modify+infixr 2 $~+ -- | Maps a function over all entities with a @cx@, and writes their @cy@. {-# INLINE cmap #-} cmap :: forall w m cx cy. (Get w m cx, Members w m cx, Set w m cy)@@ -134,37 +157,3 @@ s :: Storage c <- getStore sl <- lift$ explMembers s U.foldM'_ (\a e -> lift (explGet s e) >>= sys a) a0 sl---- | Get all components @c@.--- Call as @[(c,Entity)]@ to also read the entity index.-{-# INLINE getAll #-}-getAll :: forall w m c. (Get w m c, Members w m c)- => SystemT w m [c]-getAll = do- s :: Storage c <- getStore- sl <- lift$ explMembers s- forM (U.toList sl) $ lift . explGet s---- | Destroys component @c@ for the given entity.-{-# INLINE destroy #-}-destroy :: forall w m c. Destroy w m c => Entity -> Proxy c -> SystemT w m ()-destroy (Entity ety) ~_ = do- s :: Storage c <- getStore- lift$ explDestroy s ety---- | Applies a function, if possible.-{-# INLINE modify #-}-modify :: forall w m c. (Get w m c, Set w m c) => Entity -> (c -> c) -> SystemT w m ()-modify (Entity ety) f = do- s :: Storage c <- getStore- lift$ do- x <- explGet s ety- explSet s ety (f x)---- | Counts the number of entities with a @c@-{-# INLINE count #-}-count :: forall w m c. Members w m c => Proxy c -> SystemT w m Int-count ~_ = do- s :: Storage c <- getStore- sl <- lift$ explMembers s- return $ U.length sl
test/Main.hs view
@@ -111,6 +111,7 @@ prop_setGetTuple = genericSetGet initTuples (undefined :: (T1,T2,T3)) prop_setSetTuple = genericSetSet initTuples (undefined :: (T1,T2,T3)) +-- Tests Reactive store properties newtype TestBool = TestBool Bool deriving (Eq, Show, Bounded, Enum, Arbitrary) instance Component TestBool where type Storage TestBool = Reactive (EnumMap TestBool) (Map TestBool) @@ -123,6 +124,7 @@ forM_ writes $ uncurry set forM_ deletes $ flip destroy (Proxy @TestBool) + let getAll = cfold (flip (:)) [] :: SystemT ReactiveWld IO [(TestBool, Entity)] et <- fmap snd . filter ((== TestBool True ) . fst) <$> getAll ef <- fmap snd . filter ((== TestBool False) . fst) <$> getAll