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

#include <StateHolder.h>

Public Member Functions

 ObjectsComponentStates ()
 
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 >

Definition at line 22 of file StateHolder.h.

Constructor & Destructor Documentation

◆ ObjectsComponentStates()

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

Definition at line 25 of file StateHolder.h.

25  {
26 
27  for(size_t i = 0; i < StoredStates.size(); ++i){
28 
29  StoredStates[i] = nullptr;
30  }
31  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

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.

Definition at line 125 of file StateHolder.h.

125  {
126 
127  // Fill from the back and pop from the front if states don't fit //
128 
129  // This would only speed up the first added state, so maybe the performance
130  // is better if we skip this check
131  // if(StoredStates[KEPT_STATES_COUNT - 1] == nullptr){
132 
133  // StoredStates[KEPT_STATES_COUNT - 1] = newstate;
134  // return;
135  // }
136 
137  // First state will be popped off if it exists //
138  if(StoredStates[0] != nullptr){
139 
140  stateowner._DestroyStateObject(StoredStates[0]);
141  }
142 
143  for(size_t i = 0; i < KEPT_STATES_COUNT - 1; ++i){
144 
145  StoredStates[i] = StoredStates[i + 1];
146  }
147 
148  StoredStates[KEPT_STATES_COUNT - 1] = newstate;
149  }
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:167

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

75  {
76 
77  StateT* closest = nullptr;
78  int closestBy = std::numeric_limits<int>::max();
79 
80  for(StateT* state : StoredStates){
81 
82  if(!state || state->TickNumber < ticknumber)
83  continue;
84 
85  if(state->TickNumber == ticknumber){
86 
87  return state;
88  }
89 
90  const auto difference = state->TickNumber - ticknumber;
91  if(difference <= closestBy){
92 
93  closestBy = difference;
94  closest = state;
95  }
96  }
97 
98  return closest;
99  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

◆ GetNewest()

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

Returns the state with the highest tick number.

Definition at line 34 of file StateHolder.h.

34  {
35 
36  StateT* newest = nullptr;
37  int newestTick = 0;
38 
39  for(StateT* state : StoredStates){
40 
41  if(state && state->TickNumber >= newestTick){
42 
43  newest = state;
44  newestTick = state->TickNumber;
45  }
46  }
47 
48  return newest;
49  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

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

153  {
154 
155  int count = 0;
156 
157  for(StateT* state : StoredStates){
158 
159  if(state)
160  ++count;
161  }
162 
163  return count;
164  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

◆ GetOldest()

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

Returns the state with the lowest tick number.

Definition at line 52 of file StateHolder.h.

52  {
53 
54  StateT* oldest = nullptr;
55  int oldestTick = std::numeric_limits<int>::max();
56 
57  for(StateT* state : StoredStates){
58 
59  if(state && state->TickNumber <= oldestTick){
60 
61  oldest = state;
62  oldestTick = state->TickNumber;
63  }
64  }
65 
66  return oldest;
67  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

◆ GetState()

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

Returns state matching tick number.

Definition at line 102 of file StateHolder.h.

102  {
103 
104  for(StateT* state : StoredStates){
105 
106  if(state && state->TickNumber == ticknumber)
107  return state;
108  }
109 
110  return nullptr;
111  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

◆ IsStateValid()

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

Returns true if state is still valid.

Definition at line 114 of file StateHolder.h.

114  {
115 
116  for(StateT* state : StoredStates){
117  if(state == statetocheck)
118  return true;
119  }
120 
121  return false;
122  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:167

Member Data Documentation

◆ StoredStates

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

Definition at line 167 of file StateHolder.h.


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