module Apecs.System where
import Control.Monad.Reader
import qualified Data.Vector.Unboxed as U
import Apecs.Types
runSystem :: System w a -> w -> IO a
runSystem sys = runReaderT (unSystem sys)
runWith :: w -> System w a -> IO a
runWith = flip runSystem
owners :: forall w c. (Has w c, HasMembers (Storage c)) => System w (Slice c)
owners = do s :: Storage c <- getStore
liftIO$ Slice <$> explMembers s
exists :: forall w c. (Has w c, HasMembers (Storage c)) => Entity c -> System w Bool
exists (Entity n) = do s :: Storage c <- getStore
liftIO$ explExists s n
destroy :: forall w c. (Has w c, HasMembers (Storage c)) => Entity c -> System w ()
destroy (Entity n) = do s :: Storage c <- getStore
liftIO$ explDestroy s n
resetStore :: forall w c p. (Has w c, HasMembers (Storage c)) => p c -> System w ()
resetStore _ = do s :: Storage c <- getStore
liftIO$ explReset s
get :: forall w c. (Store (Storage c), Has w c) => Entity c -> System w (Safe c)
get (Entity ety) = do s :: Storage c <- getStore
liftIO$ Safe <$> explGet s ety
set :: forall w c e. (Store (Storage c), Stores (Storage c) ~ c, Has w c) => Entity e -> c -> System w ()
set (Entity ety) x = do
s :: Storage c <- getStore
liftIO$ explSet s ety x
setOrDelete :: forall w c. (IsRuntime c, Has w c) => Entity c -> Safe c -> System w ()
setOrDelete (Entity ety) (Safe c) = do
s :: Storage c <- getStore
liftIO$ explSetMaybe s ety c
modify :: forall w c. (IsRuntime c, Has w c) => Entity c -> (c -> c) -> System w ()
modify (Entity ety) f = do
s :: Storage c <- getStore
liftIO$ explModify s ety f
imapM_ :: forall w c. (Has w c, HasMembers (Storage c))
=> (Entity c -> System w ()) -> System w ()
imapM_ sys = do s :: Storage c <- getStore
explImapM_ s (sys . Entity)
imapM :: forall w c a. (Has w c, HasMembers (Storage c))
=> (Entity c -> System w a) -> System w [a]
imapM sys = do s :: Storage c <- getStore
explImapM s (sys . Entity)
cmap :: forall world c. (IsRuntime c, Has world c) => (c -> c) -> System world ()
cmap f = do s :: Storage c <- getStore
liftIO$ explCmap s f
cmapM_ :: forall w c. (Has w c, IsRuntime c)
=> (c -> System w ()) -> System w ()
cmapM_ sys = do s :: Storage c <- getStore
explCmapM_ s sys
cimapM_ :: forall w c. (Has w c, IsRuntime c)
=> ((Entity c, c) -> System w ()) -> System w ()
cimapM_ sys = do s :: Storage c <- getStore
explCimapM_ s (\(e,c) -> sys (Entity e,c))
cmapM :: forall w c a. (Has w c, IsRuntime c)
=> (c -> System w a) -> System w [a]
cmapM sys = do s :: Storage c <- getStore
explCmapM s sys
cimapM :: forall w c a. (Has w c, IsRuntime c)
=> ((Entity c, c) -> System w a) -> System w [a]
cimapM sys = do s :: Storage c <- getStore
explCimapM s (\(e,c) -> sys (Entity e,c))
cmap' :: forall world c. (Has world c, IsRuntime c)
=> (c -> Safe c) -> System world ()
cmap' f = do s :: Storage c <- getStore
liftIO$ do sl <- explMembers s
U.forM_ sl $ \e -> do
r <- explGetUnsafe s e
explSetMaybe s e (getSafe . f $ r)
rmap :: forall world r w. (Has world w, Has world r, IsRuntime w, IsRuntime r)
=> (r -> w) -> System world ()
rmap f = do sr :: Storage r <- getStore
sc :: Storage w <- getStore
liftIO$ do sl <- explMembers sr
U.forM_ sl $ \ e -> do
r <- explGetUnsafe sr e
explSet sc e (f r)
rmap' :: forall world r w. (Has world w, Has world r, Store (Storage w), IsRuntime r)
=> (r -> Safe w) -> System world ()
rmap' f = do sr :: Storage r <- getStore
sw :: Storage w <- getStore
liftIO$ do sl <- explMembers sr
U.forM_ sl $ \ e -> do
r <- explGetUnsafe sr e
explSetMaybe sw e (getSafe $ f r)
wmap :: forall world r w. (Has world w, Has world r, IsRuntime w, IsRuntime r)
=> (Safe r -> w) -> System world ()
wmap f = do sr :: Storage r <- getStore
sw :: Storage w <- getStore
liftIO$ do sl <- explMembers sr
U.forM_ sl $ \ e -> do
r <- explGet sr e
explSet sw e (f . Safe $ r)
wmap' :: forall world r w. (Has world w, Has world r, Store (Storage w), IsRuntime r)
=> (Safe r -> Safe w) -> System world ()
wmap' f = do sr :: Storage r <- getStore
sw :: Storage w <- getStore
liftIO$ do sl <- explMembers sr
U.forM_ sl $ \ e -> do
r <- explGet sr e
explSetMaybe sw e (getSafe . f . Safe $ r)
slice :: forall w c q. (Query q (Storage c), Has w c) => q -> System w (Slice c)
slice q = do
s :: Storage c <- getStore
liftIO$ Slice <$> explSlice s q
readGlobal :: forall w c. (Has w c, GlobalRW (Storage c) c) => System w c
readGlobal = do s :: Storage c <- getStore
liftIO$ explGlobalRead s
writeGlobal :: forall w c. (Has w c, GlobalRW (Storage c) c) => c -> System w ()
writeGlobal c = do s :: Storage c <- getStore
liftIO$ explGlobalWrite s c
modifyGlobal :: forall w c. (Has w c, GlobalRW (Storage c) c) => (c -> c) -> System w ()
modifyGlobal f = do s :: Storage c <- getStore
liftIO$ explGlobalModify s f