Nameless Engine
Loading...
Searching...
No Matches
World.h
1#pragma once
2
3// Standard.
4#include <memory>
5#include <chrono>
6#include <string>
7#include <variant>
8#include <filesystem>
9#include <unordered_set>
10#include <mutex>
11#include <atomic>
12
13// Custom.
14#include "misc/Globals.h"
15#include "misc/Error.h"
16
17// External.
18#include "GcPtr.h"
19
20namespace ne {
21 class GameManager;
22 class Node;
23
26 CalledEveryFrameNodes() = default;
27
29 CalledEveryFrameNodes& operator=(const CalledEveryFrameNodes&) = delete;
30
32 std::pair<std::recursive_mutex, std::unordered_set<Node*>> mtxFirstTickGroup;
33
35 std::pair<std::recursive_mutex, std::unordered_set<Node*>> mtxSecondTickGroup;
36 };
37
43 class World {
44 // Nodes notify the world about being spawned/despawned.
45 friend class Node;
46
47 public:
48 World() = delete;
49
50 World(const World&) = delete;
51 World& operator=(const World&) = delete;
52
53 World(World&&) = delete;
54 World& operator=(World&&) = delete;
55
57 ~World();
58
73 static std::unique_ptr<World>
75
94 static std::variant<std::unique_ptr<World>, Error> loadNodeTreeAsWorld(
96 const std::filesystem::path& pathToNodeTree,
98
105
114 void destroyWorld();
115
122
128 std::pair<std::recursive_mutex, std::unordered_set<Node*>>* getReceivingInputNodes();
129
135 sgc::GcPtr<Node> getRootNode();
136
143
149 float getWorldTimeInSeconds() const;
150
156 size_t getWorldSize() const;
157
165 bool isNodeSpawned(size_t iNodeId);
166
167 private:
176 World(GameManager* pGameManager, sgc::GcPtr<Node> pRootNode, size_t iWorldSize);
177
183 void onNodeSpawned(Node* pNode);
184
190 void onNodeDespawned(Node* pNode);
191
202
213
221
229
237
245
248
250 std::pair<std::recursive_mutex, bool> mtxIsDestroyed;
251
253 std::pair<std::mutex, sgc::GcPtr<Node>> mtxRootNode;
254
257
259 std::pair<std::recursive_mutex, std::unordered_set<Node*>> mtxReceivingInputNodes;
260
262 std::pair<std::recursive_mutex, std::unordered_map<size_t, Node*>> mtxSpawnedNodes;
263
265 std::atomic<size_t> iTotalSpawnedNodeCount{0};
266
268 const size_t iWorldSize = 0;
269
271 std::chrono::steady_clock::time_point timeWhenWorldCreated;
272 };
273} // namespace ne
Definition: Error.h:27
Definition: GameManager.h:34
static constexpr size_t getDefaultWorldSize()
Definition: Globals.h:33
Definition: Node.h:39
Definition: World.h:43
void destroyWorld()
Definition: World.cpp:123
std::pair< std::recursive_mutex, std::unordered_map< size_t, Node * > > mtxSpawnedNodes
Definition: World.h:262
static std::variant< std::unique_ptr< World >, Error > loadNodeTreeAsWorld(GameManager *pGameManager, const std::filesystem::path &pathToNodeTree, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: World.cpp:77
sgc::GcPtr< Node > getRootNode()
Definition: World.cpp:94
float getWorldTimeInSeconds() const
Definition: World.cpp:106
void addNodeToCalledEveryFrameArrays(Node *pNode)
Definition: World.cpp:352
std::atomic< size_t > iTotalSpawnedNodeCount
Definition: World.h:265
const size_t iWorldSize
Definition: World.h:268
std::chrono::steady_clock::time_point timeWhenWorldCreated
Definition: World.h:271
CalledEveryFrameNodes * getCalledEveryFrameNodes()
Definition: World.cpp:143
~World()
Definition: World.cpp:47
void onNodeSpawned(Node *pNode)
Definition: World.cpp:149
void onNodeDespawned(Node *pNode)
Definition: World.cpp:201
std::pair< std::recursive_mutex, std::unordered_set< Node * > > * getReceivingInputNodes()
Definition: World.cpp:145
size_t getCalledEveryFrameNodeCount()
Definition: World.cpp:99
void onSpawnedNodeChangedIsReceivingInput(Node *pNode)
Definition: World.cpp:300
CalledEveryFrameNodes calledEveryFrameNodes
Definition: World.h:256
void addNodeToReceivingInputArray(Node *pNode)
Definition: World.cpp:346
size_t getTotalSpawnedNodeCount()
Definition: World.cpp:92
std::pair< std::recursive_mutex, bool > mtxIsDestroyed
Definition: World.h:250
void removeNodeFromReceivingInputArray(Node *pNode)
Definition: World.cpp:395
void removeNodeFromCalledEveryFrameArrays(Node *pNode)
Definition: World.cpp:366
GameManager * pGameManager
Definition: World.h:247
bool isNodeSpawned(size_t iNodeId)
Definition: World.cpp:115
void onSpawnedNodeChangedIsCalledEveryFrame(Node *pNode)
Definition: World.cpp:254
std::pair< std::recursive_mutex, std::unordered_set< Node * > > mtxReceivingInputNodes
Definition: World.h:259
std::pair< std::mutex, sgc::GcPtr< Node > > mtxRootNode
Definition: World.h:253
size_t getWorldSize() const
Definition: World.cpp:113
static std::unique_ptr< World > createWorld(GameManager *pGameManager, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: World.cpp:73
Definition: World.h:25
std::pair< std::recursive_mutex, std::unordered_set< Node * > > mtxFirstTickGroup
Definition: World.h:32
std::pair< std::recursive_mutex, std::unordered_set< Node * > > mtxSecondTickGroup
Definition: World.h:35