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

Holds state objects of type StateT related to a single entity. More...

#include <StateHolder.h>

Public Member Functions

StateT * GetNewest () const
 Returns the state with the highest tick number. More...
 
StateT * GetOldest () const
 Returns the state with the lowest tick number. More...
 
StateT * GetMatchingOrNewer (int ticknumber) const
 Returns the state matching the tick number or the closest tick that is higher than the tick number. More...
 
StateT * GetState (int ticknumber) const
 Returns state matching tick number. More...
 
bool IsStateValid (StateT *statetocheck) const
 Returns true if state is still valid. More...
 
void Append (StateT *newstate, StateHolder< StateT > &stateowner)
 Appends a new state removing old states if they don't fit anymore. More...
 
auto GetNumberOfStates () const
 Counts the filled number of state slots. More...
 

Protected Attributes

std::array< StateT *, KEPT_STATES_COUNTStoredStates
 

Detailed Description

template<class StateT>
class Leviathan::ObjectsComponentStates< StateT >

Holds state objects of type StateT related to a single entity.

Definition at line 23 of file StateHolder.h.

Member Function Documentation

◆ Append()

template<class StateT>
void Leviathan::ObjectsComponentStates< StateT >::Append ( StateT *  newstate,
StateHolder< StateT > &  stateowner 
)
inline

Appends a new state removing old states if they don't fit anymore.

Todo:
It is probably more efficient to use a swap algorithm for ading new states instead of this

Definition at line 119 of file StateHolder.h.

120  {
121  // Fill from the back and pop from the front if states don't fit //
122 
123  // This would only speed up the first added state, so maybe the performance
124  // is better if we skip this check
125  // if(StoredStates[KEPT_STATES_COUNT - 1] == nullptr){
126 
127  // StoredStates[KEPT_STATES_COUNT - 1] = newstate;
128  // return;
129  // }
130 
131  // First state will be popped off if it exists //
132  if(StoredStates[0] != nullptr) {
133 
134  stateowner._DestroyStateObject(StoredStates[0]);
135  }
136 
137  for(size_t i = 0; i < KEPT_STATES_COUNT - 1; ++i) {
138 
139  StoredStates[i] = StoredStates[i + 1];
140  }
141 
142  StoredStates[KEPT_STATES_COUNT - 1] = newstate;
143  }
constexpr auto KEPT_STATES_COUNT
Number of states that are kept. Corresponds to time span of TICKSPEED * KEPT_STATES_COUNT.
Definition: StateHolder.h:16
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ GetMatchingOrNewer()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetMatchingOrNewer ( int  ticknumber) const
inline

Returns the state matching the tick number or the closest tick that is higher than the tick number.

Todo:
How can the caller of this method detect when a state has been missed and should instead (maybe) wait a bit? Or should we just skip missed states and start interpolating from the later state

Definition at line 67 of file StateHolder.h.

68  {
69  StateT* closest = nullptr;
70  int closestBy = std::numeric_limits<int>::max();
71 
72  for(StateT* state : StoredStates) {
73 
74  if(!state || state->TickNumber < ticknumber)
75  continue;
76 
77  if(state->TickNumber == ticknumber) {
78 
79  return state;
80  }
81 
82  const auto difference = state->TickNumber - ticknumber;
83  if(difference <= closestBy) {
84 
85  closestBy = difference;
86  closest = state;
87  }
88  }
89 
90  return closest;
91  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ GetNewest()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetNewest ( ) const
inline

Returns the state with the highest tick number.

Definition at line 26 of file StateHolder.h.

27  {
28  StateT* newest = nullptr;
29  int newestTick = 0;
30 
31  for(StateT* state : StoredStates) {
32 
33  if(state && state->TickNumber >= newestTick) {
34 
35  newest = state;
36  newestTick = state->TickNumber;
37  }
38  }
39 
40  return newest;
41  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ GetNumberOfStates()

template<class StateT>
auto Leviathan::ObjectsComponentStates< StateT >::GetNumberOfStates ( ) const
inline

Counts the filled number of state slots.

Returns
A number in range [0, KEPT_STATES_COUNT]

Definition at line 147 of file StateHolder.h.

148  {
149  int count = 0;
150 
151  for(StateT* state : StoredStates) {
152 
153  if(state)
154  ++count;
155  }
156 
157  return count;
158  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ GetOldest()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetOldest ( ) const
inline

Returns the state with the lowest tick number.

Definition at line 44 of file StateHolder.h.

45  {
46  StateT* oldest = nullptr;
47  int oldestTick = std::numeric_limits<int>::max();
48 
49  for(StateT* state : StoredStates) {
50 
51  if(state && state->TickNumber <= oldestTick) {
52 
53  oldest = state;
54  oldestTick = state->TickNumber;
55  }
56  }
57 
58  return oldest;
59  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ GetState()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetState ( int  ticknumber) const
inline

Returns state matching tick number.

Definition at line 94 of file StateHolder.h.

95  {
96  for(StateT* state : StoredStates) {
97 
98  if(state && state->TickNumber == ticknumber)
99  return state;
100  }
101 
102  return nullptr;
103  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

◆ IsStateValid()

template<class StateT>
bool Leviathan::ObjectsComponentStates< StateT >::IsStateValid ( StateT *  statetocheck) const
inline

Returns true if state is still valid.

Definition at line 106 of file StateHolder.h.

107  {
108  for(StateT* state : StoredStates) {
109  if(state == statetocheck)
110  return true;
111  }
112 
113  return false;
114  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:161

Member Data Documentation

◆ StoredStates

template<class StateT>
std::array<StateT*, KEPT_STATES_COUNT> Leviathan::ObjectsComponentStates< StateT >::StoredStates
protected

Definition at line 161 of file StateHolder.h.


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