Leviathan  0.8.0.0
Leviathan game engine
ComponentState.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Component.h"
7 
8 namespace Leviathan {
9 
11 template<class StateT>
13 public:
15 
20  bool StateMarked = true;
21 
22  // Data for currently interpolating state
23  // Some child classes might not use this if interpolating is not done
24  StateT* InterpolatingStartState = nullptr;
25  StateT* InterpolatingEndState = nullptr;
26  float TimeSinceStartState = 0.f;
27 };
28 
31 public:
32  inline BaseComponentState(float time, COMPONENT_TYPE componenttype) :
33  StateTime(time), ComponentType(componenttype)
34  {}
35  virtual ~BaseComponentState() {}
36 
37  inline BaseComponentState(const BaseComponentState& other) noexcept :
38  StateTime(other.StateTime), ComponentType(other.ComponentType),
39  UpdatedFields(other.UpdatedFields)
40  {}
41 
42  inline BaseComponentState(BaseComponentState&& other) noexcept :
43  StateTime(other.StateTime), ComponentType(other.ComponentType),
44  UpdatedFields(other.UpdatedFields)
45  {}
46 
47  inline BaseComponentState& operator=(const BaseComponentState& other) noexcept
48  {
49  StateTime = other.StateTime;
50  ComponentType = other.ComponentType;
51  UpdatedFields = other.UpdatedFields;
52  return *this;
53  }
54 
56  {
57  StateTime = other.StateTime;
58  ComponentType = other.ComponentType;
59  UpdatedFields = other.UpdatedFields;
60  return *this;
61  }
62 
68  DLLEXPORT virtual void AddDataToPacket(
69  sf::Packet& packet, BaseComponentState* olderstate) const = 0;
70 
75  DLLEXPORT virtual bool FillMissingData(BaseComponentState& otherstate) = 0;
76 
79  inline bool IsBitSet(uint8_t bitnum = 0) const
80  {
81  return (UpdatedFields & (1 << bitnum)) == 1;
82  }
83 
86  inline void SetBit(uint8_t bitnum = 0)
87  {
88  UpdatedFields |= (1 << bitnum);
89  }
90 
91  inline void SetAllBitsInUpdated()
92  {
93  UpdatedFields = std::numeric_limits<uint8_t>::max();
94  }
95 
96 public:
100  float StateTime;
101 
105 
109  uint8_t UpdatedFields = std::numeric_limits<uint8_t>::max();
110 };
111 
115 
117 class EntityState {
118 public:
120  DLLEXPORT void CreateUpdatePacket(EntityState& olderstate, sf::Packet& packet);
121 
125  DLLEXPORT void AddDataToPacket(sf::Packet& packet);
126 
127 
128  inline void Append(std::unique_ptr<BaseComponentState>&& state)
129  {
130  ComponentStates.push_back(std::move(state));
131  }
132 
133  std::vector<std::unique_ptr<BaseComponentState>> ComponentStates;
134 };
135 
136 
137 
138 } // namespace Leviathan
std::vector< std::unique_ptr< BaseComponentState > > ComponentStates
Class for supporting script defined component states.
BaseComponentState(float time, COMPONENT_TYPE componenttype)
BaseComponentState(const BaseComponentState &other) noexcept
COMPONENT_TYPE
Must contain all valid Component types.
Definition: Component.h:21
BaseComponentState & operator=(BaseComponentState &&other) noexcept
BaseComponentState(BaseComponentState &&other) noexcept
Base for all component state classes.
Alternative base class for Component that creates distinct state objects.
bool IsBitSet(uint8_t bitnum=0) const
Returns true if bitnum bit is set in UpdatedFields.
unsigned char uint8_t
Definition: core.h:38
void Append(std::unique_ptr< BaseComponentState > &&state)
DLLEXPORT void AddDataToPacket(sf::Packet &packet)
Adds full data to packet.
virtual DLLEXPORT void AddDataToPacket(sf::Packet &packet, BaseComponentState *olderstate) const =0
Adds update data to a packet.
DLLEXPORT void CreateUpdatePacket(EntityState &olderstate, sf::Packet &packet)
Creates a delta update to packet.
ComponentWithStates(COMPONENT_TYPE type)
#define DLLEXPORT
Definition: Include.h:84
Base class for all components.
Definition: Component.h:52
void SetBit(uint8_t bitnum=0)
Sets bitnum bit in Updated.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
Holder of state for a whole entity.
BaseComponentState & operator=(const BaseComponentState &other) noexcept
virtual DLLEXPORT bool FillMissingData(BaseComponentState &otherstate)=0
Copies data to missing values in this state from another state.