Leviathan  0.8.0.0
Leviathan game engine
Leviathan::StateHolder< StateT > Class Template Reference

Holds state objects of type for quick access by ObjectID. More...

#include <GameWorld.h>

Public Member Functions

 StateHolder ()
 
 ~StateHolder ()
 
template<class ComponentT >
bool CreateStateIfChanged (ObjectID id, const ComponentT &component, int ticknumber)
 Creates a new state for entity's component if it has changed. More...
 
auto GetNumberOfEntitiesWithStates () const
 Returns the number of entities that have states. More...
 
ObjectsComponentStates< StateT > const * GetEntityStates (ObjectID id) const
 Returns a pointer to entity's states if they exist. More...
 

Protected Member Functions

template<typename... Args>
StateT * _CreateNewState (Args &&... args)
 
void _DestroyStateObject (StateT *state)
 

Detailed Description

template<class StateT>
class Leviathan::StateHolder< StateT >

Holds state objects of type for quick access by ObjectID.

Todo:
The GameWorld needs to notify this when ObjectID is deleted

Definition at line 34 of file GameWorld.h.

Constructor & Destructor Documentation

◆ StateHolder()

template<class StateT>
Leviathan::StateHolder< StateT >::StateHolder ( )
inline

Definition at line 177 of file StateHolder.h.

177  {
178 
179  }

◆ ~StateHolder()

template<class StateT>
Leviathan::StateHolder< StateT >::~StateHolder ( )
inline

Definition at line 181 of file StateHolder.h.

181  {
182  // Both of the pools are released here so the destructor of ObjectsComponentStates
183  // doesn't need to notify us of the states it held
184  }

Member Function Documentation

◆ _CreateNewState()

template<class StateT>
template<typename... Args>
StateT* Leviathan::StateHolder< StateT >::_CreateNewState ( Args &&...  args)
inlineprotected

Definition at line 235 of file StateHolder.h.

235  {
236 
237  return StatePool.ConstructNew(std::forward<Args>(args)...);
238  }
ElementType * ConstructNew(Args &&... args)
Constructs a new component of the held type for entity.
Definition: ObjectPool.h:31

◆ _DestroyStateObject()

template<class StateT>
void Leviathan::StateHolder< StateT >::_DestroyStateObject ( StateT *  state)
inlineprotected

Definition at line 240 of file StateHolder.h.

240  {
241 
242  StatePool.Destroy(state);
243  }
void Destroy(ElementType *element)
Destroys a created element.
Definition: ObjectPool.h:54

◆ CreateStateIfChanged()

template<class StateT>
template<class ComponentT >
bool Leviathan::StateHolder< StateT >::CreateStateIfChanged ( ObjectID  id,
const ComponentT &  component,
int  ticknumber 
)
inline

Creates a new state for entity's component if it has changed.

Returns
True if a new state was created

Definition at line 189 of file StateHolder.h.

191  {
192  ObjectsComponentStates<StateT>* entityStates = StateObjects.Find(id);
193 
194  if(!entityStates){
195 
196  entityStates = StateObjects.ConstructNew(id);
197  }
198 
199  // Get latest state to compare current values against //
200  StateT* latestState = entityStates->GetNewest();
201 
202  // If empty always create //
203  if(!latestState){
204 
205  StateT* newState = _CreateNewState(ticknumber, component);
206  entityStates->Append(newState, *this);
207  return true;
208  }
209 
210  // Check is the latest state still correct //
211  if(latestState->DoesMatchState(component))
212  return false;
213 
214  // Create a new state //
215  StateT* newState = _CreateNewState(ticknumber, component);
216  entityStates->Append(newState, *this);
217  return true;
218  }
StateT * _CreateNewState(Args &&... args)
Definition: StateHolder.h:235

◆ GetEntityStates()

template<class StateT>
ObjectsComponentStates<StateT> const* Leviathan::StateHolder< StateT >::GetEntityStates ( ObjectID  id) const
inline

Returns a pointer to entity's states if they exist.

Definition at line 227 of file StateHolder.h.

227  {
228 
229  return StateObjects.Find(id);
230  }

◆ GetNumberOfEntitiesWithStates()

template<class StateT>
auto Leviathan::StateHolder< StateT >::GetNumberOfEntitiesWithStates ( ) const
inline

Returns the number of entities that have states.

Definition at line 221 of file StateHolder.h.

221  {
222 
223  return StateObjects.GetObjectCount();
224  }

The documentation for this class was generated from the following files: