unit.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "math_types.h"
  26. #include "string_utils.h"
  27. #include "scene_graph.h"
  28. #include "string_utils.h"
  29. #include "physics_types.h"
  30. #include "world_types.h"
  31. #include "render_world_types.h"
  32. #include "config.h"
  33. #include "sprite_animation.h"
  34. namespace crown
  35. {
  36. //-----------------------------------------------------------------------------
  37. struct ComponentType
  38. {
  39. enum Enum
  40. {
  41. UNKNOWN,
  42. CAMERA,
  43. MESH,
  44. SPRITE,
  45. ACTOR
  46. };
  47. };
  48. typedef Id ComponentId;
  49. typedef Id MaterialId;
  50. struct Component
  51. {
  52. uint32_t name;
  53. ComponentId component;
  54. };
  55. class SceneGraphManager;
  56. class World;
  57. struct Actor;
  58. struct Camera;
  59. struct Controller;
  60. struct Mesh;
  61. struct Sprite;
  62. struct Material;
  63. struct UnitResource;
  64. /// Represents a game entity.
  65. ///
  66. /// @ingroup World
  67. struct Unit
  68. {
  69. Unit(World& w, UnitId unit_id, const ResourceId id, const UnitResource* ur, const Matrix4x4& pose);
  70. ~Unit();
  71. void set_id(const UnitId id);
  72. UnitId id();
  73. const UnitResource* resource() const;
  74. /// Returns the node @a name.
  75. int32_t node(const char* name) const;
  76. /// Returns whether the unit has the node @a name.
  77. bool has_node(const char* name) const;
  78. /// Returns the number of nodes of the unit.
  79. uint32_t num_nodes() const;
  80. /// Returns the local position of the unit.
  81. Vector3 local_position(int32_t node) const;
  82. /// Returns the local rotation of the unit.
  83. Quaternion local_rotation(int32_t node) const;
  84. /// Returns the local pose of the unit.
  85. Matrix4x4 local_pose(int32_t node) const;
  86. /// Returns the world position of the unit.
  87. Vector3 world_position(int32_t node) const;
  88. /// Returns the world rotation of the unit.
  89. Quaternion world_rotation(int32_t node) const;
  90. /// Returns the world pose of the unit.
  91. Matrix4x4 world_pose(int32_t node) const;
  92. /// Sets the local position of the unit.
  93. void set_local_position(int32_t node, const Vector3& pos);
  94. /// Sets the local rotation of the unit.
  95. void set_local_rotation(int32_t node, const Quaternion& rot);
  96. /// Sets the local pose of the unit.
  97. void set_local_pose(int32_t node, const Matrix4x4& pose);
  98. /// Links the @a child node to the @a parent node.
  99. /// After the linking the @a child pose is reset to identity.
  100. /// @note The @a parent node must be either -1 (meaning no parent), or an index lesser than child.
  101. void link_node(int32_t child, int32_t parent);
  102. /// Unlinks @a child from its parent, if any.
  103. void unlink_node(int32_t child);
  104. void update();
  105. void reload(UnitResource* new_ur);
  106. void add_component(StringId32 name, Id component, uint32_t& size, Component* array);
  107. Id find_component(const char* name, uint32_t size, Component* array);
  108. Id find_component_by_index(uint32_t index, uint32_t size, Component* array);
  109. Id find_component_by_name(StringId32 name, uint32_t size, Component* array);
  110. void add_camera(StringId32 name, CameraId camera);
  111. void add_mesh(StringId32 name, MeshId mesh);
  112. void add_sprite(StringId32 name, SpriteId sprite);
  113. void add_actor(StringId32 name, ActorId actor);
  114. void set_controller(StringId32 name, ControllerId controller);
  115. void add_material(StringId32 name, MaterialId material);
  116. Camera* camera(const char* name);
  117. Camera* camera(uint32_t i);
  118. Mesh* mesh(const char* name);
  119. Mesh* mesh(uint32_t i);
  120. Sprite* sprite(const char* name);
  121. Sprite* sprite(uint32_t i);
  122. Actor* actor(const char* name);
  123. Actor* actor(uint32_t i);
  124. Actor* actor_by_index(StringId32 name);
  125. Material* material(const char* name);
  126. Material* material(uint32_t i);
  127. Controller* controller();
  128. bool is_a(const char* name);
  129. void play_sprite_animation(const char* name, bool loop);
  130. void stop_sprite_animation();
  131. bool has_key(const char* k) const;
  132. ValueType::Enum value_type(const char* k);
  133. bool get_key(const char* k, bool& v) const;
  134. bool get_key(const char* k, float& v) const;
  135. bool get_key(const char* k, StringId32& v) const;
  136. bool get_key(const char* k, Vector3& v) const;
  137. void set_key(const char* k, bool v);
  138. void set_key(const char* k, float v);
  139. void set_key(const char* k, const char* v);
  140. void set_key(const char* k, const Vector3& v);
  141. private:
  142. void create_objects(const Matrix4x4& pose);
  143. void destroy_objects();
  144. void create_camera_objects();
  145. void create_renderable_objects();
  146. void create_physics_objects();
  147. void set_default_material();
  148. public:
  149. World& m_world;
  150. SceneGraph& m_scene_graph;
  151. SpriteAnimation* m_sprite_animation;
  152. const ResourceId m_resource_id;
  153. const UnitResource* m_resource;
  154. UnitId m_id;
  155. uint32_t m_num_cameras;
  156. Component m_cameras[CE_MAX_CAMERA_COMPONENTS];
  157. uint32_t m_num_meshes;
  158. Component m_meshes[CE_MAX_MESH_COMPONENTS];
  159. uint32_t m_num_sprites;
  160. Component m_sprites[CE_MAX_SPRITE_COMPONENTS];
  161. uint32_t m_num_actors;
  162. Component m_actors[CE_MAX_ACTOR_COMPONENTS];
  163. uint32_t m_num_materials;
  164. Component m_materials[CE_MAX_MATERIAL_COMPONENTS];
  165. Component m_controller;
  166. char* m_values;
  167. };
  168. } // namespace crown