Overte C++ Documentation
EntitySimulation.h
1 //
2 // EntitySimulation.h
3 // libraries/entities/src
4 //
5 // Created by Andrew Meadows on 2014.11.24
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_EntitySimulation_h
13 #define hifi_EntitySimulation_h
14 
15 #include <limits>
16 #include <unordered_set>
17 
18 #include <QtCore/QObject>
19 #include <QVector>
20 
21 #include <PerfStat.h>
22 
23 #include "EntityItem.h"
24 #include "EntityTree.h"
25 
26 using EntitySimulationPointer = std::shared_ptr<EntitySimulation>;
27 using VectorOfEntities = QVector<EntityItemPointer>;
28 
29 // the EntitySimulation needs to know when these things change on an entity,
30 // so it can sort EntityItem or relay its state to the PhysicsEngine.
31 const int DIRTY_SIMULATION_FLAGS =
32  Simulation::DIRTY_POSITION |
33  Simulation::DIRTY_ROTATION |
34  Simulation::DIRTY_LINEAR_VELOCITY |
35  Simulation::DIRTY_ANGULAR_VELOCITY |
36  Simulation::DIRTY_MASS |
37  Simulation::DIRTY_COLLISION_GROUP |
38  Simulation::DIRTY_MOTION_TYPE |
39  Simulation::DIRTY_SHAPE |
40  Simulation::DIRTY_LIFETIME |
41  Simulation::DIRTY_UPDATEABLE |
42  Simulation::DIRTY_MATERIAL |
43  Simulation::DIRTY_SIMULATOR_ID;
44 
45 class EntitySimulation : public QObject, public std::enable_shared_from_this<EntitySimulation> {
46 public:
47  EntitySimulation() : _mutex(), _nextExpiry(std::numeric_limits<uint64_t>::max()), _entityTree(nullptr) { }
48  virtual ~EntitySimulation() { setEntityTree(nullptr); }
49 
50  inline EntitySimulationPointer getThisPointer() const {
51  return std::const_pointer_cast<EntitySimulation>(shared_from_this());
52  }
53 
55  void setEntityTree(EntityTreePointer tree);
56 
57  virtual void updateEntities();
58 
59  // FIXME: remove these
60  virtual void addDynamic(EntityDynamicPointer dynamic) {}
61  virtual void removeDynamic(const QUuid dynamicID) {}
62  virtual void applyDynamicChanges() {};
63 
66  void addEntity(EntityItemPointer entity);
67 
70  void changeEntity(EntityItemPointer entity);
71 
72  virtual void clearEntities();
73 
74  void moveSimpleKinematics(uint64_t now);
75 
76  EntityTreePointer getEntityTree() { return _entityTree; }
77 
78  virtual void prepareEntityForDelete(EntityItemPointer entity);
79 
80  void processChangedEntities();
81  virtual void queueEraseDomainEntity(const QUuid& id) const { }
82 
83 protected:
84  virtual void addEntityToInternalLists(EntityItemPointer entity);
85  virtual void removeEntityFromInternalLists(EntityItemPointer entity);
86  virtual void processChangedEntity(const EntityItemPointer& entity);
87  virtual void processDeadEntities();
88 
89  void expireMortalEntities(uint64_t now);
90  void callUpdateOnEntitiesThatNeedIt(uint64_t now);
91  virtual void sortEntitiesThatMoved();
92 
93  QRecursiveMutex _mutex;
94 
95  SetOfEntities _entitiesToSort; // entities moved by simulation (and might need resort in EntityTree)
96  SetOfEntities _simpleKinematicEntities; // entities undergoing non-colliding kinematic motion
97  SetOfEntities _deadEntitiesToRemoveFromTree;
98 
99 private:
100  void moveSimpleKinematics();
101 
102  // We maintain multiple lists, each for its distinct purpose.
103  // An entity may be in more than one list.
104  std::unordered_set<EntityItemPointer> _changedEntities; // all changes this frame
105  SetOfEntities _allEntities; // tracks all entities added the simulation
106  SetOfEntities _entitiesToUpdate; // entities that need to call EntityItem::update()
107  SetOfEntities _mortalEntities; // entities that have an expiry
108  uint64_t _nextExpiry;
109 
110  // back pointer to EntityTree structure
111  EntityTreePointer _entityTree;
112 };
113 
114 #endif // hifi_EntitySimulation_h