Entity.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 
34 #ifndef DWORLD_H_INCLUDED
35 #define DWORLD_H_INCLUDED
36 
37 #include <Dynacoe/AssetID.h>
38 #include <Dynacoe/Variable.h>
39 #include <Dynacoe/Spatial.h>
40 #include <set>
41 #include <vector>
42 #include <unordered_map>
43 #include <unordered_set>
44 #include <stack>
45 
46 namespace Dynacoe {
47 class Component;
48 class UintToID;
62 class Entity : public Spatial {
63 
64  public:
67  class ID {
68  public:
69  ~ID();
70  ID() : id(nullptr){};
71  ID(const std::string & str);
72  ID(uint64_t value);
73  ID(const ID &);
74  ID & operator=(const ID & other);
75 
76  bool operator==(const ID & other) const {
77  return other.id == id;
78  }
79  bool operator!=(const ID & other) const {
80  return other.id != id;
81  }
84  uint64_t Value() const { return (uint64_t)id; }
85 
88  Entity * Identify() const;
89 
92  const std::string & String() const;
93 
97  template<typename T>
98  T * Query() {
99  if (!Valid()) return nullptr;
100  return Identify()->QueryComponent<T>();
101  }
102 
103 
104  friend bool operator<(const Entity::ID & l, const Entity::ID & r) {
105  return l.id < r.id;
106  }
110  template<typename T>
111  T * IdentifyAs() const { return dynamic_cast<T*>(Identify()); }
112 
113  bool Valid() const;
114 
115  private:
116  void * id;
117 
118  friend class Entity;
119 
120  };
121 
122 
123  using Priority = int64_t;
124 
125 
126 
127 
133  const std::vector<Entity::ID> & GetChildren() const;
134 
135 
137  std::vector<Entity::ID> GetAllSubEntities() const;
138 
142  bool Contains(Entity::ID);
143 
144 
148  std::vector<Entity::ID> FindChildByName(const std::string &);
149 
152  void Step();
153 
156  void Draw();
157 
161  void Attach(Entity::ID);
162  void Attach(Entity &);
163 
166  void Detach(Entity::ID);
167 
168 
169 
170 
171 
172 
175  template<typename T>
176  T * CreateChild() {
177  T * out = new T();
178  Attach(out->GetID());
179  return out;
180  }
181 
184  template<typename T>
185  static Entity::ID Create() {
186  T * out = new T();
187  return out->GetID();
188  }
189 
192  static Entity::ID Create() {
193  return (new Entity)->GetID();
194  }
195  template<typename T>
196 
199  static T * CreateReference() {
200  return new T;
201  }
202 
203 
206  int GetNumChildren();
207 
208 
209 
210 
211 
212 
217  double StepDuration();
218 
223  double DrawDuration();
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
244 
245 
250  void Watch(Variable v);
251 
255  Variable GetWatched(const std::string & name);
256 
259  std::vector<Variable> GetWatchedVars();
260 
264  void Unwatch(const std::string & varName);
266 
267 
268 
269 
276  void SetPriority(Priority p);
277 
281  void SetPriorityLast();
282 
286  void SetPriorityFirst();
287 
290  Priority GetPriority();
291 
292 
293 
294 
295 
302 
303 
308  bool step;
309 
314  bool draw;
315 
316 
320  bool IsStepping();
321 
325  bool IsDrawing();
327 
328 
329 
334  Entity & GetParent();
335 
338  bool HasParent();
339 
340 
341 
344  enum class UpdateClass {
345  Before,
346  After
347  };
348 
355 
356 
362  template <typename T>
364  T * c = new T;
365  AddComponentInternal(c, when);
366  return c;
367  }
368 
374  template<typename T>
375  T * QueryComponent();
376 
377 
380  const std::vector<Component *> & GetComponents() const;
381 
388  void RemoveComponent(const std::string & tag);
389 
392  void RemoveComponent(const Component *);
394 
395 
396 
397 
400  void Remove();
401 
402 
403 
404 
405 
410  void SetName(const std::string & name);
411 
416  ID GetID();
417 
420  std::string GetName();
421 
422 
423 
426  static std::vector<Entity::ID> GetAll();
427 
428 
429 
430  virtual ~Entity();
431 
432 
433 
434 
435 
436 
437 
438 
439 
440 
441 
442 
443 
444  protected:
445 
446  Entity();
447  Entity(const std::string &);
448 
449 
450 
456 
457 
461  virtual void OnEnter(){}
462 
465  virtual void OnDepart(){}
466 
467 
472  virtual void OnRemove(){}
473 
474 
477  virtual void OnPreStep(){}
478 
481  virtual void OnStep(){}
482 
483 
486  virtual void OnPreDraw(){}
487 
490  virtual void OnDraw(){}
491 
493 
494  void * operator new(std::size_t);
495  void * operator new[](std::size_t);
496  void operator delete(void * ptr);
497  void operator delete[](void * ptr);
498 
499 
500  private:
501 
502 
503 
504 
505  void AddComponentInternal(Component * c, UpdateClass);
506  friend class Engine;
507 
508  bool WasDetachedMidExecution(Entity::ID id);
509  void priorityListQueueAdd(Entity::ID);
510  void priorityListQueueRemove(Entity::ID);
511  void priorityListAdd(Entity::ID);
512  void priorityListRemove(int);
513  void initBase();
514  int getNewEntID();
515 
516 
517 
518  std::vector<Entity::ID> PriorityList; // normal entity list
519 
520 
521 
522 
523  std::set<Variable> watchList;
524 
525  Entity::ID id;
526  std::string name;
527  Entity * world;
528 
529 
530 
531 
532  int64_t priority;
533 
534 
535 
536  void * idTable;
537  bool protectd;
538  bool removed;
539 
540  double stepTime;
541  double drawTime;
542  static uint64_t idPool;
543 
544 
545  std::vector<Component *> componentsBefore;
546  std::vector<Component *> componentsAfter;
547  std::vector<Component *> components;
548  friend void EntityErase(Entity * e);
549 };
550 
551 
552 
553 
554 template<typename T>
556  for(int i = 0; i < componentsBefore.size(); ++i) {
557  if (dynamic_cast<T *>(componentsBefore[i])) {
558  return static_cast<T *>(componentsBefore[i]);
559  }
560  }
561 
562  for(int i = 0; i < componentsAfter.size(); ++i) {
563  if (dynamic_cast<T *>(componentsAfter[i])) {
564  return static_cast<T *>(componentsAfter[i]);
565  }
566  }
567 
568  return nullptr;
569 }
570 void EntityErase(Entity * e);
571 
572 };
573 
574 
575 #endif // DSPAWNED_H_INCLUDED
T * CreateChild()
Allocates an Entity and Attaches it for you.
Definition: Entity.h:176
Basic interactive object.
Definition: Entity.h:62
void SetPriority(Priority p)
Alters the priority of this entity.
T * IdentifyAs() const
Same as Find, but casts it to the type you care about.
Definition: Entity.h:111
void SetName(const std::string &name)
Sets an optional name to further identify the entity. Tools such as the debugger. ...
void Draw()
Draws all attached Entities.
void RemoveComponent(const std::string &tag)
Removes the first occurrance of a component with the tag given.
virtual void OnPreDraw()
Called when Draw() is called.
Definition: Entity.h:486
Priority GetPriority()
Returns the priority of the this entity.
ID GetID()
Returns the Entity's unique ID.
virtual void OnStep()
Called when Step() is called.
Definition: Entity.h:481
bool draw
Whether the engine should call Draw() automatically for this entity. Note that Draw() calls also mana...
Definition: Entity.h:314
bool Contains(Entity::ID)
Returns the Entity with the given ID.
T * AddComponent(UpdateClass when=UpdateClass::Before)
Attaches a component to this entity.
Definition: Entity.h:363
std::vector< Entity::ID > GetAllSubEntities() const
Returns all Entities that are within this Entity's hierarchy.
int GetNumChildren()
Returns number of active Entities.
void SetPriorityFirst()
Sends the entity to be drawn and updated as the last in the queue causing it to be on top when drawin...
virtual void OnEnter()
The Entry function is called upon each attachment to a world.
Definition: Entity.h:461
T * QueryComponent()
Returns whether or not there is currently an attached component of the type given.
Definition: Entity.h:555
Entity & GetParent()
Returns the World that the Entity belongs to.
Before Entity base logic is run.
const std::vector< Entity::ID > & GetChildren() const
Returns the i'th Entity starting at 0.
static T * CreateReference()
Creates a new Entity of the given type.
Definition: Entity.h:199
static Entity::ID Create()
Creates a new Entity of no type and returns its ID.
Definition: Entity.h:192
Definition: AssetID.h:37
T * Query()
Convenience function for .Identify()->QueryComponent()
Definition: Entity.h:98
Variable GetWatched(const std::string &name)
Returns a watched Variable of the given name.
uint64_t Value() const
Returns a value that unique ientifies the Entity.
Definition: Entity.h:84
virtual void OnRemove()
The Destruct function is called just before removal of the object.
Definition: Entity.h:472
bool IsDrawing()
Returns wether or not the Engine is handling calling Draw() automatically, taking into account the En...
Entity * Identify() const
Returns the entity referred to by this ID.
double DrawDuration()
Returns the last recorded amount of milliseconds it took the Entity, iedentified by id...
void Step()
Updates all attached Entities.
std::vector< Variable > GetWatchedVars()
Returns all watched variables associated with this Entity.
virtual void OnDraw()
Called when Draw() is called.
Definition: Entity.h:490
const std::string & String() const
Returns the ID in string form.
virtual void OnPreStep()
Called when Step() is called.
Definition: Entity.h:477
Abstraction for a variable in Dynacoe.
Definition: Variable.h:50
double StepDuration()
Returns the last recorded amount of milliseconds it took the Entity, iedentified by id...
void SetPriorityLast()
Sends the entity of the ID to be the last of in line for drawing and updates.
static std::vector< Entity::ID > GetAll()
Returns a list of all Entitys. Note that this list is generated every time.
bool IsStepping()
Returns wether or not the Engine is handling calling Step() automatically, taking into account the En...
const std::vector< Component * > & GetComponents() const
Returns all components that belong to the Entity.
void Attach(Entity::ID)
Binds an entity to the World. If bound, an Entity will be updated when the World is updated...
virtual void OnDepart()
The Depart function is called upon each detachment from a world.
Definition: Entity.h:465
std::vector< Entity::ID > FindChildByName(const std::string &)
Returns all bound Entities with the name equivalent to the one given within this Entity's hierarchy...
bool HasParent()
Returns whether or not the Entity belongs to a world.
void Watch(Variable v)
Allows you to monitor a Variable through the debugger.
void Detach(Entity::ID)
Unbinds an Entity.
std::string GetName()
Returns the name identifier of the Entity.
void Unwatch(const std::string &varName)
Stops watching a variable of the given name.
void Remove()
Detaches and marks this entity for deletion. After this is called, all references to...
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
Uniquely identifies an Entity.
Definition: Entity.h:67
UpdateClass
When Components should be updated.
Definition: Entity.h:344
After Entity base logic is run.
Main class that handles automated updates of Dynacoe.
Definition: Dynacoe.h:55
bool step
Whether the engine should call Step() automatically for this entity. Note that Step() calls also mana...
Definition: Entity.h:308
static Entity::ID Create()
Creates a new Entity and returns its ID.
Definition: Entity.h:185