Node.pkg 17 KB

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