Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
World.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_WORLD_HPP
8 #define NDK_WORLD_HPP
9 
10 #include <Nazara/Core/Bitset.hpp>
11 #include <Nazara/Core/HandledObject.hpp>
12 #include <NDK/Entity.hpp>
13 #include <NDK/EntityList.hpp>
14 #include <NDK/System.hpp>
15 #include <algorithm>
16 #include <memory>
17 #include <vector>
18 
19 namespace Ndk
20 {
21  class World;
22 
23  using WorldHandle = Nz::ObjectHandle<World>;
24 
25  class NDK_API World : public Nz::HandledObject<World>
26  {
27  friend BaseSystem;
28  friend Entity;
29 
30  public:
31  using EntityVector = std::vector<EntityHandle>;
32 
33  inline World(bool addDefaultSystems = true);
34  World(const World&) = delete;
35  inline World(World&& world) noexcept;
36  ~World() noexcept;
37 
38  void AddDefaultSystems();
39 
40  inline BaseSystem& AddSystem(std::unique_ptr<BaseSystem>&& system);
41  template<typename SystemType, typename... Args> SystemType& AddSystem(Args&&... args);
42 
43  const EntityHandle& CreateEntity();
44  inline EntityVector CreateEntities(unsigned int count);
45 
46  void Clear() noexcept;
47  const EntityHandle& CloneEntity(EntityId id);
48 
49  inline const EntityHandle& GetEntity(EntityId id);
50  inline const EntityList& GetEntities() const;
51  inline BaseSystem& GetSystem(SystemIndex index);
52  template<typename SystemType> SystemType& GetSystem();
53 
54  inline bool HasSystem(SystemIndex index) const;
55  template<typename SystemType> bool HasSystem() const;
56 
57  inline void KillEntity(Entity* entity);
58  inline void KillEntities(const EntityVector& list);
59 
60  inline bool IsEntityValid(const Entity* entity) const;
61  inline bool IsEntityIdValid(EntityId id) const;
62 
63  inline void RemoveAllSystems();
64  inline void RemoveSystem(SystemIndex index);
65  template<typename SystemType> void RemoveSystem();
66 
67  void Update();
68  inline void Update(float elapsedTime);
69 
70  World& operator=(const World&) = delete;
71  inline World& operator=(World&& world) noexcept;
72 
73  private:
74  inline void Invalidate();
75  inline void Invalidate(EntityId id);
76  inline void InvalidateSystemOrder();
77  void ReorderSystems();
78 
79  struct EntityBlock
80  {
81  EntityBlock(Entity&& e) :
82  entity(std::move(e)),
83  handle(&entity)
84  {
85  }
86 
87  EntityBlock(EntityBlock&& block) = default;
88 
89  Entity entity;
90  EntityHandle handle;
91  };
92 
93  std::vector<std::unique_ptr<BaseSystem>> m_systems;
94  std::vector<BaseSystem*> m_orderedSystems;
95  std::vector<EntityBlock> m_entities;
96  std::vector<EntityBlock*> m_entityBlocks;
97  std::vector<std::unique_ptr<EntityBlock>> m_waitingEntities;
98  std::vector<EntityId> m_freeIdList;
99  EntityList m_aliveEntities;
100  Nz::Bitset<Nz::UInt64> m_dirtyEntities;
101  Nz::Bitset<Nz::UInt64> m_killedEntities;
102  bool m_orderedSystemsUpdated;
103  };
104 }
105 
106 #include <NDK/World.inl>
107 
108 #endif // NDK_WORLD_HPP
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
NDK class that represents an entity in a world.
Definition: Entity.hpp:28
NDK class that represents the common base of all systems.
Definition: BaseSystem.hpp:17
NDK class that represents a set of entities to help performing batch operations.
Definition: EntityList.hpp:16
NDK class that represents a world.
Definition: World.hpp:25