Leviathan  0.8.0.0
Leviathan game engine
Systems.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 // ------------------------------------ //
7 #include "Include.h"
8 
9 #include "Components.h"
10 #include "StateInterpolator.h"
11 #include "System.h"
12 
13 #include "Utility/Convert.h"
14 
15 #include "Generated/ComponentStates.h"
16 
17 namespace Leviathan {
18 
19 // ------------------------------------ //
20 // State creation systems
21 class PositionStateSystem : public StateCreationSystem<Position, PositionState> {};
22 
23 
24 // ------------------------------------ //
25 
27 class RenderingPositionSystem : public System<std::tuple<RenderNode&, Position&>> {
28 
30  void ProcessNode(std::tuple<RenderNode&, Position&>& node, ObjectID id,
31  const StateHolder<PositionState>& heldstates, float elapsed)
32  {
33  auto& pos = std::get<1>(node);
34 
35  if(!pos.StateMarked)
36  return;
37 
38  auto interpolated = StateInterpolator::Interpolate(heldstates, id, &pos, elapsed);
39 
40  auto& rendernode = std::get<0>(node);
41 
42  if(!std::get<0>(interpolated)) {
43  // No states to interpolate //
44  rendernode.Node->SetPositionAndOrientation(
45  pos.Members._Position, pos.Members._Orientation);
46  return;
47  }
48 
49  const auto& state = std::get<1>(interpolated);
50  rendernode.Node->SetPositionAndOrientation(state._Position, state._Orientation);
51  }
52 
53 public:
54  template<class GameWorldT>
55  void Run(GameWorldT& world)
56  {
57  auto& index = CachedComponents.GetIndex();
58  for(auto iter = index.begin(); iter != index.end(); ++iter) {
59 
60  // this->ProcessNode(*iter->second, iter->first, heldstates, tick, timeintick);
61  auto& pos = std::get<1>(*iter->second);
62 
63  if(!pos.Marked)
64  continue;
65 
66  auto& rendernode = std::get<0>(*iter->second);
67  rendernode.Node->SetPositionAndOrientation(
68  pos.Members._Position, pos.Members._Orientation);
69 
70  // TODO: some other system might also want to detect if Position is marked. Design
71  // some architecture to allow that
72  pos.Marked = false;
73  }
74  }
75 
80  void CreateNodes(const std::vector<std::tuple<RenderNode*, ObjectID>>& firstdata,
81  const std::vector<std::tuple<Position*, ObjectID>>& seconddata,
82  const ComponentHolder<RenderNode>& firstholder,
83  const ComponentHolder<Position>& secondholder)
84  {
86  CachedComponents, firstdata, seconddata, firstholder, secondholder);
87  }
88 
90  void DestroyNodes(const std::vector<std::tuple<RenderNode*, ObjectID>>& firstdata,
91  const std::vector<std::tuple<Position*, ObjectID>>& seconddata)
92  {
95  }
96 };
97 
100 public:
101  void Run(GameWorld& world, std::unordered_map<ObjectID, RenderNode*>& index)
102  {
103  for(auto iter = index.begin(); iter != index.end(); ++iter) {
104 
105  auto& node = *iter->second;
106 
107  if(!node.Marked)
108  continue;
109 
110  node.Node->SetHidden(node.Hidden);
111  node.Node->SetScale(node.Scale);
112  node.Marked = false;
113  }
114  }
115 };
116 
119 public:
120  DLLEXPORT void Run(GameWorld& world, std::unordered_map<ObjectID, Model*>& index);
121 };
122 
123 
127 public:
128  DLLEXPORT void Run(
129  GameWorld& world, std::unordered_map<ObjectID, Animated*>& index, float elapsed);
130 };
131 
132 
137 public:
139  void Run(GameWorld& world, std::unordered_map<ObjectID, Sendable*>& index)
140  {
141  for(auto iter = index.begin(); iter != index.end(); ++iter) {
142 
143  auto& node = *iter->second;
144 
145  if(!node.Marked)
146  continue;
147 
148  HandleNode(iter->first, node, world);
149 
150  node.Marked = false;
151  }
152  }
153 
154 protected:
156  DLLEXPORT void HandleNode(ObjectID id, Sendable& obj, GameWorld& world);
157 };
158 
160 template<class T>
161 class SendableMarkFromSystem : public System<std::tuple<Sendable&, T&>> {
162 public:
163  void Run(GameWorld& world)
164  {
165  auto& index = this->CachedComponents.GetIndex();
166  for(auto iter = index.begin(); iter != index.end(); ++iter) {
167 
168  if(std::get<1>(*iter->second).Marked) {
169  std::get<0>(*iter->second).Marked = true;
170  }
171  }
172  }
173 
174  void CreateNodes(const std::vector<std::tuple<Sendable*, ObjectID>>& firstdata,
175  const std::vector<std::tuple<Position*, ObjectID>>& seconddata,
176  const ComponentHolder<Sendable>& firstholder,
177  const ComponentHolder<Position>& secondholder)
178  {
180  this->CachedComponents, firstdata, seconddata, firstholder, secondholder);
181  }
182 
183  void DestroyNodes(const std::vector<std::tuple<Sendable*, ObjectID>>& firstdata,
184  const std::vector<std::tuple<Position*, ObjectID>>& seconddata)
185  {
188  }
189 };
190 
193 public:
195  DLLEXPORT void Run(GameWorld& world, std::unordered_map<ObjectID, Received*>& index) {}
196 };
197 } // namespace Leviathan
void CreateNodes(const std::vector< std::tuple< RenderNode *, ObjectID >> &firstdata, const std::vector< std::tuple< Position *, ObjectID >> &seconddata, const ComponentHolder< RenderNode > &firstholder, const ComponentHolder< Position > &secondholder)
Creates nodes if matching ids are found in all data vectors or already existing component holders.
Definition: Systems.h:80
void Run(GameWorld &world, std::unordered_map< ObjectID, RenderNode * > &index)
Definition: Systems.h:101
void Run(GameWorld &world, std::unordered_map< ObjectID, Sendable * > &index)
Definition: Systems.h:139
void Run(GameWorld &world)
Definition: Systems.h:163
int32_t ObjectID
Definition: EntityCommon.h:11
static std::tuple< bool, StateT > Interpolate(const StateHolder< StateT > &stateholder, ObjectID entity, ComponentT *entitycomponent, float elapsed)
Interpolates states for component.
void DestroyNodes(const std::vector< std::tuple< RenderNode *, ObjectID >> &firstdata, const std::vector< std::tuple< Position *, ObjectID >> &seconddata)
Destroys nodes if matching ids are found in the removed data data vectors.
Definition: Systems.h:90
std::unordered_map< KeyType, ElementType * > & GetIndex()
Returns a direct access to Index.
Definition: ObjectPool.h:272
static void TupleCachedComponentCollectionHelper(ObjectPool< std::tuple< FirstType &, SecondType & >, ObjectID > &CachedComponentCollections, const std::vector< std::tuple< FirstType *, ObjectID >> &firstdata, const std::vector< std::tuple< SecondType *, ObjectID >> &seconddata, const ComponentHolder< FirstType > &firstholder, const ComponentHolder< SecondType > &secondholder)
Helper function for creating nodes based on std::tuple.
Definition: System.h:46
System type for marking Sendable as marked if a component of type T is marked.
Definition: Systems.h:161
Interpolates states for received objects and handles locally controlled entities.
Definition: Systems.h:192
DLLEXPORT void Run(GameWorld &world, std::unordered_map< ObjectID, Model * > &index)
Definition: Systems.cpp:19
Sends updated entities from server to clients.
Definition: Systems.h:136
DLLEXPORT void HandleNode(ObjectID id, Sendable &obj, GameWorld &world)
Definition: Systems.cpp:125
Base class for all systems that create states from changed components.
Definition: System.h:192
void RemoveBasedOnKeyTupleList(const std::vector< std::tuple< Any, KeyType >> &values)
Destroys without releasing elements based on ids in vector.
Definition: ObjectPool.h:198
Base for all entity component related systems.
Definition: System.h:167
Moves nodes of entities that have their positions changed.
Definition: Systems.h:27
void DestroyNodes(const std::vector< std::tuple< Sendable *, ObjectID >> &firstdata, const std::vector< std::tuple< Position *, ObjectID >> &seconddata)
Definition: Systems.h:183
Handles updating time of Ogre animations.
Definition: Systems.h:126
void Run(GameWorldT &world)
Definition: Systems.h:55
DLLEXPORT void Run(GameWorld &world, std::unordered_map< ObjectID, Received * > &index)
Definition: Systems.h:195
#define DLLEXPORT
Definition: Include.h:84
Handles properties of Model.
Definition: Systems.h:118
Holds state objects of type for quick access by ObjectID.
Definition: GameWorld.h:31
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
Entity is sendable to clients.
Definition: Components.h:115
void CreateNodes(const std::vector< std::tuple< Sendable *, ObjectID >> &firstdata, const std::vector< std::tuple< Position *, ObjectID >> &seconddata, const ComponentHolder< Sendable > &firstholder, const ComponentHolder< Position > &secondholder)
Definition: Systems.h:174
DLLEXPORT void Run(GameWorld &world, std::unordered_map< ObjectID, Animated * > &index, float elapsed)
Definition: Systems.cpp:242
Handles properties of scene objects that have a changed RenderNode.
Definition: Systems.h:99
Represents a world that contains entities.
Definition: GameWorld.h:57