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 time. More...
 
StateT * GetOldest () const
 Returns the state with the lowest time. More...
 
StateT * GetNewer (float time) const
 Returns the state with closest time that is higher. More...
 
StateT * GetState (float time) const
 Returns state matching the time. 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 25 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 115 of file StateHolder.h.

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

◆ GetNewer()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetNewer ( float  time) const
inline

Returns the state with closest time that is higher.

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

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

◆ GetNewest()

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

Returns the state with the time.

Definition at line 28 of file StateHolder.h.

29  {
30  StateT* newest = nullptr;
31  float highest = 0;
32 
33  for(StateT* state : StoredStates) {
34 
35  if(state && state->StateTime >= highest) {
36 
37  newest = state;
38  highest = state->StateTime;
39  }
40  }
41 
42  return newest;
43  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:157

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

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

◆ GetOldest()

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

Returns the state with the lowest time.

Definition at line 46 of file StateHolder.h.

47  {
48  StateT* oldest = nullptr;
49  float lowest = std::numeric_limits<float>::max();
50 
51  for(StateT* state : StoredStates) {
52 
53  if(state && state->StateTime <= lowest) {
54 
55  oldest = state;
56  lowest = state->StateTime;
57  }
58  }
59 
60  return oldest;
61  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:157

◆ GetState()

template<class StateT>
StateT* Leviathan::ObjectsComponentStates< StateT >::GetState ( float  time) const
inline

Returns state matching the time.

Definition at line 90 of file StateHolder.h.

91  {
92  for(StateT* state : StoredStates) {
93 
94  if(state && state->StateTime == time)
95  return state;
96  }
97 
98  return nullptr;
99  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:157

◆ IsStateValid()

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

Returns true if state is still valid.

Definition at line 102 of file StateHolder.h.

103  {
104  for(StateT* state : StoredStates) {
105  if(state == statetocheck)
106  return true;
107  }
108 
109  return false;
110  }
std::array< StateT *, KEPT_STATES_COUNT > StoredStates
Definition: StateHolder.h:157

Member Data Documentation

◆ StoredStates

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

Definition at line 157 of file StateHolder.h.


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