Toast Engine
Loading...
Searching...
No Matches
world.hpp
1
8
9#pragma once
10#include "box.hpp"
11#include "control_box.hpp"
12#include "node.hpp"
13#include "node_owner.hpp"
14#include "tick_scheduler.hpp"
15#include "world_events.hpp"
16
17#include <compare>
18#include <concepts>
19#include <future>
20#include <iterator>
21#include <mutex>
22#include <queue>
23#include <stack>
24#include <toast/assets/prefab.hpp>
25#include <toast/events/listener.hpp>
26#include <toast/log.hpp>
27#include <toast/reflect/reflect.hpp>
28#include <type_traits>
29#include <unordered_map>
30#include <unordered_set>
31#include <utility>
32#include <variant>
33
34namespace toast {
35
36class Node;
37
38class World final : public INodeOwner {
39public:
40 World();
41 ~World() override;
42
43 auto name() -> std::string override { return "World"; }
44
45 void tick() override;
46
47 [[nodiscard]]
48 auto participatesIn(NodeOwnerParticipation /*use*/) const noexcept -> bool override {
49 return true;
50 }
51
58 void registerDependency(Node& from, Node& to) override;
59 void unregisterDependency(Node& from, Node& to) override;
60 void registerInteraction(Node& first, Node& second) override;
61
66 static void loadNode(UID uid);
67
74 static void loadNode(std::string_view uri);
75
76 auto findFrom(const Node& origin, std::string_view query) -> Box<Node> override;
77 auto findFrom(const Node& origin, const UID& uid) -> Box<Node> override;
78 auto searchFrom(const Node& origin, std::string_view query) -> std::vector<Box<Node>> override;
79
90 static auto setRoot(Node& node) -> Box<Node>;
91
98 static auto cacheNode(Node& node) -> Box<Node>;
99
106 static void destroyNode(Node& node);
107
114 [[nodiscard]]
115 static auto findCached(std::string_view name) -> Box<Node>;
116
122 [[nodiscard]]
123 static auto graphviz() -> std::string;
124
128 static void hotReload();
129
134 static void hotReloadScripts(toast::UID script_uid);
135
136 [[nodiscard]]
137 auto dependencyGraphGraphviz() const -> std::string;
138
139private:
140 inline static World* instance = nullptr;
141
143 void computeDependencyGraph();
144 void applyActiveCamera() override;
145
147 auto swapRoot(Node& node) -> Box<Node>;
148
150 auto moveToCached(Node& node) -> Box<Node>;
151
153 auto moveToGlobal(Node& node) -> Box<Node>;
154
156 auto moveToChild(Node& node, Node& parent) -> Box<Node>;
157
159 void detachFromParent(Node& node);
160
162 void drainLoadQueue();
163
165 void drainDestroyQueue();
166
168 void drainSpawnQueue();
169
171 static void spawn(UID prefab, Node& parent);
172
174 [[nodiscard]]
175 static auto findNode(const UID& uid, Node* scope = nullptr) -> Box<Node>;
176
178 [[nodiscard]]
179 static auto findNode(std::string_view path) -> Box<Node>;
180
182 [[nodiscard]]
183 static auto uidPath(const Node& node) -> std::string;
184
186 [[nodiscard]]
187 static auto findScoped(Node& scope, std::string_view seg) -> Box<Node>;
188
190 static void searchScoped(Node& scope, std::string_view seg, std::vector<Box<Node>>& out);
191
192 struct {
193 event::Listener listener;
194 std::mutex load_mutex;
195 std::vector<std::future<void>> load_futures;
196 std::vector<std::pair<Box<Node>, Box<Node>>> spawn_queue;
197 } m;
198
199 struct Trees {
200 Box<Node> root;
201 std::vector<Box<Node>> global;
202 std::vector<Box<Node>> cached;
203 std::vector<Box<Node>> load_queue;
204 std::vector<Box<Node>> destroy_queue;
205 } trees;
206
207 using DependencyGraph = TickScheduler::DependencyGraph;
208 TickScheduler m_scheduler;
209
210 // for testing only
211 friend struct toast::_detail::WorldTestAccess;
212};
213
214}
pseudo shared pointer for Nodes
manages the lifetime, subscription, and unsubscription of callbacks
Definition listener.hpp:39
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
Definition node.hpp:81
Definition world.hpp:38
static auto setRoot(Node &node) -> Box< Node >
Promotes a cached or global node to be the active world root.
Definition world.cpp:611
auto participatesIn(NodeOwnerParticipation) const noexcept -> bool override
Determines if this owner is elegible for runtime systems.
Definition world.hpp:48
void registerDependency(Node &from, Node &to) override
Records a tick ordering constraint between two active nodes.
Definition world.cpp:123
static auto graphviz() -> std::string
Serializes the current tick schedule as a Graphviz DOT string.
Definition world.cpp:668
static auto cacheNode(Node &node) -> Box< Node >
Moves an active node back to the cached state, disabling it and calling its end() lifecycle.
Definition world.cpp:615
static void loadNode(UID uid)
Begins asynchronous loading of a prefab into the cache.
Definition world.cpp:135
static auto findCached(std::string_view name) -> Box< Node >
Linear search for a cached root node by display name.
Definition world.cpp:619
static void hotReloadScripts(toast::UID script_uid)
Recomputes the tick schedule after the script runtimes using the given script asset were rebuilt.
Definition world.cpp:658
static void destroyNode(Node &node)
Schedules a cached node for destruction at the start of the next tick.
Definition world.cpp:672
static void hotReload()
Refreshes the NodeInfo pointers on every live node after a game library hot reload.
Definition world.cpp:633
Control block for the pseudo shared pointer Box<Node>.
Public logging interface for the engine.
Base object type for the scene graph.
Definition uid.hpp:20
Dependency-graph tick scheduling shared by World and PlayWorkspace.
Events that allow client code to interact with the World class.