12 #ifndef hifi_EntitySimulation_h
13 #define hifi_EntitySimulation_h
16 #include <unordered_set>
18 #include <QtCore/QObject>
23 #include "EntityItem.h"
24 #include "EntityTree.h"
26 using EntitySimulationPointer = std::shared_ptr<EntitySimulation>;
27 using VectorOfEntities = QVector<EntityItemPointer>;
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;
45 class EntitySimulation :
public QObject,
public std::enable_shared_from_this<EntitySimulation> {
47 EntitySimulation() : _mutex(), _nextExpiry(std::numeric_limits<uint64_t>::max()), _entityTree(nullptr) { }
48 virtual ~EntitySimulation() { setEntityTree(
nullptr); }
50 inline EntitySimulationPointer getThisPointer()
const {
51 return std::const_pointer_cast<EntitySimulation>(shared_from_this());
55 void setEntityTree(EntityTreePointer tree);
57 virtual void updateEntities();
60 virtual void addDynamic(EntityDynamicPointer dynamic) {}
61 virtual void removeDynamic(
const QUuid dynamicID) {}
62 virtual void applyDynamicChanges() {};
66 void addEntity(EntityItemPointer entity);
70 void changeEntity(EntityItemPointer entity);
72 virtual void clearEntities();
74 void moveSimpleKinematics(uint64_t now);
76 EntityTreePointer getEntityTree() {
return _entityTree; }
78 virtual void prepareEntityForDelete(EntityItemPointer entity);
80 void processChangedEntities();
81 virtual void queueEraseDomainEntity(
const QUuid&
id)
const { }
84 virtual void addEntityToInternalLists(EntityItemPointer entity);
85 virtual void removeEntityFromInternalLists(EntityItemPointer entity);
86 virtual void processChangedEntity(
const EntityItemPointer& entity);
87 virtual void processDeadEntities();
89 void expireMortalEntities(uint64_t now);
90 void callUpdateOnEntitiesThatNeedIt(uint64_t now);
91 virtual void sortEntitiesThatMoved();
93 QRecursiveMutex _mutex;
95 SetOfEntities _entitiesToSort;
96 SetOfEntities _simpleKinematicEntities;
97 SetOfEntities _deadEntitiesToRemoveFromTree;
100 void moveSimpleKinematics();
104 std::unordered_set<EntityItemPointer> _changedEntities;
105 SetOfEntities _allEntities;
106 SetOfEntities _entitiesToUpdate;
107 SetOfEntities _mortalEntities;
108 uint64_t _nextExpiry;
111 EntityTreePointer _entityTree;