{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Aztecs.ECS.World.Components
( ComponentID (..),
Components (..),
empty,
lookup,
insert,
insert',
)
where
import Aztecs.ECS.Component
import Aztecs.ECS.World.Components.Internal (Components (..))
import qualified Data.Map.Strict as Map
import Data.Typeable
import Prelude hiding (lookup)
empty :: Components
empty :: Components
empty =
Components
{ componentIds :: Map TypeRep ComponentID
componentIds = Map TypeRep ComponentID
forall a. Monoid a => a
mempty,
nextComponentId :: ComponentID
nextComponentId = Int -> ComponentID
ComponentID Int
0
}
lookup :: forall a. (Typeable a) => Components -> Maybe ComponentID
lookup :: forall a. Typeable a => Components -> Maybe ComponentID
lookup Components
cs = TypeRep -> Map TypeRep ComponentID -> Maybe ComponentID
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Proxy a -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) (Components -> Map TypeRep ComponentID
componentIds Components
cs)
insert :: forall a m. (Component m a) => Components -> (ComponentID, Components)
insert :: forall a (m :: * -> *).
Component m a =>
Components -> (ComponentID, Components)
insert Components
cs = case forall a. Typeable a => Components -> Maybe ComponentID
lookup @a Components
cs of
Just ComponentID
cId -> (ComponentID
cId, Components
cs)
Maybe ComponentID
Nothing -> forall a (m :: * -> *).
Component m a =>
Components -> (ComponentID, Components)
insert' @a @m Components
cs
insert' :: forall c m. (Component m c) => Components -> (ComponentID, Components)
insert' :: forall a (m :: * -> *).
Component m a =>
Components -> (ComponentID, Components)
insert' Components
cs =
let !cId :: ComponentID
cId = Components -> ComponentID
nextComponentId Components
cs
in ( ComponentID
cId,
Components
cs
{ componentIds = Map.insert (typeOf (Proxy @c)) cId (componentIds cs),
nextComponentId = ComponentID (unComponentId cId + 1)
}
)