@@ -15,6 +15,7 @@ module Aztecs.Internal (AztecsT (..), runAztecsT_) where
1515
1616import Aztecs.ECS.Access.Internal
1717import qualified Aztecs.ECS.Access.Internal as A
18+ import Aztecs.ECS.Bundle
1819import Aztecs.ECS.Class
1920import Aztecs.ECS.Commands
2021import Aztecs.ECS.Executor
@@ -29,13 +30,16 @@ import qualified Aztecs.ECS.Scheduler as Scheduler
2930import Aztecs.ECS.System
3031import Aztecs.Entities
3132import qualified Aztecs.Entities as E
32- import Aztecs.World (ComponentStorage , bundle )
33+ import Aztecs.World (ComponentStorage )
3334import qualified Aztecs.World as W
3435import Control.Monad.Identity
3536import Control.Monad.Primitive
3637import Control.Monad.State.Strict
38+ import qualified Data.IntMap.Strict as IntMap
39+ import qualified Data.Map.Strict as Map
3740import Data.Maybe
3841import qualified Data.Set as Set
42+ import qualified Data.SparseSet.Strict as S
3943import qualified Data.SparseSet.Strict.Mutable as MS
4044import Data.Typeable
4145import Data.Word
@@ -49,20 +53,17 @@ instance MonadTrans (AztecsT cs) where
4953
5054instance (PrimMonad m ) => ECS (AztecsT cs m ) where
5155 type Entity (AztecsT cs m ) = E. Entity
52- type Bundle (AztecsT cs m ) = W. Bundle cs m
5356 type Components (AztecsT cs m ) = cs
5457 type Task (AztecsT cs m ) = (Commands (AztecsT cs ) m )
5558
56- spawn b = AztecsT $ do
57- w <- get
58- (e, w') <- lift $ W. spawn b w
59- put w'
59+ spawn b = do
60+ w <- AztecsT $ get
61+ let (e, counter) = mkEntityWithCounter (W. worldEntities w)
62+ AztecsT $ put w {W. worldEntities = counter}
63+ runBundle b e
6064 return e
6165 {-# INLINE spawn #-}
62- insert e b = AztecsT $ do
63- w <- get
64- w' <- lift $ W. insert e b w
65- put w'
66+ insert e b = runBundle b e
6667 {-# INLINE insert #-}
6768 remove e = AztecsT $ do
6869 w <- get
@@ -73,10 +74,27 @@ instance (PrimMonad m) => ECS (AztecsT cs m) where
7374 {-# INLINE task #-}
7475
7576instance
76- (PrimMonad m , Typeable c , AdjustM m (ComponentStorage (PrimState m )) c cs ) =>
77+ ( PrimMonad m ,
78+ Typeable c ,
79+ AdjustM m (ComponentStorage (PrimState m )) c cs
80+ ) =>
7781 Bundleable c (AztecsT cs m )
7882 where
79- bundle = W. bundle
83+ bundle c = Bundle $ \ entity -> do
84+ w <- AztecsT $ get
85+ let entityIdx = fromIntegral (entityIndex entity)
86+ componentType = typeOf c
87+ go s = do
88+ s' <- S. freeze s
89+ S. thaw $ S. insert (entityIndex entity) c s'
90+ cs <- lift . HS. adjustM go $ W. worldComponents w
91+ let entityComponents' =
92+ IntMap. insertWith
93+ Map. union
94+ entityIdx
95+ (Map. singleton componentType (W. removeComponent' @ m @ c entity))
96+ (W. worldEntityComponents w)
97+ AztecsT $ put w {W. worldComponents = cs, W. worldEntityComponents = entityComponents'}
8098
8199runAztecsT_ :: (Monad m ) => AztecsT cs m a -> W. World m cs -> m a
82100runAztecsT_ (AztecsT m) = evalStateT m
0 commit comments