Node.pkg 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. $#include "AnimationController.h"
  2. $#include "AnimatedModel.h"
  3. $#include "Camera.h"
  4. $#include "CollisionShape.h"
  5. $#include "Constraint.h"
  6. $#include "DebugRenderer.h"
  7. $#include "Drawable.h"
  8. $#include "Light.h"
  9. $#include "Navigable.h"
  10. $#include "NavigationMesh.h"
  11. $#include "NetworkPriority.h"
  12. $#include "Node.h"
  13. $#include "Octree.h"
  14. $#include "OffMeshConnection.h"
  15. $#include "PhysicsWorld.h"
  16. $#include "RigidBody.h"
  17. $#include "SmoothedTransform.h"
  18. $#include "SoundListener.h"
  19. $#include "SoundSource.h"
  20. $#include "StaticModel.h"
  21. $#include "Terrain.h"
  22. $#include "Zone.h"
  23. /// Component and child node creation mode for networking.
  24. enum CreateMode
  25. {
  26. REPLICATED = 0,
  27. LOCAL = 1
  28. };
  29. /// %Scene node that may contain components and child nodes.
  30. class Node
  31. {
  32. public:
  33. /// Construct.
  34. Node(Context* context);
  35. /// Destruct. Any child nodes are detached.
  36. virtual ~Node();
  37. /// Set name.
  38. void SetName(const String& name);
  39. /// Set position relative to parent node.
  40. void SetPosition(const Vector3& position);
  41. /// Set rotation relative to parent node.
  42. void SetRotation(const Quaternion& rotation);
  43. /// Set direction relative to parent node. Positive Z equals identity.
  44. void SetDirection(const Vector3& direction);
  45. /// Set uniform scale relative to parent node.
  46. void SetScale(float scale);
  47. /// Set scale relative to parent node.
  48. void SetScale(const Vector3& scale);
  49. /// Set transform relative to parent node.
  50. void SetTransform(const Vector3& position, const Quaternion& rotation);
  51. /// Set transform relative to parent node.
  52. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  53. /// Set transform relative to parent node.
  54. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  55. /// Set position relative to world space.
  56. void SetWorldPosition(const Vector3& position);
  57. /// Set rotation relative to world space.
  58. void SetWorldRotation(const Quaternion& rotation);
  59. /// Set direction relative to world space.
  60. void SetWorldDirection(const Vector3& direction);
  61. /// Set uniform scale relative to world space.
  62. void SetWorldScale(float scale);
  63. /// Set scale relative to world space.
  64. void SetWorldScale(const Vector3& scale);
  65. /// Set transform relative to world space.
  66. void SetWorldTransform(const Vector3& position, const Quaternion& rotation);
  67. /// Set transform relative to world space.
  68. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale);
  69. /// Set transform relative to world space.
  70. void SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  71. /// Move the scene node.
  72. void Translate(const Vector3& delta);
  73. /// Move the scene node relative to its rotation.
  74. void TranslateRelative(const Vector3& delta);
  75. /// Rotate the scene node.
  76. void Rotate(const Quaternion& delta, bool fixedAxis = false);
  77. /// Rotate around the X axis.
  78. void Pitch(float angle, bool fixedAxis = false);
  79. /// Rotate around the Y axis.
  80. void Yaw(float angle, bool fixedAxis = false);
  81. /// Rotate around the Z axis.
  82. void Roll(float angle, bool fixedAxis = false);
  83. /// Look at a target world position.
  84. void LookAt(const Vector3& target, const Vector3& upAxis = Vector3::UP);
  85. /// Modify scale uniformly.
  86. void Scale(float scale);
  87. /// Modify scale.
  88. void Scale(const Vector3& scale);
  89. /// Set enabled/disabled state without recursion. Components in a disabled node become effectively disabled regardless of their own enable/disable state.
  90. void SetEnabled(bool enable);
  91. /// Set enabled/disabled state with optional recursion.
  92. void SetEnabled(bool enable, bool recursive);
  93. /// Set owner connection for networking.
  94. void SetOwner(Connection* owner);
  95. /// Mark node and child nodes to need world transform recalculation. Notify listener components.
  96. void MarkDirty();
  97. /// Create a child scene node (with specified ID if provided).
  98. Node* CreateChild(const String& name = String::EMPTY, CreateMode mode = REPLICATED, unsigned id = 0);
  99. /// Add a child scene node.
  100. void AddChild(Node* node);
  101. /// Remove a child scene node.
  102. void RemoveChild(Node* node);
  103. /// Remove all child scene nodes.
  104. void RemoveAllChildren();
  105. /// Create a component to this node (with specified ID if provided).
  106. Component* CreateComponent(ShortStringHash type, CreateMode mode = REPLICATED, unsigned id = 0);
  107. /// Create a component to this node if it does not exist already.
  108. Component* GetOrCreateComponent(ShortStringHash type, CreateMode mode = REPLICATED, unsigned id = 0);
  109. /// Remove a component from this node.
  110. void RemoveComponent(Component* component);
  111. /// Remove the first component of specific type from this node.
  112. void RemoveComponent(ShortStringHash type);
  113. /// Remove all components from this node.
  114. void RemoveAllComponents();
  115. /// Clone scene node, components and child nodes. Return the clone.
  116. Node* Clone(CreateMode mode = REPLICATED);
  117. /// Remove from the parent node. If no other shared pointer references exist, causes immediate deletion.
  118. void Remove();
  119. /// Set parent scene node. Retains the world transform.
  120. void SetParent(Node* parent);
  121. /// Set a user variable.
  122. void SetVar(ShortStringHash key, const Variant& value);
  123. /// Add listener component that is notified of node being dirtied. Can either be in the same node or another.
  124. void AddListener(Component* component);
  125. /// Remove listener component.
  126. void RemoveListener(Component* component);
  127. /// Template version of creating a component.
  128. // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  129. AnimationController* CreateComponent<AnimationController> @ CreateAnimationController();
  130. AnimatedModel* CreateComponent<AnimatedModel> @ CreateAnimatedModel();
  131. Camera* CreateComponent<Camera> @ CreateCamera();
  132. CollisionShape* CreateComponent<CollisionShape> @ CreateCollisionShape();
  133. Constraint* CreateComponent<Constraint> @ CreateConstraint();
  134. DebugRenderer* CreateComponent<DebugRenderer> @ CreateDebugRenderer();
  135. Drawable* CreateComponent<Drawable> @ CreateDrawable();
  136. Light* CreateComponent<Light> @ CreateLight();
  137. Navigable* CreateComponent<Navigable> @ CreateNavigable();
  138. NavigationMesh* CreateComponent<NavigationMesh> @ CreateNavigationMesh();
  139. NetworkPriority* CreateComponent<NetworkPriority> @ CreateNetworkPriority();
  140. Octree* CreateComponent<Octree> @ CreateOctree();
  141. OffMeshConnection* CreateComponent<OffMeshConnection> @ CreateOffMeshConnection();
  142. PhysicsWorld* CreateComponent<PhysicsWorld> @ CreatePhysicsWorld();
  143. RigidBody* CreateComponent<RigidBody> @ CreateRigidBody();
  144. SmoothedTransform* CreateComponent<SmoothedTransform> @ CreateSmoothedTransform();
  145. SoundListener* CreateComponent<SoundListener> @ CreateSoundListener();
  146. SoundSource* CreateComponent<SoundSource> @ CreateSoundSource();
  147. StaticModel* CreateComponent<StaticModel> @ CreateStaticModel();
  148. Terrain* CreateComponent<Terrain> @ CreateTerrain();
  149. Zone* CreateComponent<Zone> @ CreateZone();
  150. /// Template version of getting or creating a component.
  151. // template <class T> T* GetOrCreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
  152. AnimationController* GetOrCreateComponent<AnimationController> @ CreateAnimationController();
  153. AnimatedModel* GetOrCreateComponent<AnimatedModel> @ CreateAnimatedModel();
  154. Camera* GetOrCreateComponent<Camera> @ CreateCamera();
  155. CollisionShape* GetOrCreateComponent<CollisionShape> @ CreateCollisionShape();
  156. Constraint* GetOrCreateComponent<Constraint> @ CreateConstraint();
  157. DebugRenderer* GetOrCreateComponent<DebugRenderer> @ CreateDebugRenderer();
  158. Drawable* GetOrCreateComponent<Drawable> @ CreateDrawable();
  159. Light* GetOrCreateComponent<Light> @ CreateLight();
  160. Navigable* GetOrCreateComponent<Navigable> @ CreateNavigable();
  161. NavigationMesh* GetOrCreateComponent<NavigationMesh> @ CreateNavigationMesh();
  162. NetworkPriority* GetOrCreateComponent<NetworkPriority> @ CreateNetworkPriority();
  163. Octree* GetOrCreateComponent<Octree> @ CreateOctree();
  164. OffMeshConnection* GetOrCreateComponent<OffMeshConnection> @ CreateOffMeshConnection();
  165. PhysicsWorld* GetOrCreateComponent<PhysicsWorld> @ CreatePhysicsWorld();
  166. RigidBody* GetOrCreateComponent<RigidBody> @ CreateRigidBody();
  167. SmoothedTransform* GetOrCreateComponent<SmoothedTransform> @ CreateSmoothedTransform();
  168. SoundListener* GetOrCreateComponent<SoundListener> @ CreateSoundListener();
  169. SoundSource* GetOrCreateComponent<SoundSource> @ CreateSoundSource();
  170. StaticModel* GetOrCreateComponent<StaticModel> @ CreateStaticModel();
  171. Terrain* GetOrCreateComponent<Terrain> @ CreateTerrain();
  172. Zone* GetOrCreateComponent<Zone> @ CreateZone();
  173. /// Return ID.
  174. unsigned GetID() const { return id_; }
  175. /// Return name.
  176. const String& GetName() const { return name_; }
  177. /// Return name hash.
  178. StringHash GetNameHash() const { return nameHash_; }
  179. /// Return parent scene node.
  180. Node* GetParent() const { return parent_; }
  181. /// Return scene.
  182. Scene* GetScene() const { return scene_; }
  183. /// Return whether is enabled. Disables nodes effectively disable all their components.
  184. bool IsEnabled() const { return enabled_; }
  185. /// Return owner connection in networking.
  186. Connection* GetOwner() const { return owner_; }
  187. /// Return position relative to parent node.
  188. const Vector3& GetPosition() const { return position_; }
  189. /// Return rotation relative to parent node.
  190. const Quaternion& GetRotation() const { return rotation_; }
  191. /// Return direction relative to parent node. Identity rotation equals positive Z.
  192. Vector3 GetDirection() const { return rotation_ * Vector3::FORWARD; }
  193. /// Return scale relative to parent node.
  194. const Vector3& GetScale() const { return scale_; }
  195. /// Return transform matrix relative to parent node.
  196. Matrix3x4 GetTransform() const { return Matrix3x4(position_, rotation_, scale_); }
  197. /// Return position in world space.
  198. Vector3 GetWorldPosition() const
  199. {
  200. if (dirty_)
  201. UpdateWorldTransform();
  202. return worldTransform_.Translation();
  203. }
  204. /// Return rotation in world space.
  205. Quaternion GetWorldRotation() const
  206. {
  207. if (dirty_)
  208. UpdateWorldTransform();
  209. return worldRotation_;
  210. }
  211. /// Return direction in world space.
  212. Vector3 GetWorldDirection() const
  213. {
  214. if (dirty_)
  215. UpdateWorldTransform();
  216. return worldRotation_ * Vector3::FORWARD;
  217. }
  218. /// Return scale in world space.
  219. Vector3 GetWorldScale() const
  220. {
  221. if (dirty_)
  222. UpdateWorldTransform();
  223. return worldTransform_.Scale();
  224. }
  225. /// Return transform matrix in world space.
  226. const Matrix3x4& GetWorldTransform() const
  227. {
  228. if (dirty_)
  229. UpdateWorldTransform();
  230. return worldTransform_;
  231. }
  232. /// Convert a local space position to world space.
  233. Vector3 LocalToWorld(const Vector3& position) const;
  234. /// Convert a local space position or rotation to world space.
  235. Vector3 LocalToWorld(const Vector4& vector) const;
  236. /// Convert a world space position to local space.
  237. Vector3 WorldToLocal(const Vector3& position) const;
  238. /// Convert a world space position or rotation to local space.
  239. Vector3 WorldToLocal(const Vector4& vector) const;
  240. /// Return whether transform has changed and world transform needs recalculation.
  241. bool IsDirty() const { return dirty_; }
  242. /// Return number of child scene nodes.
  243. unsigned GetNumChildren(bool recursive = false) const;
  244. /// Return child scene node by index.
  245. Node* GetChild(unsigned index) const;
  246. /// Return child scene node by name.
  247. Node* GetChild(const String& name, bool recursive = false) const;
  248. /// Return child scene node by name.
  249. Node* GetChild(const char* name, bool recursive = false) const;
  250. /// Return child scene node by name hash.
  251. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  252. /// Return number of components.
  253. unsigned GetNumComponents() const { return components_.Size(); }
  254. /// Return number of non-local components.
  255. unsigned GetNumNetworkComponents() const;
  256. /// Return component by type. If there are several, returns the first.
  257. // Component* GetComponent(ShortStringHash type) const;
  258. /// Return whether has a specific component.
  259. // bool HasComponent(ShortStringHash type) const;
  260. /// Template version of returning a component by type.
  261. // template <class T> T* GetComponent() const;
  262. AnimationController* GetComponent<AnimationController> @ GetAnimationController() const;
  263. AnimatedModel* GetComponent<AnimatedModel> @ GetAnimatedModel() const;
  264. Camera* GetComponent<Camera> @ GetCamera() const;
  265. CollisionShape* GetComponent<CollisionShape> @ GetCollisionShape() const;
  266. Constraint* GetComponent<Constraint> @ GetConstraint() const;
  267. DebugRenderer* GetComponent<DebugRenderer> @ GetDebugRenderer() const;
  268. Drawable* GetComponent<Drawable> @ GetDrawable() const;
  269. Light* GetComponent<Light> @ GetLight() const;
  270. Navigable* GetComponent<Navigable> @ GetNavigable() const;
  271. NavigationMesh* GetComponent<NavigationMesh> @ GetNavigationMesh() const;
  272. NetworkPriority* GetComponent<NetworkPriority> @ GetNetworkPriority() const;
  273. Octree* GetComponent<Octree> @ GetOctree() const;
  274. OffMeshConnection* GetComponent<OffMeshConnection> @ GetOffMeshConnection() const;
  275. PhysicsWorld* GetComponent<PhysicsWorld> @ GetPhysicsWorld() const;
  276. RigidBody* GetComponent<RigidBody> @ GetRigidBody() const;
  277. SmoothedTransform* GetComponent<SmoothedTransform> @ GetSmoothedTransform() const;
  278. SoundListener* GetComponent<SoundListener> @ GetSoundListener() const;
  279. SoundSource* GetComponent<SoundSource> @ GetSoundSource() const;
  280. StaticModel* GetComponent<StaticModel> @ GetStaticModel() const;
  281. Terrain* GetComponent<Terrain> @ GetTerrain() const;
  282. Zone* GetComponent<Zone> @ GetZone() const;
  283. };
  284. Node* NewNode(Context* context);
  285. void Delete(Node* node);
  286. ${
  287. static Node* NewNode(Context* context)
  288. {
  289. return new Node(context);
  290. }
  291. static void Delete(Node* node)
  292. {
  293. delete node;
  294. }
  295. $}