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...
 
void DeserializeState (ObjectID id, int32_t ticknumber, sf::Packet &data, int32_t referencetick)
 Deserializes a state for entity's component from an archive. More...
 
template<class ComponentT >
void DeserializeAndApplyState (ObjectID id, ComponentT &component, int32_t ticknumber, sf::Packet &data, int32_t referencetick)
 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, int ticknumber) 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, int32_t ticknumber, sf::Packet &data, int32_t referencetick)
 
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 36 of file GameWorld.h.

Constructor & Destructor Documentation

◆ StateHolder()

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

Definition at line 171 of file StateHolder.h.

171 {}

◆ ~StateHolder()

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

Definition at line 173 of file StateHolder.h.

174  {
175  // Both of the pools are released here so the destructor of ObjectsComponentStates
176  // doesn't need to notify us of the states it held
177  }

Member Function Documentation

◆ _CreateNewState()

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

Definition at line 313 of file StateHolder.h.

314  {
315  return StatePool.ConstructNew(std::forward<Args>(args)...);
316  }
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,
int32_t  ticknumber,
sf::Packet &  data,
int32_t  referencetick 
)
inlineprotected

Definition at line 270 of file StateHolder.h.

272  {
273  StateT* reference = nullptr;
274 
275  if(referencetick != -1) {
276  reference = entityStates->GetState(referencetick);
277 
278  if(!reference) {
279 
280  LOG_WARNING("StateHolder: DeserializeState: can't find reference tick: " +
281  std::to_string(referencetick) +
282  " for entity: " + std::to_string(id));
283 
284  reference = entityStates->GetNewest();
285  }
286  }
287 
288  // Create a new state //
289  StateT* newState = _CreateNewState(reference, data);
290 
291  // This can't be deserialized from data so we forward it
292  newState->TickNumber = ticknumber;
293 
294  // TODO: do we need to check whether the states already contain a state for this
295  // tick?
296  entityStates->Append(newState, *this);
297  return newState;
298  }
#define LOG_WARNING(x)
Definition: Define.h:82
StateT * _CreateNewState(Args &&... args)
Definition: StateHolder.h:313

◆ _DestroyStateObject()

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

Definition at line 318 of file StateHolder.h.

319  {
320  StatePool.Destroy(state);
321  }
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,
int  ticknumber 
) const
inline

Creates a state object for sending.

Definition at line 251 of file StateHolder.h.

252  {
253  return StateT(ticknumber, component);
254  }

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

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

◆ DeserializeAndApplyState()

template<class StateT>
template<class ComponentT >
void Leviathan::StateHolder< StateT >::DeserializeAndApplyState ( ObjectID  id,
ComponentT &  component,
int32_t  ticknumber,
sf::Packet &  data,
int32_t  referencetick 
)
inline

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

Definition at line 219 of file StateHolder.h.

221  {
222  auto entityStates = GetStateFor(id);
223 
224  // Deserialize
225  auto* deserialized =
226  this->_DeserializeState(entityStates, id, ticknumber, data, referencetick);
227 
228  if(!deserialized) {
229  LOG_ERROR("StateHolder: failed to deserialize state");
230  return;
231  }
232 
233  // And apply it if it is newest
234  auto* newest = entityStates->GetNewest();
235 
236  if(deserialized == newest) {
237 
238  component.ApplyState(*newest);
239  } else {
240  int newestNumber = newest ? newest->TickNumber : -1;
241 
242  LOG_WARNING("StateHolder: DeserializeAndApplyState: received not the newest "
243  "packet, received: " +
244  std::to_string(deserialized->TickNumber) +
245  ", newest: " + std::to_string(newestNumber));
246  }
247  }
StateT * _DeserializeState(ObjectsComponentStates< StateT > *entityStates, ObjectID id, int32_t ticknumber, sf::Packet &data, int32_t referencetick)
Definition: StateHolder.h:270
#define LOG_ERROR(x)
Definition: Define.h:83
#define LOG_WARNING(x)
Definition: Define.h:82
ObjectsComponentStates< StateT > * GetStateFor(ObjectID id)
Definition: StateHolder.h:300

◆ DeserializeState()

template<class StateT>
void Leviathan::StateHolder< StateT >::DeserializeState ( ObjectID  id,
int32_t  ticknumber,
sf::Packet &  data,
int32_t  referencetick 
)
inline

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

Definition at line 208 of file StateHolder.h.

210  {
211  auto entityStates = GetStateFor(id);
212 
213  this->_DeserializeState(entityStates, id, ticknumber, data, referencetick);
214  }
StateT * _DeserializeState(ObjectsComponentStates< StateT > *entityStates, ObjectID id, int32_t ticknumber, sf::Packet &data, int32_t referencetick)
Definition: StateHolder.h:270
ObjectsComponentStates< StateT > * GetStateFor(ObjectID id)
Definition: StateHolder.h:300

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

265  {
266  return StateObjects.Find(id);
267  }

◆ GetNumberOfEntitiesWithStates()

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

Returns the number of entities that have states.

Definition at line 258 of file StateHolder.h.

259  {
260  return StateObjects.GetObjectCount();
261  }

◆ GetStateFor()

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

Definition at line 300 of file StateHolder.h.

301  {
302  ObjectsComponentStates<StateT>* entityStates = StateObjects.Find(id);
303 
304  if(!entityStates) {
305 
306  entityStates = StateObjects.ConstructNew(id);
307  }
308 
309  return entityStates;
310  }

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