module Apecs.System where
import Control.Monad.Reader
import qualified Data.Vector.Unboxed as U
import Apecs.Core
runSystem :: System w a -> w -> IO a
runSystem sys = runReaderT (unSystem sys)
runWith :: w -> System w a -> IO a
runWith = flip runSystem
get :: forall w c. Has w c => Entity -> System w c
get (Entity ety) = do
s :: Storage c <- getStore
liftIO$ explGet s ety
set :: forall w c. Has w c => Entity -> c -> System w ()
set (Entity ety) x = do
s :: Storage c <- getStore
liftIO$ explSet s ety x
exists :: forall w c. Has w c => Entity -> c -> System w Bool
exists (Entity ety) ~_ = do
s :: Storage c <- getStore
liftIO$ explExists s ety
cmap :: forall world cx cy. (Has world cx, Has world cy)
=> (cx -> cy) -> System world ()
cmap f = do
sx :: Storage cx <- getStore
sy :: Storage cy <- getStore
liftIO$ do
sl <- liftIO$ explMembers sx
U.forM_ sl $ \ e -> do
r <- explGet sx e
explSet sy e (f r)
cmapM :: forall world c a. Has world c
=> (c -> System world a) -> System world [a]
cmapM sys = do
s :: Storage c <- getStore
sl <- liftIO$ explMembers s
forM (U.toList sl) $ \ ety -> do
x <- liftIO$ explGet s ety
sys x
cmapM_ :: forall world c a. Has world c
=> (c -> System world a) -> System world ()
cmapM_ sys = do
s :: Storage c <- getStore
sl <- liftIO$ explMembers s
U.forM_ sl $ \ ety -> do
x <- liftIO$ explGet s ety
sys x
destroy :: forall w c. Has w c => Entity -> c -> System w ()
destroy (Entity ety) ~_ = do
s :: Storage c <- getStore
liftIO$ explDestroy s ety
modify :: forall w c. Has w c => Entity -> (c -> c) -> System w ()
modify (Entity ety) f = do
s :: Storage c <- getStore
liftIO$ do
x <- explGet s ety
explSet s ety (f x)
count :: forall w c. Has w c => c -> System w Int
count ~_ = do
s :: Storage c <- getStore
sl <- liftIO$ explMembers s
return $ U.length sl