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-2018 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 #include "OgreSceneNode.h"
18 
19 namespace Leviathan {
20 
21 // ------------------------------------ //
22 // State creation systems
23 class PositionStateSystem : public StateCreationSystem<Position, PositionState> {};
24 
25 
26 // ------------------------------------ //
27 
29 class RenderingPositionSystem : public System<std::tuple<RenderNode&, Position&>> {
30 
31  void ProcessNode(std::tuple<RenderNode&, Position&>& node, ObjectID id,
32  const StateHolder<PositionState>& heldstates, int tick, int timeintick)
33  {
34  auto& pos = std::get<1>(node);
35 
36  if(!pos.StateMarked)
37  return;
38 
39  auto interpolated =
40  StateInterpolator::Interpolate(heldstates, id, &pos, tick, timeintick);
41 
42  auto& rendernode = std::get<0>(node);
43 
44  if(!std::get<0>(interpolated)) {
45  // No states to interpolate //
46  rendernode.Node->setPosition(pos.Members._Position);
47  rendernode.Node->setOrientation(pos.Members._Orientation);
48  return;
49  }
50 
51  const auto& state = std::get<1>(interpolated);
52  rendernode.Node->setPosition(state._Position);
53  rendernode.Node->setOrientation(state._Orientation);
54  }
55 
56 public:
57  template<class GameWorldT>
58  void Run(GameWorldT& world, const StateHolder<PositionState>& heldstates, int tick,
59  int timeintick)
60  {
61  auto& index = CachedComponents.GetIndex();
62  for(auto iter = index.begin(); iter != index.end(); ++iter) {
63 
64  this->ProcessNode(*iter->second, iter->first, heldstates, tick, timeintick);
65  }
66  }
67 
72  void CreateNodes(const std::vector<std::tuple<RenderNode*, ObjectID>>& firstdata,
73  const std::vector<std::tuple<Position*, ObjectID>>& seconddata,
74  const ComponentHolder<RenderNode>& firstholder,
75  const ComponentHolder<Position>& secondholder)
76  {
78  CachedComponents, firstdata, seconddata, firstholder, secondholder);
79  }
80 
82  void DestroyNodes(const std::vector<std::tuple<RenderNode*, ObjectID>>& firstdata,
83  const std::vector<std::tuple<Position*, ObjectID>>& seconddata)
84  {
87  }
88 };
89 
92 public:
93  void Run(GameWorld& world, std::unordered_map<ObjectID, RenderNode*>& index)
94  {
95  for(auto iter = index.begin(); iter != index.end(); ++iter) {
96 
97  auto& node = *iter->second;
98 
99  if(!node.Marked)
100  continue;
101 
102  // TODO: would it be faster to first check have these
103  // changed or is it better to just set them as Ogre might
104  // also check have the value changed
105  node.Node->setVisible(!node.Hidden);
106  node.Node->setScale(node.Scale);
107 
108  node.Marked = false;
109  }
110  }
111 };
112 
113 
116 public:
117  DLLEXPORT void Run(GameWorld& world, std::unordered_map<ObjectID, Animated*>& index,
118  int tick, int timeintick);
119 
120 private:
121  float LastSeconds = 0;
122 };
123 
124 
129 public:
131  void Run(GameWorld& world, std::unordered_map<ObjectID, Sendable*>& index)
132  {
133  for(auto iter = index.begin(); iter != index.end(); ++iter) {
134 
135  auto& node = *iter->second;
136 
137  if(!node.Marked)
138  continue;
139 
140  HandleNode(iter->first, node, world);
141 
142  node.Marked = false;
143  }
144  }
145 
146 protected:
148  DLLEXPORT void HandleNode(ObjectID id, Sendable& obj, GameWorld& world);
149 };
150 
152 template<class T>
153 class SendableMarkFromSystem : public System<std::tuple<Sendable&, T&>> {
154 public:
155  void Run(GameWorld& world)
156  {
157  auto& index = this->CachedComponents.GetIndex();
158  for(auto iter = index.begin(); iter != index.end(); ++iter) {
159 
160  if(std::get<1>(*iter->second).Marked) {
161  std::get<0>(*iter->second).Marked = true;
162  }
163  }
164  }
165 
166  void CreateNodes(const std::vector<std::tuple<Sendable*, ObjectID>>& firstdata,
167  const std::vector<std::tuple<Position*, ObjectID>>& seconddata,
168  const ComponentHolder<Sendable>& firstholder,
169  const ComponentHolder<Position>& secondholder)
170  {
172  this->CachedComponents, firstdata, seconddata, firstholder, secondholder);
173  }
174 
175  void DestroyNodes(const std::vector<std::tuple<Sendable*, ObjectID>>& firstdata,
176  const std::vector<std::tuple<Position*, ObjectID>>& seconddata)
177  {
180  }
181 };
182 
185 public:
187  DLLEXPORT void Run(GameWorld& world, std::unordered_map<ObjectID, Received*>& index) {}
188 };
189 } // 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:72
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:47
DLLEXPORT void Run(GameWorld &world, std::unordered_map< ObjectID, Animated *> &index, int tick, int timeintick)
Definition: Systems.cpp:219
Handles updating time of Ogre animations.
Definition: Systems.h:115
void Run(GameWorld &world)
Definition: Systems.h:155
int32_t ObjectID
Definition: EntityCommon.h:11
DLLEXPORT void Run(GameWorld &world, std::unordered_map< ObjectID, Received *> &index)
Definition: Systems.h:187
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:82
std::unordered_map< KeyType, ElementType * > & GetIndex()
Returns a direct access to Index.
Definition: ObjectPool.h:272
static std::tuple< bool, StateT > Interpolate(const StateHolder< StateT > &stateholder, ObjectID entity, ComponentT *entitycomponent, int currenttick, int timeintick)
Interpolates states for component.
System type for marking Sendable as marked if a component of type T is marked.
Definition: Systems.h:153
Interpolates states for received objects and handles locally controlled entities. ...
Definition: Systems.h:184
void Run(GameWorld &world, std::unordered_map< ObjectID, Sendable *> &index)
Definition: Systems.h:131
Sends updated entities from server to clients.
Definition: Systems.h:128
DLLEXPORT void HandleNode(ObjectID id, Sendable &obj, GameWorld &world)
Definition: Systems.cpp:102
Base class for all systems that create states from changed components.
Definition: System.h:193
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:168
Moves nodes of entities that have their positions changed.
Definition: Systems.h:29
void DestroyNodes(const std::vector< std::tuple< Sendable *, ObjectID >> &firstdata, const std::vector< std::tuple< Position *, ObjectID >> &seconddata)
Definition: Systems.h:175
void Run(GameWorld &world, std::unordered_map< ObjectID, RenderNode *> &index)
Definition: Systems.h:93
void Run(GameWorldT &world, const StateHolder< PositionState > &heldstates, int tick, int timeintick)
Definition: Systems.h:58
#define DLLEXPORT
Definition: Include.h:115
Holds state objects of type for quick access by ObjectID.
Definition: GameWorld.h:36
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
Entity is sendable to clients.
Definition: Components.h:112
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:166
Handles properties of Ogre nodes that have a changed RenderNode.
Definition: Systems.h:91
Represents a world that contains entities.
Definition: GameWorld.h:99