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 => System w (Slice c)
owners = do s :: Storage c <- getStore
liftIO$ Slice <$> explMembers s
exists :: forall w c. Has w 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 => Entity c -> System w ()
destroy (Entity n) = do s :: Storage c <- getStore
liftIO$ explDestroy s n
resetStore :: forall w c p. Has w c => p c -> System w ()
resetStore _ = do s :: Storage c <- getStore
liftIO$ explReset s
get :: forall w 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. Has w c => Entity e -> c -> System w ()
set (Entity ety) x = do
s :: Storage c <- getStore
liftIO$ explSet s ety x
set' :: forall w c. Has w c => Entity c -> Safe c -> System w ()
set' (Entity ety) (Safe c) = do
s :: Storage c <- getStore
liftIO$ explSetMaybe s ety c
modify :: forall w 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 => (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
=> (Entity c -> System w a) -> System w [a]
imapM sys = do s :: Storage c <- getStore
explImapM s (sys . Entity)
cmap :: forall world 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 => (c -> System w ()) -> System w ()
cmapM_ sys = do s :: Storage c <- getStore
explCmapM_ s sys
cimapM_ :: forall w c. Has w 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 => (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 => ((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 => (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)
=> (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 r), Store (Storage w))
=> (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, Store (Storage r), Store (Storage w))
=> (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 r), Store (Storage w))
=> (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)
readGlobal :: forall w c. (Has w c, GlobalStore (Storage c)) => System w c
readGlobal = do s :: Storage c <- getStore
liftIO$ explGet s 0
writeGlobal :: forall w c. (Has w c, GlobalStore (Storage c)) => c -> System w ()
writeGlobal c = do s :: Storage c <- getStore
liftIO$ explSet s 0 c
modifyGlobal :: forall w c. (Has w c, GlobalStore (Storage c)) => (c -> c) -> System w ()
modifyGlobal f = do s :: Storage c <- getStore
liftIO$ explModify s 0 f