Unit.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "Vector3.h"
  25. #include "Quaternion.h"
  26. #include "Matrix4x4.h"
  27. #include "Hash.h"
  28. #include "IdTable.h"
  29. #include "SceneGraph.h"
  30. #include "StringUtils.h"
  31. namespace crown
  32. {
  33. typedef Id CameraId;
  34. typedef Id ComponentId;
  35. typedef Id UnitId;
  36. //-----------------------------------------------------------------------------
  37. struct ComponentType
  38. {
  39. enum Enum
  40. {
  41. UNKNOWN,
  42. CAMERA,
  43. MESH,
  44. SPRITE,
  45. SOUND
  46. };
  47. };
  48. struct Component
  49. {
  50. uint32_t name;
  51. Id component;
  52. };
  53. typedef Id UnitId;
  54. typedef Id MeshId;
  55. typedef Id SpriteId;
  56. class Camera;
  57. class Mesh;
  58. class Sprite;
  59. class World;
  60. struct UnitResource;
  61. #define MAX_CAMERA_COMPONENTS 8
  62. #define MAX_MESH_COMPONENTS 8
  63. #define MAX_SPRITE_COMPONENTS 8
  64. struct Unit
  65. {
  66. Unit();
  67. void create(World& world, UnitResource* ur, UnitId id, const Vector3& pos, const Quaternion& rot);
  68. void destroy();
  69. Vector3 local_position(int32_t node = 0) const;
  70. Quaternion local_rotation(int32_t node = 0) const;
  71. Matrix4x4 local_pose(int32_t node = 0) const;
  72. Vector3 world_position(int32_t node = 0) const;
  73. Quaternion world_rotation(int32_t node = 0) const;
  74. Matrix4x4 world_pose(int32_t node = 0) const;
  75. void set_local_position(const Vector3& pos, int32_t node = 0);
  76. void set_local_rotation(const Quaternion& rot, int32_t node = 0);
  77. void set_local_pose(const Matrix4x4& pose, int32_t node = 0);
  78. void link_node(int32_t child, int32_t parent);
  79. void unlink_node(int32_t child);
  80. void add_component(uint32_t name, Id component, uint32_t& size, Component* array);
  81. Id find_component(const char* name, uint32_t size, Component* array);
  82. Id find_component(uint32_t index, uint32_t size, Component* array);
  83. void add_camera(uint32_t name, CameraId camera);
  84. void add_mesh(uint32_t name, MeshId mesh);
  85. void add_sprite(uint32_t name, SpriteId sprite);
  86. Camera* camera(const char* name);
  87. Camera* camera(uint32_t i);
  88. Mesh* mesh(const char* name);
  89. Mesh* mesh(uint32_t i);
  90. Sprite* sprite(const char* name);
  91. Sprite* sprite(uint32_t i);
  92. public:
  93. World* m_world;
  94. UnitResource* m_resource;
  95. UnitId m_id;
  96. int32_t m_root_node;
  97. SceneGraph m_scene_graph;
  98. uint32_t m_num_cameras;
  99. Component m_cameras[MAX_CAMERA_COMPONENTS];
  100. uint32_t m_num_meshes;
  101. Component m_meshes[MAX_MESH_COMPONENTS];
  102. uint32_t m_num_sprites;
  103. Component m_sprites[MAX_SPRITE_COMPONENTS];
  104. };
  105. } // namespace crown