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, float time)
 Creates a new state for entity's component if it has changed. More...
 
void DeserializeState (ObjectID id, float time, sf::Packet &data, float referencestatestime)
 Deserializes a state for entity's component from an archive. More...
 
template<class ComponentT >
void DeserializeAndApplyState (ObjectID id, ComponentT &component, float time, sf::Packet &data, float referencestatestime)
 Deserializes a state for entity's component from an archive and applies it if it is the newest. More...
 
template<class ComponentT >
StateT CreateStateForSending (const ComponentT &component, float time) const
 Creates a state object for sending. 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

StateT * _DeserializeState (ObjectsComponentStates< StateT > *entityStates, ObjectID id, float time, sf::Packet &data, float referencestatestime)
 
ObjectsComponentStates< StateT > * GetStateFor (ObjectID id)
 
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 31 of file GameWorld.h.

Constructor & Destructor Documentation

◆ StateHolder()

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

Definition at line 167 of file StateHolder.h.

167 {}

◆ ~StateHolder()

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

Definition at line 169 of file StateHolder.h.

170  {
171  // Both of the pools are released here so the destructor of ObjectsComponentStates
172  // doesn't need to notify us of the states it held
173  }

Member Function Documentation

◆ _CreateNewState()

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

Definition at line 307 of file StateHolder.h.

308  {
309  return StatePool.ConstructNew(std::forward<Args>(args)...);
310  }
ElementType * ConstructNew(Args &&... args)
Constructs a new component of the held type for entity.
Definition: ObjectPool.h:40

◆ _DeserializeState()

template<class StateT>
StateT* Leviathan::StateHolder< StateT >::_DeserializeState ( ObjectsComponentStates< StateT > *  entityStates,
ObjectID  id,
float  time,
sf::Packet &  data,
float  referencestatestime 
)
inlineprotected

Definition at line 265 of file StateHolder.h.

267  {
268  StateT* reference = nullptr;
269 
270  if(referencestatestime >= 0.f) {
271  reference = entityStates->GetState(referencestatestime);
272 
273  if(!reference) {
274 
275  LOG_WARNING("StateHolder: DeserializeState: can't find reference tick: " +
276  std::to_string(referencestatestime) +
277  " for entity: " + std::to_string(id));
278 
279  reference = entityStates->GetNewest();
280  }
281  }
282 
283  // Create a new state //
284  StateT* newState = _CreateNewState(reference, data);
285 
286  // This can't be deserialized from data so we forward it
287  newState->StateTime = time;
288 
289  // TODO: do we need to check whether the states already contain a state for this time
290  entityStates->Append(newState, *this);
291  return newState;
292  }
#define LOG_WARNING(x)
Definition: Define.h:91
StateT * _CreateNewState(Args &&... args)
Definition: StateHolder.h:307

◆ _DestroyStateObject()

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

Definition at line 312 of file StateHolder.h.

313  {
314  StatePool.Destroy(state);
315  }
void Destroy(ElementType *element)
Destroys a created element.
Definition: ObjectPool.h:62

◆ CreateStateForSending()

template<class StateT>
template<class ComponentT >
StateT Leviathan::StateHolder< StateT >::CreateStateForSending ( const ComponentT &  component,
float  time 
) const
inline

Creates a state object for sending.

Definition at line 246 of file StateHolder.h.

247  {
248  return StateT(time, component);
249  }

◆ CreateStateIfChanged()

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

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

Returns
True if a new state was created

Definition at line 178 of file StateHolder.h.

179  {
180  auto entityStates = GetStateFor(id);
181 
182  // Get latest state to compare current values against //
183  StateT* latestState = entityStates->GetNewest();
184 
185  // If empty always create //
186  if(!latestState) {
187 
188  StateT* newState = _CreateNewState(time, component);
189  entityStates->Append(newState, *this);
190  return true;
191  }
192 
193  // Check is the latest state still correct //
194  if(latestState->DoesMatchState(component))
195  return false;
196 
197  // Create a new state //
198  StateT* newState = _CreateNewState(time, component);
199  entityStates->Append(newState, *this);
200  return true;
201  }
ObjectsComponentStates< StateT > * GetStateFor(ObjectID id)
Definition: StateHolder.h:294
StateT * _CreateNewState(Args &&... args)
Definition: StateHolder.h:307

◆ DeserializeAndApplyState()

template<class StateT>
template<class ComponentT >
void Leviathan::StateHolder< StateT >::DeserializeAndApplyState ( ObjectID  id,
ComponentT &  component,
float  time,
sf::Packet &  data,
float  referencestatestime 
)
inline

Deserializes a state for entity's component from an archive and applies it if it is the newest.

Definition at line 214 of file StateHolder.h.

216  {
217  auto entityStates = GetStateFor(id);
218 
219  // Deserialize
220  auto* deserialized =
221  this->_DeserializeState(entityStates, id, time, data, referencestatestime);
222 
223  if(!deserialized) {
224  LOG_ERROR("StateHolder: failed to deserialize state");
225  return;
226  }
227 
228  // And apply it if it is newest
229  auto* newest = entityStates->GetNewest();
230 
231  if(deserialized == newest) {
232 
233  component.ApplyState(*newest);
234  } else {
235  float newestNumber = newest ? newest->StateTime : -1;
236 
237  LOG_WARNING("StateHolder: DeserializeAndApplyState: received not the newest "
238  "packet, received: " +
239  std::to_string(deserialized->StateTime) +
240  ", newest: " + std::to_string(newestNumber));
241  }
242  }
#define LOG_ERROR(x)
Definition: Define.h:92
#define LOG_WARNING(x)
Definition: Define.h:91
StateT * _DeserializeState(ObjectsComponentStates< StateT > *entityStates, ObjectID id, float time, sf::Packet &data, float referencestatestime)
Definition: StateHolder.h:265
ObjectsComponentStates< StateT > * GetStateFor(ObjectID id)
Definition: StateHolder.h:294

◆ DeserializeState()

template<class StateT>
void Leviathan::StateHolder< StateT >::DeserializeState ( ObjectID  id,
float  time,
sf::Packet &  data,
float  referencestatestime 
)
inline

Deserializes a state for entity's component from an archive.

Definition at line 204 of file StateHolder.h.

205  {
206  auto entityStates = GetStateFor(id);
207 
208  this->_DeserializeState(entityStates, id, time, data, referencestatestime);
209  }
StateT * _DeserializeState(ObjectsComponentStates< StateT > *entityStates, ObjectID id, float time, sf::Packet &data, float referencestatestime)
Definition: StateHolder.h:265
ObjectsComponentStates< StateT > * GetStateFor(ObjectID id)
Definition: StateHolder.h:294

◆ 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 259 of file StateHolder.h.

260  {
261  return StateObjects.Find(id);
262  }

◆ GetNumberOfEntitiesWithStates()

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

Returns the number of entities that have states.

Definition at line 253 of file StateHolder.h.

254  {
255  return StateObjects.GetObjectCount();
256  }

◆ GetStateFor()

template<class StateT>
ObjectsComponentStates<StateT>* Leviathan::StateHolder< StateT >::GetStateFor ( ObjectID  id)
inlineprotected

Definition at line 294 of file StateHolder.h.

295  {
296  ObjectsComponentStates<StateT>* entityStates = StateObjects.Find(id);
297 
298  if(!entityStates) {
299 
300  entityStates = StateObjects.ConstructNew(id);
301  }
302 
303  return entityStates;
304  }

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