Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
ecs_world.hpp
Go to the documentation of this file.
1
13
14#ifndef FOOLSENGINE_ECSWORLD_H
15#define FOOLSENGINE_ECSWORLD_H
16
17#include <cstdint>
18#include <typeinfo>
19#include <iostream>
20#include <tuple>
21#include <memory>
22#include <vector>
23#include <set>
24#include <bitset>
25#include <unordered_map>
26#include <type_traits>
27#include <set>
28
29#include <nlohmann/json.hpp>
30
31#include "../util.hpp"
32#include "../registrator.hpp"
33
39
45
51
57
58namespace ToyMaker {
59
60
61
68 using EntityID = std::uint64_t;
69
76 using WorldID = std::uint64_t;
77
85 using UniversalEntityID = std::pair<WorldID, EntityID>;
86
93 using ECSType = std::uint8_t;
94
104
113
119 constexpr EntityID kMaxEntities { 1000000 };
120
125 constexpr ECSType kMaxECSTypes { 255 };
126
133
140
154 using Signature = std::bitset<kMaxComponents>;
155
156 class BaseSystem;
157 class SystemManager;
158 class ComponentManager;
159 class Entity;
160 class ECSWorld;
161
169 public:
170
176 explicit BaseComponentArray(std::weak_ptr<ECSWorld> world): mWorld{world} {}
177
182 virtual ~BaseComponentArray()=default;
183
189 virtual void handleEntityDestroyed(EntityID entityID)=0;
190
197 virtual void handlePreSimulationStep() = 0;
198
205 virtual void copyComponent(EntityID to, EntityID from)=0;
206
214 virtual void copyComponent(EntityID to, EntityID from, BaseComponentArray& other) = 0;
215
222 virtual void addComponent(EntityID to, const nlohmann::json& jsonComponent)=0;
223
230 virtual void updateComponent(EntityID to, const nlohmann::json& jsonComponent)=0;
231
239 virtual bool hasComponent(EntityID entityID) const=0;
240
246 virtual void removeComponent(EntityID entityID)=0;
247
254 virtual std::shared_ptr<BaseComponentArray> instantiate(std::weak_ptr<ECSWorld> world) const = 0;
255
256 protected:
257
262 std::weak_ptr<ECSWorld> mWorld {};
263 };
264
288 template <typename TComponent, typename Enable=void>
296 static TComponent get(const nlohmann::json& jsonComponent){
297 // in the regular case, just invoke the from_json method that the
298 // author of the component has presumably implemented
299 TComponent component = jsonComponent;
300 return component;
301 }
302 };
303
314 template <typename TComponent, typename Enable>
315 struct ComponentFromJSON<std::shared_ptr<TComponent>, Enable> {
316 static std::shared_ptr<TComponent> get(const nlohmann::json& jsonComponent) {
317 // assume once again that the author of the component has provided
318 // a from_json function that will be invoked here
319 std::shared_ptr<TComponent> component { new TComponent{} = jsonComponent };
320 return component;
321 }
322 };
323
336 template<typename T>
338 public:
347 T operator() (const T& previousState, const T& nextState, float simulationProgress=1.f) const;
348
349 private:
354 RangeMapperLinear mProgressLimits {0.f, 1.f, 0.f, 1.f};
355 };
356
365 template<typename TComponent>
367 public:
373 explicit ComponentArray(std::weak_ptr<ECSWorld> world): BaseComponentArray{ world } {}
374
375 private:
376
383 std::shared_ptr<BaseComponentArray> instantiate(std::weak_ptr<ECSWorld> world) const override;
384
391 void addComponent(EntityID entityID, const TComponent& component);
392
399 void addComponent(EntityID entityID, const nlohmann::json& componentJSON) override;
400
406 void removeComponent(EntityID entityID) override;
407
415 TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const;
416
426 bool hasComponent(EntityID entityID) const override;
427
434 void updateComponent(EntityID entityID, const TComponent& newValue);
435
442 void updateComponent(EntityID entityID, const nlohmann::json& value) override;
443
449 virtual void handleEntityDestroyed(EntityID entityID) override;
450
457 virtual void handlePreSimulationStep() override;
458
465 virtual void copyComponent(EntityID to, EntityID from) override;
466
474 virtual void copyComponent(EntityID to, EntityID from, BaseComponentArray& other) override;
475
480 std::vector<TComponent> mComponentsNext {};
481
486 std::vector<TComponent> mComponentsPrevious {};
487
493 std::unordered_map<EntityID, std::size_t> mEntityToComponentIndex {};
494
500 std::unordered_map<std::size_t, EntityID> mComponentToEntity {};
501 friend class ComponentManager;
502 };
503
504
512 public:
518 explicit ComponentManager(std::weak_ptr<ECSWorld> world): mWorld { world } {};
519 private:
520
527 ComponentManager instantiate(std::weak_ptr<ECSWorld> world) const;
528
534 template<typename TComponent>
536
542 template <typename TComponent>
549 std::string operator()() {
550 return TComponent::getComponentTypeName();
551 }
552 };
553
560 template <typename TComponent>
561 struct getComponentTypeName<std::shared_ptr<TComponent>> {
562 std::string operator()() {
563 return TComponent::getComponentTypeName();
564 }
565 };
566
573 template<typename TComponent>
574 std::shared_ptr<ComponentArray<TComponent>> getComponentArray() const {
575 const std::size_t componentHash { typeid(TComponent).hash_code() };
576 assert(mHashToComponentType.find(componentHash) != mHashToComponentType.end() && "This component type has not been registered");
577 return std::dynamic_pointer_cast<ComponentArray<TComponent>>(mHashToComponentArray.at(componentHash));
578 }
579
586 std::shared_ptr<BaseComponentArray> getComponentArray(const std::string& componentTypeName) const {
587 const std::size_t componentHash { mNameToComponentHash.at(componentTypeName) };
588 return mHashToComponentArray.at(componentHash);
589 }
590
597 template<typename TComponent>
599
606 ComponentType getComponentType(const std::string& typeName) const;
607
624
632 template<typename TComponent>
633 void addComponent(EntityID entityID, const TComponent& component);
634
641 void addComponent(EntityID entityID, const nlohmann::json& jsonComponent);
642
649 template<typename TComponent>
650 void removeComponent(EntityID entityID);
651
658 void removeComponent(EntityID entityID, const std::string& type);
659
670 template<typename TComponent>
671 bool hasComponent(EntityID entityID) const;
672
683 bool hasComponent(EntityID entityID, const std::string& type);
684
693 template<typename TComponent>
694 TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const;
695
705 template<typename TComponent>
706 void updateComponent(EntityID entityID, const TComponent& newValue);
707
716 void updateComponent(EntityID entityID, const nlohmann::json& componentProperties);
717
725 template<typename TComponent>
726 void copyComponent(EntityID to, EntityID from);
727
734 void copyComponents(EntityID to, EntityID from);
735
743 void copyComponents(EntityID to, EntityID from, ComponentManager& other);
744
752 void handleEntityDestroyed(EntityID entityID);
753
760
765 void unregisterAll();
766
772 std::unordered_map<std::string, std::size_t> mNameToComponentHash {};
773
781 std::unordered_map<std::size_t, ComponentType> mHashToComponentType {};
782
787 std::unordered_map<std::size_t, std::shared_ptr<BaseComponentArray>> mHashToComponentArray {};
788
798 std::unordered_map<EntityID, Signature> mEntityToSignature {};
799
804 std::weak_ptr<ECSWorld> mWorld;
805
806 friend class ECSWorld;
807 };
808
814 class BaseSystem : public std::enable_shared_from_this<BaseSystem> {
815 public:
821 BaseSystem(std::weak_ptr<ECSWorld> world): mWorld { world } {}
822
827 virtual ~BaseSystem() = default;
828
835 virtual bool isSingleton() const { return false; }
836 protected:
837
843 const std::set<EntityID>& getEnabledEntities();
844
854 template <typename TComponent, typename TSystem>
855 TComponent getComponent_(EntityID entityID, float progress=1.f) const;
856
865 template <typename TComponent, typename TSystem>
866 void updateComponent_(EntityID entityID, const TComponent& component);
867
875 bool isEnabled(EntityID entityID) const;
876
884 bool isRegistered(EntityID entityID) const;
885
892 virtual std::shared_ptr<BaseSystem> instantiate(std::weak_ptr<ECSWorld> world) = 0;
893
898 std::weak_ptr<ECSWorld> mWorld;
899 private:
900
907 void addEntity(EntityID entityID, bool enabled=true);
908
914 void removeEntity(EntityID entityID);
915
921 void enableEntity(EntityID entityID);
927 void disableEntity(EntityID entityID);
928
936 virtual void onEntityEnabled(EntityID entityID) {(void)entityID; /* prevent unused parameter warnings*/}
937
945 virtual void onEntityDisabled(EntityID entityID) { (void)entityID; /* prevent unused parameter warnings*/}
946
952 virtual void onEntityUpdated(EntityID entityID) { (void)entityID; /* prevent unused parameter warnings*/assert(false && "The base class version of onEntityUpdated should never be called"); }
953
958 virtual void onInitialize() {}
959
964 virtual void onSimulationActivated() {}
965
971 virtual void onSimulationPreStep(uint32_t simStepMillis) {(void)simStepMillis;/*prevent unused parameter warnings*/}
972
980 virtual void onSimulationStep(uint32_t simStepMillis) {(void)simStepMillis;/*prevent unused parameter warnings*/}
981
987 virtual void onSimulationPostStep(uint32_t simStepMillis) {(void)simStepMillis;/*prevent unused parameter warnings*/}
988
994 virtual void onPostTransformUpdate(uint32_t timeStepMillis) {(void)timeStepMillis;/*prevent unused parameter warnings*/}
995
1005 virtual void onVariableStep(float simulationProgress, uint32_t variableStepMillis) {(void)simulationProgress; (void)variableStepMillis;/*prevent unused parameter warnings*/}
1006
1012 virtual void onPreRenderStep(float simulationProgress) {(void)simulationProgress;/*prevent unused parameter warnings*/}
1013
1019 virtual void onPostRenderStep(float simulationProgress) {(void)simulationProgress;/*prevent unused parameter warnings*/}
1020
1027 virtual void onSimulationDeactivated() {}
1028
1035 virtual void onDestroyed() {}
1036
1041 std::set<EntityID> mEnabledEntities {};
1042
1047 std::set<EntityID> mDisabledEntities {};
1048
1049 friend class SystemManager;
1050 friend class ECSWorld;
1051 };
1052
1061 template <typename TSystemDerived, typename TListenedForComponentsTuple, typename TRequiredComponentsTuple>
1062 class System{ static_assert(false && "Non specialized system cannot be declared"); };
1063
1074 template <typename TSystemDerived, typename ...TListenedForComponents, typename ...TRequiredComponents>
1075 class System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>: public BaseSystem {
1076
1081 static void registerSelf();
1082
1083 protected:
1084
1092 explicit System(std::weak_ptr<ECSWorld> world): BaseSystem { world } { s_registrator.emptyFunc(); }
1093
1102 template<typename TComponent>
1103 TComponent getComponent(EntityID entityID, float progress=1.f) {
1104 assert(!isSingleton() && "Singletons cannot retrieve components by EntityID alone");
1106 }
1107
1115 template<typename TComponent>
1116 void updateComponent(EntityID entityID, const TComponent& component) {
1117 assert(!isSingleton() && "Singletons cannot retrieve components by EntityID alone");
1119 }
1120
1127 std::shared_ptr<BaseSystem> instantiate(std::weak_ptr<ECSWorld> world) override;
1128 private:
1129
1134 inline static Registrator<System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>& s_registrator {
1135 Registrator<System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>::getRegistrator()
1136 };
1137
1138 friend class Registrator<System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>;
1139 };
1140
1149 public:
1155 explicit SystemManager(std::weak_ptr<ECSWorld> world): mWorld{ world } {}
1156 private:
1163 SystemManager instantiate(std::weak_ptr<ECSWorld> world) const;
1164
1174 template<typename TSystem>
1175 void registerSystem(const Signature& signature, const Signature& listenedForComponents);
1176
1181 void unregisterAll();
1182
1189 template<typename TSystem>
1190 std::shared_ptr<TSystem> getSystem();
1191
1198 template<typename TSystem>
1199 void enableEntity(EntityID entityID);
1200
1210 void enableEntity(EntityID entityID, Signature entitySignature, Signature systemMask = Signature{}.set());
1211
1218 template<typename TSystem>
1219 void disableEntity(EntityID entityID);
1220
1227 void disableEntity(EntityID entityID, Signature entitySignature);
1228
1237 template<typename TSystem>
1238 SystemType getSystemType() const;
1239
1248 template<typename TSystem>
1249 bool isEnabled(EntityID entityID);
1250
1259 template <typename TSystem>
1260 bool isRegistered(EntityID entityID);
1261
1268 void handleEntitySignatureChanged(EntityID entityID, Signature signature);
1269
1275 void handleEntityDestroyed(EntityID entityID);
1276
1284 void handleEntityUpdated(EntityID entityID, Signature signature, ComponentType updatedComponent);
1285
1295 template<typename TSystem>
1296 void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent);
1297
1303 void handleInitialize();
1304
1310 void handleSimulationActivated();
1311
1319 void handleSimulationPreStep(uint32_t simStepMillis);
1320
1328 void handleSimulationStep(uint32_t simStepMillis);
1329
1337 void handleSimulationPostStep(uint32_t simStepMillis);
1338
1346 void handlePostTransformUpdate(uint32_t timeStepMillis);
1347
1357 void handleVariableStep(float simulationProgress, uint32_t variableStepMillis);
1358
1366 void handlePreRenderStep(float simulationProgress);
1367
1373 void handlePostRenderStep(float simulationProgress);
1374
1379 void handleSimulationDeactivated();
1380
1387 std::unordered_map<std::string, Signature> mNameToSignature {};
1388
1395 std::unordered_map<std::string, Signature> mNameToListenedForComponents {};
1396
1402 std::unordered_map<std::string, SystemType> mNameToSystemType {};
1403
1408 std::unordered_map<std::string, std::shared_ptr<BaseSystem>> mNameToSystem {};
1409
1414 std::weak_ptr<ECSWorld> mWorld;
1415
1416 friend class ECSWorld;
1417 friend class BaseSystem;
1418 };
1419
1439 class ECSWorld: public std::enable_shared_from_this<ECSWorld> {
1440 public:
1452 static std::weak_ptr<const ECSWorld> getPrototype();
1453
1459 std::shared_ptr<ECSWorld> instantiate() const;
1460
1518 template<typename ...TComponent>
1519 static void registerComponentTypes();
1520
1528 template <typename TSystemDerived, typename TListenedForComponents, typename TRequiredComponents>
1529 struct SystemRegistrationArgs { static_assert(false && "Cannot create unspecialized instance of SystemRegistrationArgs"); };
1530
1541 template <typename TSystemDerived, typename ...TListenedForComponents, typename ...TRequiredComponents>
1542 struct SystemRegistrationArgs<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>> {};
1543
1544
1590 template <typename TSystemDerived, typename ...TListenedForComponents, typename ...TRequiredComponents>
1591 static void registerSystem(SystemRegistrationArgs<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>);
1592
1599 template<typename TSystem>
1600 std::shared_ptr<TSystem> getSystem();
1601
1610 template<typename TSystem>
1611 static std::shared_ptr<TSystem> getSystemPrototype();
1612
1623 template <typename TSingletonSystem>
1624 static std::shared_ptr<TSingletonSystem> getSingletonSystem();
1625
1636 template <typename TSystem>
1638
1647 template <typename TSystem>
1648 bool isEnabled(EntityID entityID);
1649
1658 template <typename TSystem>
1659 bool isRegistered(EntityID entityID);
1660
1670 template<typename ...TComponents>
1671 Entity createEntity(TComponents...components);
1672
1684 template <typename ...TComponents>
1685 static Entity createEntityPrototype(TComponents...components);
1686
1687 // Simulation lifecycle events
1688
1693 void initialize();
1694
1699 void activateSimulation();
1700
1705 void deactivateSimulation();
1706
1707 // Simulation loop events
1708
1718 void simulationPreStep(uint32_t simStepMillis);
1719
1727 void simulationStep(uint32_t simStepMillis);
1728
1736 void simulationPostStep(uint32_t simStepMillis);
1737
1745 void postTransformUpdate(uint32_t timeStepMillis);
1746
1757 void variableStep(float simulationProgress, uint32_t variableStepMillis);
1758
1766 void preRenderStep(float simulationProgress);
1767
1775 void postRenderStep(float simulationProgress);
1776
1781 void cleanup();
1782
1790 inline WorldID getID() const { return mID; }
1791
1792 private:
1793
1799 static std::shared_ptr<ECSWorld> createWorld();
1800
1806 static std::weak_ptr<ECSWorld> getInstance();
1807
1812 ECSWorld() = default;
1813
1820 void copyComponents(EntityID to, EntityID from);
1821
1829 void copyComponents(EntityID to, EntityID from, ECSWorld& other);
1830
1836 void relocateEntity(Entity& entity);
1837
1845 template<typename ...TComponents>
1846 Entity privateCreateEntity(TComponents...components);
1847
1855 void destroyEntity(EntityID entityID);
1856
1865 template<typename TSystem>
1866 void enableEntity(EntityID entityID);
1867
1876 void enableEntity(EntityID entityID, Signature systemMask = Signature{}.set());
1877
1886 template<typename TSystem>
1887 void disableEntity(EntityID entityID);
1888
1895 void disableEntity(EntityID entityID);
1896
1906 template<typename TComponent>
1907 void addComponent(EntityID entityID, const TComponent& component);
1908
1917 void addComponent(EntityID entityID, const nlohmann::json& jsonComponent);
1918
1919
1930 template<typename TComponent>
1931 TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const;
1932
1943 template<typename TComponent, typename TSystem>
1944 TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const;
1945
1956 template <typename TComponent>
1957 bool hasComponent(EntityID entityID) const;
1958
1970 bool hasComponent(EntityID entityID, const std::string& typeName) const;
1971
1981 template<typename TComponent>
1982 void updateComponent(EntityID entityID, const TComponent& newValue);
1983
1992 void updateComponent(EntityID entityID, const nlohmann::json& newValue);
1993
2004 template<typename TComponent, typename TSystem>
2005 void updateComponent(EntityID entityID, const TComponent& newValue);
2006
2016 template<typename TSystem>
2017 void updateComponent(EntityID entityID, const nlohmann::json& newValue);
2018
2027 template<typename TComponent>
2028 void removeComponent(EntityID entityID);
2029
2036 void removeComponent(EntityID entityID, const std::string& typeName);
2037
2043 void removeComponentsAll(EntityID entityID);
2044
2049 std::unique_ptr<ComponentManager> mComponentManager {nullptr};
2050
2055 std::unique_ptr<SystemManager> mSystemManager {nullptr};
2056
2061 std::vector<EntityID> mDeletedIDs {};
2062
2070
2076
2082
2083 friend class Entity;
2084 friend class BaseSystem;
2085 };
2086
2095 class Entity {
2096 public:
2102 Entity(const Entity& other);
2108 Entity(Entity&& other) noexcept;
2109
2116 Entity& operator=(const Entity& other);
2117
2124 Entity& operator=(Entity&& other) noexcept;
2125
2130 ~Entity();
2131
2137 inline EntityID getID() { return mID; };
2138
2146 void copy(const Entity& other);
2147
2156 template<typename TComponent>
2157 void addComponent(const TComponent& component);
2158
2166 void addComponent(const nlohmann::json& jsonComponent);
2167
2175 template<typename TComponent>
2176 void removeComponent();
2177
2185 void removeComponent(const std::string& typeName);
2186
2187
2197 template<typename TComponent>
2198 bool hasComponent() const;
2199
2209 bool hasComponent(const std::string& typeName) const;
2210
2220 template<typename TComponent>
2221 TComponent getComponent(float simulationProgress=1.f) const;
2222
2231 template<typename TComponent>
2232 void updateComponent(const TComponent& newValue);
2233
2241 void updateComponent(const nlohmann::json& jsonValue);
2242
2252 template<typename TSystem>
2253 bool isEnabled() const;
2254
2264 template <typename TSystem>
2265 bool isRegistered() const;
2266
2274 template<typename TSystem>
2275 void enableSystem();
2276
2284 template<typename TSystem>
2285 void disableSystem();
2286
2292 void disableSystems();
2293
2299 void enableSystems(Signature systemMask);
2300
2306 inline std::weak_ptr<ECSWorld> getWorld() { return mWorld; }
2307
2313 void joinWorld(ECSWorld& world);
2314
2315 private:
2322 Entity(EntityID entityID, std::shared_ptr<ECSWorld> world): mID{ entityID }, mWorld{ world } {};
2323
2329
2334 std::weak_ptr<ECSWorld> mWorld;
2335 friend class ECSWorld;
2336 };
2337
2338
2345 template<typename T>
2346 T Interpolator<T>::operator() (const T& previousState, const T& nextState, float simulationProgress) const {
2347 if(simulationProgress < .5f) return previousState;
2348 return nextState;
2349 }
2350
2351 template<typename TComponent>
2352 void ComponentArray<TComponent>::addComponent(EntityID entityID, const TComponent& component) {
2353 assert(mEntityToComponentIndex.find(entityID) == mEntityToComponentIndex.end() && "Component already added for this entity");
2354
2355 std::size_t newComponentID { mComponentsNext.size() };
2356
2357 mComponentsNext.push_back(component);
2358 mComponentsPrevious.push_back(component);
2359 mEntityToComponentIndex[entityID] = newComponentID;
2360 mComponentToEntity[newComponentID] = entityID;
2361 }
2362
2363 template <typename TComponent>
2364 void ComponentArray<TComponent>::addComponent(EntityID entityID, const nlohmann::json& jsonComponent) {
2365 addComponent(entityID, ComponentFromJSON<TComponent>::get(jsonComponent));
2366 }
2367
2368 template <typename TComponent>
2369 void ComponentArray<TComponent>::updateComponent(EntityID entityID, const nlohmann::json& jsonComponent) {
2370 updateComponent(entityID, ComponentFromJSON<TComponent>::get(jsonComponent));
2371 }
2372
2373 template<typename TComponent>
2375 return mEntityToComponentIndex.find(entityID) != mEntityToComponentIndex.end();
2376 }
2377
2378 template<typename TComponent>
2380 assert(mEntityToComponentIndex.find(entityID) != mEntityToComponentIndex.end());
2381
2382 const std::size_t removedComponentIndex { mEntityToComponentIndex[entityID] };
2383 const std::size_t lastComponentIndex { mComponentsNext.size() - 1 };
2384 const std::size_t lastComponentEntity { mComponentToEntity[lastComponentIndex] };
2385
2386 // store last component in the removed components place
2387 mComponentsNext[removedComponentIndex] = mComponentsNext[lastComponentIndex];
2388 mComponentsPrevious[removedComponentIndex] = mComponentsPrevious[lastComponentIndex];
2389 // map the last component's entity to its new index
2390 mEntityToComponentIndex[lastComponentEntity] = removedComponentIndex;
2391 // map the removed component's index to the last entity
2392 mComponentToEntity[removedComponentIndex] = lastComponentEntity;
2393
2394 // erase all traces of the removed entity's component and other
2395 // invalid references
2396 mComponentsNext.pop_back();
2397 mComponentsPrevious.pop_back();
2398 mEntityToComponentIndex.erase(entityID);
2399 mComponentToEntity.erase(lastComponentIndex);
2400 }
2401
2402 template <typename TComponent>
2403 TComponent ComponentArray<TComponent>::getComponent(EntityID entityID, float simulationProgress) const {
2404 static Interpolator<TComponent> interpolator{};
2405 assert(mEntityToComponentIndex.find(entityID) != mEntityToComponentIndex.end());
2406 std::size_t componentID { mEntityToComponentIndex.at(entityID) };
2407 return interpolator(mComponentsPrevious[componentID], mComponentsNext[componentID], simulationProgress);
2408 }
2409
2410 template <typename TComponent>
2411 void ComponentArray<TComponent>::updateComponent(EntityID entityID, const TComponent& newComponent) {
2412 assert(mEntityToComponentIndex.find(entityID) != mEntityToComponentIndex.end());
2413 std::size_t componentID { mEntityToComponentIndex.at(entityID) };
2414 mComponentsNext[componentID] = newComponent;
2415 }
2416
2417 template <typename TComponent>
2419 if(mEntityToComponentIndex.find(entityID) != mEntityToComponentIndex.end()) {
2420 removeComponent(entityID);
2421 }
2422 }
2423
2424 template<typename TComponent>
2426 std::copy<typename std::vector<TComponent>::iterator, typename std::vector<TComponent>::iterator>(
2427 mComponentsNext.begin(), mComponentsNext.end(),
2428 mComponentsPrevious.begin()
2429 );
2430 }
2431
2432 template <typename TComponent>
2434 copyComponent(to, from, *this);
2435 }
2436
2437 template <typename TComponent>
2439 assert(to < kMaxEntities && "Cannot copy to an entity with an invalid entity ID");
2440 ComponentArray<TComponent>& downcastOther { static_cast<ComponentArray<TComponent>&>(other) };
2441 if(downcastOther.mEntityToComponentIndex.find(from) == downcastOther.mEntityToComponentIndex.end()) return;
2442
2443 const TComponent& componentValueNext { downcastOther.mComponentsNext[downcastOther.mEntityToComponentIndex[from]] };
2444 const TComponent& componentValuePrevious { downcastOther.mComponentsPrevious[downcastOther.mEntityToComponentIndex[from]] };
2445
2446 if(mEntityToComponentIndex.find(to) == mEntityToComponentIndex.end()) {
2447 addComponent(to, componentValueNext);
2448 } else {
2449 mComponentsNext[mEntityToComponentIndex[to]] = componentValueNext;
2450 }
2451 mComponentsPrevious[mEntityToComponentIndex[to]] = componentValuePrevious;
2452 }
2453
2454 template<typename TComponent>
2456 const std::size_t componentHash { typeid(TComponent).hash_code() };
2457 // nop when a component array for this type already exists
2458 if(mHashToComponentType.find(componentHash) != mHashToComponentType.end()) {
2459 return;
2460 }
2461
2462 std::string componentTypeName { getComponentTypeName<TComponent>{}() };
2463 assert(mHashToComponentType.size() + 1 < kMaxComponents && "Component type limit reached");
2464 assert(mNameToComponentHash.find(componentTypeName) == mNameToComponentHash.end() && "Another component with this name\
2465 has already been registered");
2466
2467 mNameToComponentHash.insert_or_assign(componentTypeName, componentHash);
2468 mHashToComponentArray.insert_or_assign(
2469 componentHash, std::static_pointer_cast<BaseComponentArray>(std::make_shared<ComponentArray<TComponent>>(mWorld))
2470 );
2471 mHashToComponentType[componentHash] = mHashToComponentType.size();
2472 }
2473
2474 template<typename TComponent>
2476 const std::size_t componentHash { typeid(TComponent).hash_code() };
2477 assert(mHashToComponentType.find(componentHash) != mHashToComponentType.end() && "Component type has not been registered");
2478 return mHashToComponentType.at(componentHash);
2479 }
2480
2481 template<typename TComponent>
2482 void ComponentManager::addComponent(EntityID entityID, const TComponent& component) {
2483 getComponentArray<TComponent>()->addComponent(entityID, component);
2484 mEntityToSignature[entityID].set(getComponentType<TComponent>(), true);
2485 }
2486
2487 template <typename TComponent>
2489 return getComponentArray<TComponent>()->hasComponent(entityID);
2490 }
2491
2492 template<typename TComponent>
2494 getComponentArray<TComponent>()->removeComponent(entityID);
2495 mEntityToSignature[entityID].set(getComponentType<TComponent>(), false);
2496 }
2497
2498 template<typename TComponent>
2499 TComponent ComponentManager::getComponent(EntityID entityID, float simulationProgress) const {
2500 return getComponentArray<TComponent>()->getComponent(entityID, simulationProgress);
2501 }
2502
2503 template<typename TComponent>
2504 void ComponentManager::updateComponent(EntityID entityID, const TComponent& newValue) {
2505 getComponentArray<TComponent>()->updateComponent(entityID, newValue);
2506 }
2507
2508 template <typename TComponent>
2510 assert(mEntityToSignature[from].test(getComponentType<TComponent>()) && "The entity being copied from does not have this component");
2511 getComponentArray<TComponent>()->copyComponent(to, from);
2513 }
2514
2515 template<typename TSystem>
2517 const std::string systemTypeName{ TSystem::getSystemTypeName() };
2518 assert(mNameToSystemType.find(systemTypeName) != mNameToSystemType.end() && "Component type has not been registered");
2519 return mNameToSystemType.at(systemTypeName);
2520 }
2521
2522 template <typename TSystem>
2524 return mSystemManager->isEnabled<TSystem>(entityID);
2525 }
2526 template <typename TSystem>
2528 return mSystemManager->isRegistered<TSystem>(entityID);
2529 }
2530
2531 template <typename TComponent>
2532 bool ECSWorld::hasComponent(EntityID entityID) const {
2533 return mComponentManager->hasComponent<TComponent>(entityID);
2534 }
2535
2536
2537 template <typename TSystemDerived, typename ...TListenedForComponents, typename ...TRequiredComponents>
2538 void System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>::registerSelf() {
2539 ECSWorld::registerComponentTypes<TRequiredComponents...>();
2540 ECSWorld::registerComponentTypes<TListenedForComponents...>();
2541 ECSWorld::registerSystem(ECSWorld::SystemRegistrationArgs<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>{});
2542 }
2543
2544
2545 template<typename TSystem>
2546 void SystemManager::registerSystem(const Signature& signature, const Signature& listenedForComponents) {
2547 const std::string systemTypeName { TSystem::getSystemTypeName() };
2548 assert(mNameToSignature.find(systemTypeName) == mNameToSignature.end() && "System has already been registered");
2549 assert(mNameToSystemType.size() + 1 < kMaxSystems && "System type limit reached");
2550
2551 mNameToSignature[systemTypeName] = signature;
2552 mNameToListenedForComponents[systemTypeName] = listenedForComponents;
2553 mNameToSystem.insert_or_assign(systemTypeName, std::make_shared<TSystem>(mWorld));
2554 mNameToSystemType[systemTypeName] = mNameToSystemType.size();
2555 }
2556
2557 template<typename TSystem>
2558 std::shared_ptr<TSystem> SystemManager::getSystem() {
2559 std::string systemTypeName { TSystem::getSystemTypeName() };
2560 assert(mNameToSignature.find(systemTypeName) != mNameToSignature.end() && "System has not yet been registered");
2561 return std::dynamic_pointer_cast<TSystem>(mNameToSystem[systemTypeName]);
2562 }
2563
2564 template<typename TSystem>
2566 std::string systemTypeName { TSystem::getSystemTypeName() };
2567 mNameToSystem[systemTypeName]->enableEntity(entityID);
2568 }
2569
2570 template<typename TSystem>
2572 std::string systemTypeName { TSystem::getSystemTypeName() };
2573 mNameToSystem[systemTypeName]->disableEntity(entityID);
2574 }
2575
2576 template <typename TComponent>
2577 void Entity::addComponent(const TComponent& component) {
2578 mWorld.lock()->addComponent<TComponent>(mID, component);
2579 }
2580
2581 template <typename TComponent>
2583 return mWorld.lock()->hasComponent<TComponent>(mID);
2584 }
2585
2586 template<typename TComponent>
2587 TComponent Entity::getComponent(float simulationProgress) const {
2588 return mWorld.lock()->getComponent<TComponent>(mID, simulationProgress);
2589 }
2590
2591 template<typename TComponent>
2592 void Entity::updateComponent(const TComponent& newValue) {
2593 mWorld.lock()->updateComponent<TComponent>(mID, newValue);
2594 }
2595
2596 template <typename TComponent>
2598 mWorld.lock()->removeComponent<TComponent>(mID);
2599 }
2600
2601 template <typename TSystem>
2603 mWorld.lock()->enableEntity<TSystem>(mID);
2604 }
2605
2606 template <typename TSystem>
2607 bool Entity::isEnabled() const {
2608 return mWorld.lock()->isEnabled<TSystem>(mID);
2609 }
2610
2611 template <typename TSystem>
2613 return mWorld.lock()->isRegistered<TSystem>(mID);
2614 }
2615
2616 template <typename TSystem>
2618 mWorld.lock()->disableEntity<TSystem>(mID);
2619 }
2620
2621 template <typename TSystem>
2623 const std::string systemTypeName { TSystem::getSystemTypeName() };
2624 return mNameToSystem[systemTypeName]->isEnabled(entityID);
2625 }
2626
2627 template <typename TSystem>
2629 const std::string systemTypeName { TSystem::getSystemTypeName() };
2630 return mNameToSystem[systemTypeName]->isRegistered(entityID);
2631 }
2632
2633 template<typename ...TComponents>
2634 Entity ECSWorld::privateCreateEntity(TComponents...components) {
2635 assert((mNextEntity < kMaxEntities || !mDeletedIDs.empty()) && "Max number of entities reached");
2636
2637 EntityID nextID;
2638 if(!mDeletedIDs.empty()){
2639 nextID = mDeletedIDs.back();
2640 mDeletedIDs.pop_back();
2641 } else {
2642 nextID = mNextEntity++;
2643 }
2644
2645 Entity entity { nextID, shared_from_this()};
2646
2647 (addComponent<TComponents>(nextID, components), ...);
2648 return entity;
2649 }
2650
2651 template<typename TSystem>
2653 return mSystemManager->getSystemType<TSystem>();
2654 }
2655
2656 template<typename TComponent>
2657 void ECSWorld::addComponent(EntityID entityID, const TComponent& component) {
2658 assert(entityID < kMaxEntities && "Cannot add a component to an entity that does not exist");
2659 mComponentManager->addComponent<TComponent>(entityID, component);
2660 Signature signature { mComponentManager->getSignature(entityID) };
2661 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2662 }
2663
2664 template<typename TComponent>
2666 mComponentManager->removeComponent<TComponent>(entityID);
2667 Signature signature { mComponentManager->getSignature(entityID) };
2668 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2669 }
2670
2671 template<typename TSystem>
2673 mSystemManager->enableEntity<TSystem>(entityID);
2674 }
2675
2676 template<typename TSystem>
2678 mSystemManager->disableEntity<TSystem>(entityID);
2679 }
2680
2681 template<typename TComponent>
2682 TComponent ECSWorld::getComponent(EntityID entityID, float progress) const {
2683 return mComponentManager->getComponent<TComponent>(entityID, progress);
2684 }
2685
2686 template<typename TComponent, typename TSystem>
2687 TComponent ECSWorld::getComponent(EntityID entityID, float progress) const {
2688 assert(
2689 (
2690 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2691 .test(mComponentManager->getComponentType<TComponent>())
2692 )
2693 && "This system cannot access this kind of component"
2694 );
2695 return getComponent<TComponent>(entityID, progress);
2696 }
2697
2698
2699 template<typename TComponent>
2700 void ECSWorld::updateComponent(EntityID entityID, const TComponent& newValue) {
2701 mComponentManager->updateComponent<TComponent>(entityID, newValue);
2702 mSystemManager->handleEntityUpdated(
2703 entityID,
2704 mComponentManager->getSignature(entityID),
2705 mComponentManager->getComponentType<TComponent>()
2706 );
2707 }
2708
2709 template<typename TComponent, typename TSystem>
2710 void ECSWorld::updateComponent(EntityID entityID, const TComponent& newValue) {
2711 assert(
2712 (
2713 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2714 .test(mComponentManager->getComponentType<TComponent>())
2715 )
2716 && "This system cannot access this kind of component"
2717 );
2718 mComponentManager->updateComponent<TComponent>(entityID, newValue);
2719 mSystemManager->handleEntityUpdatedBySystem<TSystem>(
2720 entityID,
2721 mComponentManager->getSignature(entityID),
2722 mComponentManager->getComponentType<TComponent>()
2723 );
2724 }
2725
2726 template <typename TSystem>
2727 void ECSWorld::updateComponent(EntityID entityID, const nlohmann::json& newValue) {
2728 assert(
2729 (
2730 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2731 .test(mComponentManager->getComponentType(newValue.at("type")))
2732 )
2733 && "This system cannot access this kind of component"
2734 );
2735 mComponentManager->updateComponent(entityID, newValue);
2736 mSystemManager->handleEntityUpdatedBySystem<TSystem>(
2737 entityID,
2738 mComponentManager->getSignature(entityID),
2739 mComponentManager->getComponentType(newValue.at("type"))
2740 );
2741 }
2742
2743 template<typename ...TComponents>
2745 ((getInstance().lock()->mComponentManager->registerComponentArray<TComponents>()),...);
2746 }
2747
2748 template<typename TSystem, typename ...TListenedForComponents, typename ...TRequiredComponents>
2750 ECSWorld::SystemRegistrationArgs<TSystem, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>
2751 ) {
2752 Signature listensFor {};
2753 Signature required {};
2754
2755 (listensFor.set(getInstance().lock()->mComponentManager->getComponentType<TListenedForComponents>()), ...);
2756 (required.set(getInstance().lock()->mComponentManager->getComponentType<TRequiredComponents>()), ...);
2757
2758 getInstance().lock()->mSystemManager->registerSystem<TSystem>(required|listensFor, listensFor);
2759 }
2760
2761 template<typename ...TComponents>
2762 Entity ECSWorld::createEntity(TComponents...components) {
2763 return privateCreateEntity<TComponents...>(components...);
2764 }
2765
2766 template <typename ...TComponents>
2767 Entity ECSWorld::createEntityPrototype(TComponents...components) {
2768 return ECSWorld::getInstance().lock()->privateCreateEntity<TComponents...>(components...);
2769 }
2770
2771 template<typename TSystem>
2772 std::shared_ptr<TSystem> ECSWorld::getSystem() {
2773 return mSystemManager->getSystem<TSystem>();
2774 }
2775
2776 template<typename TSystem>
2777 std::shared_ptr<TSystem> ECSWorld::getSystemPrototype() {
2778 return getInstance().lock()->mSystemManager->getSystem<TSystem>();
2779 }
2780
2781 template <typename TSingletonSystem>
2782 std::shared_ptr<TSingletonSystem> ECSWorld::getSingletonSystem() {
2783 std::shared_ptr<TSingletonSystem> system { getInstance().lock()->getSystem<TSingletonSystem>() };
2784 assert(system->isSingleton() && "System specified is not an ECSWorld-aware singleton system");
2785 return system;
2786 }
2787
2788 template<typename TComponent, typename TSystem>
2789 TComponent BaseSystem::getComponent_(EntityID entityID, float progress) const {
2790 assert(!isSingleton() && "Singletons cannot retrieve entity components through entity ID alone");
2791 return mWorld.lock()->getComponent<TComponent, TSystem>(entityID, progress);
2792 }
2793 template<typename TSystem, typename ...TListenedForComponents, typename ...TRequiredComponents>
2794 std::shared_ptr<BaseSystem> System<TSystem, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>::instantiate(std::weak_ptr<ECSWorld> world) {
2795 if(isSingleton()) return shared_from_this();
2796 return std::make_shared<TSystem>(world);
2797 }
2798
2799 template <typename TComponent>
2800 std::shared_ptr<BaseComponentArray> ComponentArray<TComponent>::instantiate(std::weak_ptr<ECSWorld> world) const {
2801 return std::make_shared<ComponentArray<TComponent>>(world);
2802 }
2803
2804 template<typename TComponent, typename TSystem>
2805 void BaseSystem::updateComponent_(EntityID entityID, const TComponent& component) {
2806 assert(!isSingleton() && "Singletons cannot retrieve entity components through entity ID alone");
2807 mWorld.lock()->updateComponent<TComponent, TSystem>(entityID, component);
2808 }
2809
2810 template<typename TSystem>
2812 std::string originatingSystemTypeName { TSystem::getSystemTypeName() };
2813 for(const auto& pair: mNameToSignature) {
2814 // see if the updated entity's signature matches that of the system
2815 if((pair.second&signature) != pair.second) continue;
2816
2817 // suppress update callback from the system that caused this update
2818 if(pair.first == originatingSystemTypeName) continue;
2819
2820 // see if the system is listening for updates to this system
2821 if(!mNameToListenedForComponents[pair.first].test(updatedComponent)) continue;
2822
2823 // ignore disabled and singleton systems
2824 BaseSystem& system { *(mNameToSystem[pair.first]).get() };
2825 if(system.isSingleton() || !system.isEnabled(entityID)) continue;
2826
2827 // apply update
2828 system.onEntityUpdated(entityID);
2829 }
2830 }
2831
2832}
2833#endif
An abstract base class for all ECS component arrays.
Definition ecs_world.hpp:168
virtual void copyComponent(EntityID to, EntityID from, BaseComponentArray &other)=0
Handles the copying of a component from one entity to another, where the other entity belongs to anot...
virtual void updateComponent(EntityID to, const nlohmann::json &jsonComponent)=0
Updates the component associated with an entity based on a json description of a new component.
virtual ~BaseComponentArray()=default
Destroy the Base Component Array object.
virtual std::shared_ptr< BaseComponentArray > instantiate(std::weak_ptr< ECSWorld > world) const =0
Creates a fresh, empty component array and associates it with a new World.
std::weak_ptr< ECSWorld > mWorld
A reference to the world to which this component array belongs.
Definition ecs_world.hpp:262
virtual void handleEntityDestroyed(EntityID entityID)=0
A virtual function that handles the side-effect of destroying an entity, i.e., deleting its component...
virtual void copyComponent(EntityID to, EntityID from)=0
Handles the copying of a component from one entity to another within the same array.
virtual void addComponent(EntityID to, const nlohmann::json &jsonComponent)=0
Constructs and adds a component to an array based on its json description.
BaseComponentArray(std::weak_ptr< ECSWorld > world)
Construct a new Base Component Array object.
Definition ecs_world.hpp:176
virtual bool hasComponent(EntityID entityID) const =0
Tests whether this array has an entry for this entity.
virtual void removeComponent(EntityID entityID)=0
Removes the component associated with this entity, if present.
virtual void handlePreSimulationStep()=0
An unimplemented callback for a step that occurs before each simulation step.
The base class that acts as the interface between the engine's ECS system and a particular built-in o...
Definition ecs_world.hpp:814
virtual void onInitialize()
An overridable callback for right after an ECS world has just been created.
Definition ecs_world.hpp:958
virtual void onSimulationDeactivated()
Overridable callback called just after the ECS world owning this system has been deactivated.
Definition ecs_world.hpp:1027
virtual void onPreRenderStep(float simulationProgress)
Overridable callback called just before the render step takes place.
Definition ecs_world.hpp:1012
virtual void onSimulationPostStep(uint32_t simStepMillis)
An overridable callback called once at the end of this simulation step, and after related transform u...
Definition ecs_world.hpp:987
void updateComponent_(EntityID entityID, const TComponent &component)
The actual implementation of updateComponent for a system.
Definition ecs_world.hpp:2805
virtual ~BaseSystem()=default
Destroy the Base System object.
void removeEntity(EntityID entityID)
Removes an entity from this system.
Definition ecs_world.cpp:127
virtual void onVariableStep(float simulationProgress, uint32_t variableStepMillis)
Overridable callback called after all simulation updates (if any) for the current frame have been com...
Definition ecs_world.hpp:1005
void enableEntity(EntityID entityID)
Allows a registered entity to be influenced by this system.
Definition ecs_world.cpp:80
std::weak_ptr< ECSWorld > mWorld
A reference to the world this system belongs to.
Definition ecs_world.hpp:898
virtual void onEntityUpdated(EntityID entityID)
An overridable callback for when another system has updated a component shared by this system and an ...
Definition ecs_world.hpp:952
virtual void onEntityDisabled(EntityID entityID)
An overridable callback for when an entity has been disabled.
Definition ecs_world.hpp:945
void disableEntity(EntityID entityID)
Prevents the influencing of an entity by this system.
Definition ecs_world.cpp:99
BaseSystem(std::weak_ptr< ECSWorld > world)
Construct a new Base System object.
Definition ecs_world.hpp:821
virtual void onPostTransformUpdate(uint32_t timeStepMillis)
An overridable callback called after all the transforms in the scene are updated by the scene system.
Definition ecs_world.hpp:994
virtual std::shared_ptr< BaseSystem > instantiate(std::weak_ptr< ECSWorld > world)=0
Creates a fresh copy of this system and associates it with a new ECS World, using this system as its ...
bool isEnabled(EntityID entityID) const
Tests whether a particular entity is active for this system.
Definition ecs_world.cpp:136
virtual void onEntityEnabled(EntityID entityID)
An overridable callback for when an entity has been enabled.
Definition ecs_world.hpp:936
std::set< EntityID > mEnabledEntities
A set of all entities that are actively influenced by this system, managed by this system's ECS world...
Definition ecs_world.hpp:1041
virtual void onDestroyed()
Overridable callback called just before this system is destroyed.
Definition ecs_world.hpp:1035
virtual void onSimulationActivated()
An overridable callback for right after the ECS World has been activated.
Definition ecs_world.hpp:964
virtual void onSimulationStep(uint32_t simStepMillis)
An overridable callback called once in the middle of every simulation step.
Definition ecs_world.hpp:980
const std::set< EntityID > & getEnabledEntities()
Get a set of all entities that are influenced by this System.
Definition ecs_world.cpp:132
virtual void onPostRenderStep(float simulationProgress)
Overridable callback called just after the render step takes place.
Definition ecs_world.hpp:1019
virtual bool isSingleton() const
A method to query whether a particular System is a singleton, or is instantiated for each world in th...
Definition ecs_world.hpp:835
void addEntity(EntityID entityID, bool enabled=true)
Adds an entity to this system.
Definition ecs_world.cpp:111
std::set< EntityID > mDisabledEntities
A set of all entities that are compatible with this system, but have not been enabled for it.
Definition ecs_world.hpp:1047
virtual void onSimulationPreStep(uint32_t simStepMillis)
An overridable callback called once at the beginning of every simulation step in the game loop.
Definition ecs_world.hpp:971
TComponent getComponent_(EntityID entityID, float progress=1.f) const
The actual implementation of getComponent for a system.
Definition ecs_world.hpp:2789
bool isRegistered(EntityID entityID) const
Tests whether a particular entity can be influenced by this system.
Definition ecs_world.cpp:140
A class that implements BaseComponentArray specializing it for a component of type TComponent.
Definition ecs_world.hpp:366
std::shared_ptr< BaseComponentArray > instantiate(std::weak_ptr< ECSWorld > world) const override
Creates a new component array of the same type as this and associated with a new World.
Definition ecs_world.hpp:2800
std::unordered_map< std::size_t, EntityID > mComponentToEntity
A mapping from the index of a component to the ID of the entity that owns that component.
Definition ecs_world.hpp:500
void removeComponent(EntityID entityID) override
Removes the component associated with a specific entity, maintaining packing but not order.
Definition ecs_world.hpp:2379
virtual void handlePreSimulationStep() override
A callback for the start of a simulation step.
Definition ecs_world.hpp:2425
void addComponent(EntityID entityID, const TComponent &component)
Adds a component belonging to this entity to this array.
Definition ecs_world.hpp:2352
ComponentArray(std::weak_ptr< ECSWorld > world)
Construct a new Component Array object.
Definition ecs_world.hpp:373
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates the value of the component belonging to this entity.
Definition ecs_world.hpp:2411
std::vector< TComponent > mComponentsPrevious
An array containing the state of each entity's component as seen in the last simulation tick.
Definition ecs_world.hpp:486
bool hasComponent(EntityID entityID) const override
Tests whether an entry for a component belonging to this entity is present.
Definition ecs_world.hpp:2374
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component object.
Definition ecs_world.hpp:2403
std::unordered_map< EntityID, std::size_t > mEntityToComponentIndex
A mapping from the ID of an entity to the index in the component array that stores the entity's compo...
Definition ecs_world.hpp:493
virtual void handleEntityDestroyed(EntityID entityID) override
A callback to handle the side effect of the destruction of an entity. Here: deletion of the component...
Definition ecs_world.hpp:2418
virtual void copyComponent(EntityID to, EntityID from) override
Handles the copying of a component belonging to one entity to another.
Definition ecs_world.hpp:2433
std::vector< TComponent > mComponentsNext
An array containing the state of each entity's component as will be seen at the start of the next sim...
Definition ecs_world.hpp:480
An object that stores and manages updates to all the component arrays instantiated for this ECS World...
Definition ecs_world.hpp:511
void unregisterAll()
Unregisters all component arrays associated with this manager, as part of the destruction process for...
Definition ecs_world.cpp:393
Signature getSignature(EntityID entityID)
Get the component signature for a given entity.
Definition ecs_world.cpp:348
void copyComponent(EntityID to, EntityID from)
Copies a component value from one component to another within the same component array.
Definition ecs_world.hpp:2509
void handlePreSimulationStep()
Callback for the start of a simulation step, where the contents of every component's next member is c...
Definition ecs_world.cpp:478
std::unordered_map< EntityID, Signature > mEntityToSignature
Stores the component signature of each entity.
Definition ecs_world.hpp:798
void addComponent(EntityID entityID, const TComponent &component)
Adds a component entry for this entity in the array specified.
Definition ecs_world.hpp:2482
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component value for this entity.
Definition ecs_world.hpp:2499
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with its new value.
Definition ecs_world.hpp:2504
std::shared_ptr< BaseComponentArray > getComponentArray(const std::string &componentTypeName) const
Get the Component Array object.
Definition ecs_world.hpp:586
std::unordered_map< std::size_t, ComponentType > mHashToComponentType
Maps a component's type hash to its ComponentType.
Definition ecs_world.hpp:781
void copyComponents(EntityID to, EntityID from)
Copies all components from one entity and updates or adds them to the other.
Definition ecs_world.cpp:357
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a particular type.
Definition ecs_world.hpp:2488
std::unordered_map< std::size_t, std::shared_ptr< BaseComponentArray > > mHashToComponentArray
Maps the hash of a component type to its corresponding ComponentArray.
Definition ecs_world.hpp:787
std::unordered_map< std::string, std::size_t > mNameToComponentHash
Maps each component type name to its corresponding hash.
Definition ecs_world.hpp:772
std::shared_ptr< ComponentArray< TComponent > > getComponentArray() const
Get the (specialized) Component Array object.
Definition ecs_world.hpp:574
void removeComponent(EntityID entityID)
Removes the component of the type specified from the entity.
Definition ecs_world.hpp:2493
ComponentManager(std::weak_ptr< ECSWorld > world)
Construct a new Component Manager object.
Definition ecs_world.hpp:518
std::weak_ptr< ECSWorld > mWorld
Holds a reference to the ECS world this component manager manages component arrays for.
Definition ecs_world.hpp:804
void handleEntityDestroyed(EntityID entityID)
Handles component arrays specific side effect of entity destruction.
Definition ecs_world.cpp:369
void registerComponentArray()
A method to allow a new component type to register itself with the ECS system for this project.
Definition ecs_world.hpp:2455
ComponentManager instantiate(std::weak_ptr< ECSWorld > world) const
Constructs a new component manager and associates it with the world passed in as argument....
Definition ecs_world.cpp:313
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2475
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1439
SystemType getSystemType()
Get the SystemType id for a particular system.
Definition ecs_world.hpp:2652
void cleanup()
Loses references to member systems, component arrays, and entities, resulting in their destruction.
Definition ecs_world.cpp:539
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for membership with a system, per its component signature.
Definition ecs_world.hpp:2527
void initialize()
Runs the initialization step for this world and the systems that belong to it.
Definition ecs_world.cpp:496
bool isEnabled(EntityID entityID)
Tests whether a particular entity is enabled for a particular system.
Definition ecs_world.hpp:2523
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2767
std::shared_ptr< ECSWorld > instantiate() const
Creates a new ECSWorld using the systems and component arrays present in this one as a template.
Definition ecs_world.cpp:449
void simulationPreStep(uint32_t simStepMillis)
Runs callbacks associated with the start of a simulation update.
Definition ecs_world.cpp:515
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a specific type.
Definition ecs_world.hpp:2532
static void registerComponentTypes()
Registers ComponentArrays for each type of component present in the component type list,...
Definition ecs_world.hpp:2744
void enableEntity(EntityID entityID)
Enables an entity for a specific system.
Definition ecs_world.hpp:2672
EntityID mNextEntity
A new EntityID that has never been created before in this world.
Definition ecs_world.hpp:2069
ECSWorld()=default
Construct a new ECSWorld object.
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get a component associated with an entity.
Definition ecs_world.hpp:2682
static std::weak_ptr< const ECSWorld > getPrototype()
Get the prototype ECSWorld.
Definition ecs_world.cpp:433
static std::shared_ptr< TSingletonSystem > getSingletonSystem()
Get a system that has been marked as a singleton System.
Definition ecs_world.hpp:2782
void simulationPostStep(uint32_t simStepMillis)
Runs calbacks associated with the end of the simulation step.
Definition ecs_world.cpp:522
void simulationStep(uint32_t simStepMillis)
Runs callbacks associated with the simulation step.
Definition ecs_world.cpp:519
WorldID getID() const
Gets the ID associated with this world.
Definition ecs_world.hpp:1790
void destroyEntity(EntityID entityID)
Destroys an Entity with a specific ID.
Definition ecs_world.cpp:471
std::vector< EntityID > mDeletedIDs
A list of previously existing entity IDs that were since retired and are available to be used again.
Definition ecs_world.hpp:2061
void relocateEntity(Entity &entity)
Moves an entity from one ECSWorld to this one.
Definition ecs_world.cpp:61
void copyComponents(EntityID to, EntityID from)
Copies components from one entity to another within a single ECSWorld.
Definition ecs_world.cpp:485
void preRenderStep(float simulationProgress)
Runs callbacks associated with the start of the rendering step.
Definition ecs_world.cpp:532
std::unique_ptr< SystemManager > mSystemManager
A reference to this world SystemManager.
Definition ecs_world.hpp:2055
void activateSimulation()
Marks this world as an active one, calling the activation callbacks of systems that belong to it.
Definition ecs_world.cpp:499
void removeComponent(EntityID entityID)
Removes a component from an entity.
Definition ecs_world.hpp:2665
Entity privateCreateEntity(TComponents...components)
Creates a new entity and assigns it the components specified as arguments.
Definition ecs_world.hpp:2634
WorldID mID
The unique ID associated with this ECSWorld.
Definition ecs_world.hpp:2075
std::unique_ptr< ComponentManager > mComponentManager
A reference to this world's ComponentManager.
Definition ecs_world.hpp:2049
static std::shared_ptr< TSystem > getSystemPrototype()
Get the system object of a specific type belonging to the prototype ECSWorld.
Definition ecs_world.hpp:2777
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with a new value.
Definition ecs_world.hpp:2700
Entity createEntity(TComponents...components)
Creates a new entity that will exist in this ECSWorld.
Definition ecs_world.hpp:2762
void postRenderStep(float simulationProgress)
Runs callbacks associated with the end of the rendering step.
Definition ecs_world.cpp:535
void postTransformUpdate(uint32_t timeStepMillis)
Runs callbacks associated with the end of transform updates.
Definition ecs_world.cpp:526
std::shared_ptr< TSystem > getSystem()
Get the system object of a specific type belonging to this ECSWorld.
Definition ecs_world.hpp:2772
void disableEntity(EntityID entityID)
Disables an entity on a particular system.
Definition ecs_world.hpp:2677
void variableStep(float simulationProgress, uint32_t variableStepMillis)
Runs callbacks associated with the variable step of the game loop.
Definition ecs_world.cpp:529
void deactivateSimulation()
Marks ths world as an inactive one, and suspends operation for its member systems.
Definition ecs_world.cpp:502
static void registerSystem(SystemRegistrationArgs< TSystemDerived, std::tuple< TListenedForComponents... >, std::tuple< TRequiredComponents... > >)
Registers a System with the ECS System for this project.
static WorldID s_nextWorld
The ID the next ECSWorld to be instantiated will receive.
Definition ecs_world.hpp:2081
static std::weak_ptr< ECSWorld > getInstance()
Creates an instance of the prototype ECSWorld, and returns a reference to it.
Definition ecs_world.cpp:444
void addComponent(EntityID entityID, const TComponent &component)
Adds a new component to the entity.
Definition ecs_world.hpp:2657
The Entity is a wrapper on an entity ID, used as the primary interface between an application and the...
Definition ecs_world.hpp:2095
void addComponent(const TComponent &component)
Adds a new component to this Entity.
Definition ecs_world.hpp:2577
EntityID getID()
Gets the ID associated with this Entity.
Definition ecs_world.hpp:2137
std::weak_ptr< ECSWorld > mWorld
The world this Entity belongs to.
Definition ecs_world.hpp:2334
void updateComponent(const TComponent &newValue)
Updates the value of a component belonging to this Entity.
Definition ecs_world.hpp:2592
void disableSystem()
Disables this entity for a particular system.
Definition ecs_world.hpp:2617
EntityID mID
The ID of this entity within its owning ECSWorld.
Definition ecs_world.hpp:2328
Entity & operator=(const Entity &other)
Copies an Entity object, replacing any component values currently present on this one.
Definition ecs_world.cpp:31
bool hasComponent() const
Tests whether this entity has a particular component.
Definition ecs_world.hpp:2582
std::weak_ptr< ECSWorld > getWorld()
Get the ECSWorld object this Entity belongs to.
Definition ecs_world.hpp:2306
void enableSystem()
Enables this Entity for a particular System.
Definition ecs_world.hpp:2602
Entity(const Entity &other)
Construct a new Entity object.
Definition ecs_world.cpp:15
bool isRegistered() const
Tests whether this entity is eligible for participation with a given system.
Definition ecs_world.hpp:2612
Entity(EntityID entityID, std::shared_ptr< ECSWorld > world)
Construct a new Entity object, with a new ID as a member of a new ECSWorld.
Definition ecs_world.hpp:2322
bool isEnabled() const
Tests whether this entity is enabled for a particular system.
Definition ecs_world.hpp:2607
TComponent getComponent(float simulationProgress=1.f) const
Get the value of the component at a specific time this frame.
Definition ecs_world.hpp:2587
void removeComponent()
Removes a component from an entity.
Definition ecs_world.hpp:2597
A template class for interpolating components between simulation frames for various purposes.
Definition ecs_world.hpp:337
RangeMapperLinear mProgressLimits
a functor that performs the actual interpolation in the default case
Definition ecs_world.hpp:354
A simple linear interpolation implementation between a fixed input and output range.
Definition util.hpp:52
Helper class for registering a class at program startup.
Definition registrator.hpp:64
Holds references to all the systems belonging to this manager's ECSWorld.
Definition ecs_world.hpp:1148
void disableEntity(EntityID entityID)
Disables an entity for a particular system.
Definition ecs_world.hpp:2571
std::unordered_map< std::string, Signature > mNameToListenedForComponents
Mapping from the system type name for a system to its component listening signature.
Definition ecs_world.hpp:1395
std::weak_ptr< ECSWorld > mWorld
The world this System manager belongs to.
Definition ecs_world.hpp:1414
bool isEnabled(EntityID entityID)
Tests whether an entity is enabled for a particular system.
Definition ecs_world.hpp:2622
SystemManager(std::weak_ptr< ECSWorld > world)
Construct a new System Manager object.
Definition ecs_world.hpp:1155
SystemType getSystemType() const
Get the SystemType for a system.
Definition ecs_world.hpp:2516
std::unordered_map< std::string, std::shared_ptr< BaseSystem > > mNameToSystem
Maps system type names to references to the actual in-memory Systems they represent.
Definition ecs_world.hpp:1408
std::shared_ptr< TSystem > getSystem()
Gets an instance of a system belonging to this ECS World.
Definition ecs_world.hpp:2558
void enableEntity(EntityID entityID)
Enables the visibility of an entity to a particular system.
Definition ecs_world.hpp:2565
std::unordered_map< std::string, Signature > mNameToSignature
Mapping from the system type name for a system to its component signature.
Definition ecs_world.hpp:1387
std::unordered_map< std::string, SystemType > mNameToSystemType
Maps system type names to their corresponding SystemType values.
Definition ecs_world.hpp:1402
void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent)
Handles an update to an entity by running a callback on all interested systems (except the one that t...
Definition ecs_world.hpp:2811
void registerSystem(const Signature &signature, const Signature &listenedForComponents)
During static initialization, adds the new System type and its component signatures to its tables.
Definition ecs_world.hpp:2546
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for participation in a particular system.
Definition ecs_world.hpp:2628
void updateComponent(EntityID entityID, const TComponent &component)
Updates a component via this system's specialized version of updateComponent.
Definition ecs_world.hpp:1116
std::shared_ptr< BaseSystem > instantiate(std::weak_ptr< ECSWorld > world) override
Instantiates a fresh System using this one as a template, and associates it with a new world.
System(std::weak_ptr< ECSWorld > world)
Construct a new System object.
Definition ecs_world.hpp:1092
static void registerSelf()
Helper function called during static initialization to make this project's ECS system aware that this...
Definition ecs_world.hpp:2538
TComponent getComponent(EntityID entityID, float progress=1.f)
Gets the component belonging to the entity using this System's specialized version of getComponent.
Definition ecs_world.hpp:1103
static Registrator< System< TSystemDerived, std::tuple< TListenedForComponents... >, std::tuple< TRequiredComponents... > > > & s_registrator
A specialization of Registrator<T> which ensures registerSelf() is called during this project's stati...
Definition ecs_world.hpp:1134
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1062
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:103
T operator()(const T &previousState, const T &nextState, float simulationProgress=1.f) const
Returns an interpolated value for a component between two given states.
Definition ecs_world.hpp:2346
constexpr ComponentType kMaxComponents
A constant that restricts the number of definable components in a project.
Definition ecs_world.hpp:132
std::bitset< kMaxComponents > Signature
A 255 bit number, where each enabled bit represents a relationship between an entity and some ECS rel...
Definition ecs_world.hpp:154
constexpr SystemType kMaxSystems
A constant that restricts the number of definable systems in a project.
Definition ecs_world.hpp:139
ECSType SystemType
An unsigned integer representing the type of a system.
Definition ecs_world.hpp:112
std::uint8_t ECSType
A number tag used to represent components and systems.
Definition ecs_world.hpp:93
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:119
std::pair< WorldID, EntityID > UniversalEntityID
An ID that uniquely identifies an entity.
Definition ecs_world.hpp:85
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
std::uint64_t WorldID
An unsigned integer representing the name of an ECS world.
Definition ecs_world.hpp:76
constexpr ECSType kMaxECSTypes
A constant used to restrict the number of definable system and component types in a project.
Definition ecs_world.hpp:125
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
STL namespace.
Contains the definition for the Registrator<T> utility class, used anywhere that automatic registrati...
A struct that describes how a JSON component description is turned into a component.
Definition ecs_world.hpp:289
static TComponent get(const nlohmann::json &jsonComponent)
Get method that automatically invokes a from_json function, found by nlohmann json.
Definition ecs_world.hpp:296
Helper function for retrieving the component type string defined as part of the component.
Definition ecs_world.hpp:543
std::string operator()()
The method that retrieves the component type string.
Definition ecs_world.hpp:549
Prevents the use of the unspecialized version of SystemRegistrationArgs.
Definition ecs_world.hpp:1529
Contains a couple of classes not tied to any part of the engine in particular, but useful to those pa...