Polyphase Game Engine
Loading...
Searching...
No Matches
Node.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5
6#include "PolyphaseAPI.h"
7#include "Object.h"
8#include "Assertion.h"
9#include "EngineTypes.h"
10#include "Property.h"
11#include "Stream.h"
12#include "Factory.h"
13#include "Maths.h"
14#include "NetDatum.h"
15#include "NetFunc.h"
16#include "ScriptUtils.h"
17#include "ScriptAutoReg.h"
18#include "Signals.h"
19#include "Assets/StaticMesh.h"
20
21#include "Bullet/btBulletCollisionCommon.h"
22
23#include <unordered_map>
24#include <unordered_set>
25
26class Node;
27class Scene;
28class World;
29class Script;
30
31#define DECLARE_NODE(Class, Parent) \
32 DECLARE_FACTORY(Class, Node); \
33 DECLARE_OBJECT(Class, Parent); \
34 DECLARE_SCRIPT_LINK(Class, Parent, Node) \
35 typedef Parent Super;
36
37#define DEFINE_NODE(Class, Parent) \
38 DEFINE_FACTORY(Class, Node); \
39 DEFINE_OBJECT(Class); \
40 DEFINE_SCRIPT_LINK(Class, Parent, Node);
41
42typedef std::unordered_map<std::string, NetFunc> NetFuncMap;
43
44// Can also use a lambda for Traverse() and ForEach() functions
45typedef bool(*NodeTraversalFP)(Node*);
46
47// Forward declarations so we can use these in the Node class below
48template<typename T = Node>
50
51template<typename T = Node>
53
54#if 0
55struct NodeNetData
56{
57 std::vector<NetDatum> mReplicatedData;
58 NetId mNetId = INVALID_NET_ID;
59 NetHostId mOwningHost = INVALID_HOST_ID;
60 bool mReplicate = false;
61 bool mReplicateTransform = false;
62 bool mForceReplicate = false;
63};
64#endif
65
67{
68public:
69
74
75 static NodePtr Construct(const std::string& name);
76 static NodePtr Construct(TypeId typeId);
77 static void Destruct(Node* node);
78
79 Node();
80 virtual ~Node();
81
82 virtual void Create();
83 virtual void Destroy();
84 void DestroyDeferred();
85 void Doom(); // Alias for DestroyDeferred
86
87 virtual void SaveStream(Stream& stream, Platform platform);
88 virtual void LoadStream(Stream& stream, Platform platform, uint32_t version);
89
90 virtual void Copy(Node* srcNode, bool recurse);
91 virtual void Render(PipelineConfig pipelineConfig);
92
93 virtual void Awake();
94 virtual void Start();
95 virtual void Stop();
96 virtual void PrepareTick(std::vector<NodePtrWeak>& outTickNodes, bool game, bool recurse);
97 virtual void Tick(float deltaTime);
98 virtual void EditorTick(float deltaTime);
99 uint32_t GetLastTickedFrame() const;
100 virtual void Render();
101 virtual void OnInstanced();
102
103 virtual VertexType GetVertexType() const;
104
105 virtual void GatherProperties(std::vector<Property>& outProps) override;
106
107 virtual void GatherReplicatedData(std::vector<NetDatum>& outData);
108 virtual void GatherNetFuncs(std::vector<NetFunc>& outFuncs);
109
110 void GatherPropertyOverrides(std::vector<Property>& outOverrides);
111 void ApplyPropertyOverrides(const std::vector<Property>& overs);
112
113 virtual void BeginOverlap(Primitive3D* thisComp, Primitive3D* otherComp);
114 virtual void EndOverlap(Primitive3D* thisComp, Primitive3D* otherComp);
115 virtual void OnCollision(
116 Primitive3D* thisComp,
117 Primitive3D* otherComp,
118 glm::vec3 impactPoint,
119 glm::vec3 impactNormal,
120 btPersistentManifold* manifold);
121
122 NodeId GetNodeId() const;
123
124 uint64_t GetPersistentUuid() const;
125 void SetPersistentUuid(uint64_t uuid);
126 void EnsurePersistentUuid();
127
128 void EmitSignal(const std::string& name, const std::vector<Datum>& args);
129 void ConnectSignal(const std::string& name, Node* listener, SignalHandlerFP func);
130 void ConnectSignal(const std::string& name, Node* listener, const ScriptFunc& func);
131 void DisconnectSignal(const std::string& name, Node* listener);
132
133 void RenderShadow();
134 void RenderSelected(bool renderChildren);
135
136 Node* CreateChild(TypeId nodeType);
137 Node* CreateChild(const char* typeName);
138 Node* CreateChildClone(Node* srcNode, bool recurse);
139 NodePtr Clone(bool recurse, bool instantiateLinkedScene = true, bool resolveNodePaths = false);
140 void DestroyAllChildren();
141
142 Node* GetRoot();
143 bool IsWorldRoot() const;
144 Node* GetSubRoot();
145
146 template<class NodeClass>
147 NodeClass* CreateChild()
148 {
149 return (NodeClass*)CreateChild(NodeClass::GetStaticType());
150 }
151
152 template<class NodeClass>
154 {
155 return ResolvePtr<NodeClass>(CreateChild(NodeClass::GetStaticType()));
156 }
157
158 template<class NodeClass>
159 NodeClass* CreateChild(const char* name)
160 {
161 NodeClass* ret = (NodeClass*)CreateChild(NodeClass::GetStaticType());
162 ret->SetName(name);
163 return ret;
164 }
165
166 template<class NodeClass>
168 {
169 NodeClass* ret = (NodeClass*)CreateChild(NodeClass::GetStaticType());
170 ret->SetName(name);
171 return ResolvePtr<NodeClass>(ret);
172 }
173
174 bool IsDestroyed() const;
175 bool IsPendingDestroy() const;
176 bool IsDoomed() const;
177
178 bool HasStarted() const;
179 bool HasAwoken() const;
180
181 void EnableTick(bool enable);
182 bool IsTickEnabled() const;
183
184 virtual void SetWorld(World* world, bool subRoot);
185 World* GetWorld();
186
187 void SetScene(Scene* scene);
188 Scene* GetScene();
189
190 uint8_t GetTargetScreen() const { return mTargetScreen; }
191 void SetTargetScreen(uint8_t screen) { mTargetScreen = screen; }
192
193 std::vector<NetDatum>& GetReplicatedData();
194
195 void SetNetId(NetId id);
196 NetId GetNetId() const;
197
198 NetHostId GetOwningHost() const;
199 void SetOwningHost(NetHostId hostId, bool setAsPawn = false);
200
201 void SetReplicate(bool replicate);
202 bool IsReplicated() const;
203 void SetReplicateTransform(bool repTransform);
204 bool IsTransformReplicated() const;
205
206 void ForceReplication();
207 void ClearForcedReplication();
208 bool NeedsForcedReplication();
209
210 virtual bool CheckNetRelevance(Node* playerNode);
211 bool IsAlwaysRelevant() const;
212 void SetAlwaysRelevant(bool alwaysRelevant);
213
214 bool HasTag(const std::string& tag);
215 void AddTag(const std::string& tag);
216 void RemoveTag(const std::string& tag);
217
218 void SetName(const std::string& newName);
219 const std::string& GetName() const;
220 virtual void SetActive(bool active);
221 bool IsActive(bool recurse = false) const;
222 virtual void SetVisible(bool visible);
223 bool IsVisible(bool recurse = false) const;
224 void SetTransient(bool transient);
225 virtual bool IsTransient() const;
226 void SetPersistent(bool persistent);
227 bool IsPersistent() const;
228
229 void SetDefault(bool isDefault);
230 bool IsDefault() const;
231
232 void SetUserdataCreated(bool created);
233 bool IsUserdataCreated() const;
234
235 virtual const char* GetTypeName() const;
236 virtual DrawData GetDrawData();
237
238 virtual bool IsNode3D() const;
239 virtual bool IsWidget() const;
240 virtual bool IsPrimitive3D() const;
241 virtual bool IsLight3D() const;
242
243 Node* GetParent();
244 const Node* GetParent() const;
245 const std::vector<NodePtr>& GetChildren() const;
246
247 virtual void Attach(Node* parent, bool keepWorldTransform = false, int32_t index = -1);
248 void Detach(bool keepWorldTransform = false);
249 void AddChild(Node* child, int32_t index = -1);
250 void RemoveChild(Node* child);
251 void RemoveChild(int32_t index);
252
253 template<typename T>
254 void AddChild(const SharedPtr<T>& child, int32_t index = -1)
255 {
256 AddChild(child.Get(), index);
257 }
258
259 template<typename T>
260 void RemoveChild(const SharedPtr<Node>& child)
261 {
262 RemoveChild(child.Get());
263 }
264
265 int32_t FindChildIndex(const std::string& name) const;
266 int32_t FindChildIndex(Node* child) const;
267 Node* FindChild(const std::string& name, bool recurse) const;
268 Node* FindChildWithTag(const std::string& name, bool recurse) const;
269 Node* FindDescendant(const std::string& name);
270 Node* FindAncestor(const std::string& name);
271 bool HasAncestor(Node* node);
272 Node* GetChild(int32_t index) const;
273 Node* GetChildByType(TypeId type) const;
274 uint32_t GetNumChildren() const;
275 int32_t FindParentNodeIndex() const;
276
277 void SetHitCheckId(uint32_t id);
278 uint32_t GetHitCheckId() const;
279
280 bool IsLateTickEnabled() const;
281 void EnableLateTick(bool enable);
282
283 Script* GetScript();
284 void SetScriptFile(const std::string& fileName);
285
286 bool DoChildrenHaveUniqueNames() const;
287 void BreakSceneLink();
288 bool IsSceneLinked(bool ignoreInPie = true) const;
289 bool IsSceneLinkedChild(bool ignoreInPie = true);
290 bool IsForeign() const;
291
292 bool HasAuthority() const;
293 bool IsOwned() const;
294 bool IsLocallyControlled() const;
295
296 Datum GetField(const std::string& key);
297 void SetField(const std::string& name, const Datum& value);
298 Datum GetField(int32_t key);
299 void SetField(int32_t key, const Datum& value);
300 Datum CallFunction(const std::string& name, const std::vector<Datum>& args = {});
301
302 NetFunc* FindNetFunc(const char* name);
303 NetFunc* FindNetFunc(uint16_t index);
304
305 void InvokeNetFunc(const char* name);
306 void InvokeNetFunc(const char* name, Datum param0);
307 void InvokeNetFunc(const char* name, Datum param0, Datum param1);
308 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2);
309 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2, Datum param3);
310 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2, Datum param3, Datum param4);
311 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2, Datum param3, Datum param4, Datum param5);
312 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2, Datum param3, Datum param4, Datum param5, Datum param6);
313 void InvokeNetFunc(const char* name, Datum param0, Datum param1, Datum param2, Datum param3, Datum param4, Datum param5, Datum param6, Datum param7);
314 void InvokeNetFunc(const char* name, const std::vector<Datum>& params);
315
316 static void ProcessPendingDestroys();
317
318 static void RegisterNetFuncs(Node* node);
319
320 static void Deleter(Node* node);
321
322 const WeakPtr<Node>& GetSelfPtr() const { return mSelf; }
323
324 template<typename T>
325 T* FindChild(const std::string& name, bool recurse)
326 {
327 T* ret = nullptr;
328 Node* child = FindChild(name, recurse);
329 if (child != nullptr)
330 {
331 ret = child->As<T>();
332 }
333
334 return ret;
335 }
336
337 template<typename T>
338 void Traverse(T func, bool inverted = false)
339 {
340 bool traverseChildren = true;
341
342 if (!inverted)
343 {
344 traverseChildren = func(this);
345 }
346
347 if (traverseChildren)
348 {
349 if (inverted)
350 {
351 for (int32_t i = int32_t(mChildren.size()) - 1; i >= 0; --i)
352 {
353 mChildren[i]->Traverse(func, inverted);
354 }
355 }
356 else
357 {
358 for (uint32_t i = 0; i < mChildren.size(); ++i)
359 {
360 mChildren[i]->Traverse(func, inverted);
361 }
362 }
363 }
364
365 if (inverted)
366 {
367 func(this);
368 }
369 }
370
371 template<typename T>
372 bool ForEach(T func, bool inverted = false)
373 {
374 bool cont = true;
375
376 if (!inverted)
377 {
378 cont = func(this);
379 }
380
381 if (cont)
382 {
383 if (inverted)
384 {
385 for (int32_t i = int32_t(mChildren.size()) - 1; i >= 0; --i)
386 {
387 cont = mChildren[i]->ForEach(func, inverted);
388
389 if (!cont)
390 {
391 break;
392 }
393 }
394 }
395 else
396 {
397 for (uint32_t i = 0; i < mChildren.size(); ++i)
398 {
399 cont = mChildren[i]->ForEach(func, inverted);
400
401 if (!cont)
402 {
403 break;
404 }
405 }
406 }
407 }
408
409 if (cont && inverted)
410 {
411 cont = func(this);
412 }
413
414 return cont;
415 }
416
417 template<class NodeClass>
419 {
420 // Code copied across other Construct() functions
421 NodeClass* newNode = (NodeClass*)Node::CreateInstance(NodeClass::GetStaticType());
422 SharedPtr<NodeClass> newNodePtr;
423
424 if (newNode != nullptr)
425 {
426 newNodePtr.Set(newNode, nullptr);
427 newNodePtr.SetDeleter([](NodeClass* node) { Node::Deleter(node); });
428 newNodePtr->mSelf = PtrStaticCast<Node>(newNodePtr);
429 newNodePtr->Create();
430 }
431
432 return newNodePtr;
433 }
434
435protected:
436
437 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
438
439 static bool OnRep_OwningHost(Datum* datum, uint32_t index, const void* newValue);
440
441 void TickCommon(float deltaTime);
442
443 virtual void SetParent(Node* parent);
444 void ValidateUniqueChildName(Node* newChild);
445
446 void SendNetFunc(NetFunc* func, uint32_t numParams, const Datum** params);
447
448 static std::unordered_map<TypeId, NetFuncMap> sTypeNetFuncMap;
449 static std::unordered_set<NodePtrWeak> sPendingDestroySet;
451
452 std::string mName;
453
454 World* mWorld = nullptr;
457 std::vector<NodePtr> mChildren;
458 std::unordered_map<std::string, Node*> mChildNameMap;
459 std::unordered_map<std::string, Signal> mSignalMap;
460 std::string mScriptFile;
461 uint32_t mLastTickedFrame = 0;
462 bool mActive = true;
463 bool mVisible = true;
464 bool mTransient = false;
465 bool mPersistent = false;
466 bool mDefault = false;
467 bool mUserdataCreated = false;
468 bool mHasStarted = false;
469 bool mHasAwoken = false;
470 bool mDestroyed = false;
471 bool mTickEnabled = true;
472 bool mLateTick = false;
473
474 // Merged from Actor
476 uint8_t mTargetScreen = 0; // 0 = Top Screen, 1 = Bottom Screen
477 std::vector<std::string> mTags;
479 uint64_t mPersistentUuid = 0;
480
481 // Network Data
482 // This is only about 44 bytes, so right now, we will keep this data as direct members of Node.
483 // But if we need to save data, then consider allocating a NodeNetData struct only if replicated.
484 // Leaving these as direct members will improve cache performance too.
485 std::vector<NetDatum> mReplicatedData;
488 bool mReplicate = false;
489 bool mReplicateTransform = false;
490 bool mForceReplicate = false;
491 bool mAlwaysRelevant = true;
492
493 Script* mScript = nullptr;
494 //NodeNetData* mNetData = nullptr;
495
496#if EDITOR
497public:
498 // TODO-NODE: Either remove mExposeVariable, or make sure it works with Scenes.
499 // Could consider moving this up to Node if it would be useful from avoiding FindChild() calls in Start().
500 bool ShouldExposeVariable() const;
501 void SetExposeVariable(bool expose);
502 bool AreAllChildrenHiddenInTree() const;
503
504 uint32_t mHitCheckId = 0;
505 bool mExposeVariable = false;
506 bool mHiddenInTree = false;
507#endif
508};
509
510// Eventually, if we move the mSelf pointer into Object, we will have to
511// move these functions also (and make them take Object* param)
512template<typename T>
514{
515 NodePtr nodePtr = node ? node->GetSelfPtr().Lock() : nullptr;
516 return PtrStaticCast<T>(nodePtr);
517}
518
519template<typename T>
521{
522 return PtrStaticCast<T>(node ? node->GetSelfPtr() : nullptr);
523}
#define INVALID_HOST_ID
Definition Constants.h:44
#define INVALID_NODE_ID
Definition Constants.h:42
#define INVALID_NET_ID
Definition Constants.h:41
uint32_t NetId
Definition EngineTypes.h:65
Platform
Definition EngineTypes.h:31
uint8_t NetHostId
Definition EngineTypes.h:486
uint32_t TypeId
Definition EngineTypes.h:64
uint32_t NodeId
Definition EngineTypes.h:66
World * GetWorld(int32_t index)
Definition Engine.cpp:994
PipelineConfig
Definition GraphicsTypes.h:66
std::unordered_map< std::string, NetFunc > NetFuncMap
Definition Node.h:42
bool(* NodeTraversalFP)(Node *)
Definition Node.h:45
SharedPtr< T > ResolvePtr(Node *node)
Definition Node.h:513
WeakPtr< T > ResolveWeakPtr(Node *node)
Definition Node.h:520
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
void(* SignalHandlerFP)(Node *, const std::vector< Datum > &)
Definition Signals.h:11
VertexType
Definition Vertex.h:7
Definition AssetRef.h:18
Definition Datum.h:164
Definition Node.h:67
std::vector< NetDatum > mReplicatedData
Definition Node.h:485
SceneRef mScene
Definition Node.h:475
DECLARE_FACTORY(Node, Node)
SharedPtr< NodeClass > CreateChildSp(const char *name)
Definition Node.h:167
std::string mScriptFile
Definition Node.h:460
void Traverse(T func, bool inverted=false)
Definition Node.h:338
NodePtrWeak mParent
Definition Node.h:455
NodeClass * CreateChild(const char *name)
Definition Node.h:159
std::vector< NodePtr > mChildren
Definition Node.h:457
std::string mName
Definition Node.h:452
static std::unordered_map< TypeId, NetFuncMap > sTypeNetFuncMap
Definition Node.h:448
static SharedPtr< NodeClass > Construct()
Definition Node.h:418
NodePtrWeak mSelf
Definition Node.h:456
void SetTargetScreen(uint8_t screen)
Definition Node.h:191
DECLARE_FACTORY_MANAGER(Node)
static std::unordered_set< NodePtrWeak > sPendingDestroySet
Definition Node.h:449
DECLARE_OBJECT(Node, Object)
static NodeId sNextNodeId
Definition Node.h:450
SharedPtr< NodeClass > CreateChildSp()
Definition Node.h:153
bool ForEach(T func, bool inverted=false)
Definition Node.h:372
static void Deleter(Node *node)
Definition Node.cpp:131
std::unordered_map< std::string, Node * > mChildNameMap
Definition Node.h:458
void AddChild(const SharedPtr< T > &child, int32_t index=-1)
Definition Node.h:254
std::unordered_map< std::string, Signal > mSignalMap
Definition Node.h:459
DECLARE_SCRIPT_LINK_BASE(Node)
uint8_t GetTargetScreen() const
Definition Node.h:190
T * FindChild(const std::string &name, bool recurse)
Definition Node.h:325
NodeClass * CreateChild()
Definition Node.h:147
const WeakPtr< Node > & GetSelfPtr() const
Definition Node.h:322
std::vector< std::string > mTags
Definition Node.h:477
void RemoveChild(const SharedPtr< Node > &child)
Definition Node.h:260
Definition Object.h:13
virtual void GatherProperties(std::vector< Property > &props)
Definition Object.h:41
T * As() const
Definition Object.h:52
Definition Primitive3d.h:46
Definition Scene.h:36
Definition ScriptFunc.h:10
Definition Script.h:29
Definition SmartPointer.h:33
void SetDeleter(typename RefCount< T >::DeleterFP deleteFunc)
Definition SmartPointer.h:234
void Set(T *pointer, RefCount< T > *refCount)
Definition SmartPointer.h:156
T * Get() const
Definition SmartPointer.h:247
Definition Stream.h:21
Definition SmartPointer.h:312
SharedPtr< T > Lock() const
Definition SmartPointer.h:492
Definition World.h:24
Definition EngineTypes.h:199
Definition NetFunc.h:33