Unit.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ComponentId;
  34. typedef Id UnitId;
  35. //-----------------------------------------------------------------------------
  36. struct ComponentType
  37. {
  38. enum Enum
  39. {
  40. UNKNOWN,
  41. CAMERA,
  42. MESH,
  43. SOUND
  44. };
  45. };
  46. //-----------------------------------------------------------------------------
  47. struct Component
  48. {
  49. Id32 id;
  50. ComponentType::Enum type;
  51. ComponentId component;
  52. };
  53. struct ComponentList
  54. {
  55. ComponentList()
  56. : m_components(default_allocator())
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. bool next_free_component(uint32_t& index)
  61. {
  62. uint32_t i;
  63. for (i = 0; i < m_components.size(); i++)
  64. {
  65. // id == 0 means free slot
  66. if (m_components[i].id == 0)
  67. {
  68. index = i;
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. //-----------------------------------------------------------------------------
  75. void add_component(const char* name, uint32_t type, ComponentId component)
  76. {
  77. CE_ASSERT_NOT_NULL(name);
  78. uint32_t key = hash::murmur2_32(name, string::strlen(name), 0);
  79. uint32_t free_index;
  80. if (next_free_component(free_index))
  81. {
  82. m_components[free_index].id.key = key;
  83. m_components[free_index].type = (ComponentType::Enum)type;
  84. m_components[free_index].component = component;
  85. }
  86. Component comp;
  87. comp.id.key = key;
  88. comp.type = (ComponentType::Enum)type;
  89. comp.component = component;
  90. m_components.push_back(comp);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void remove_component(const char* name)
  94. {
  95. uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
  96. for (uint32_t i = 0; i < m_components.size(); i++)
  97. {
  98. if (m_components[i].id == hashed_name)
  99. {
  100. m_components[i].id = 0;
  101. return;
  102. }
  103. }
  104. CE_FATAL("Component not found!");
  105. }
  106. //-----------------------------------------------------------------------------
  107. Component* get_component(const char* name)
  108. {
  109. uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
  110. for (uint32_t i = 0; i < m_components.size(); i++)
  111. {
  112. if (m_components[i].id == hashed_name)
  113. {
  114. return &m_components[i];
  115. }
  116. }
  117. CE_FATAL("Component not found!");
  118. }
  119. public:
  120. List<Component> m_components;
  121. };
  122. //-----------------------------------------------------------------------------
  123. struct UnitResource
  124. {
  125. };
  126. class Camera;
  127. class Mesh;
  128. class World;
  129. struct Unit
  130. {
  131. Unit();
  132. void create(World& creator, SceneGraph& graph, ComponentList& components, UnitId id, const Vector3& pos, const Quaternion& rot);
  133. void destroy();
  134. void load(UnitResource* ur);
  135. void unload();
  136. void reload(UnitResource* new_ur);
  137. Vector3 local_position(int32_t node = 0) const;
  138. Quaternion local_rotation(int32_t node = 0) const;
  139. Matrix4x4 local_pose(int32_t node = 0) const;
  140. Vector3 world_position(int32_t node = 0) const;
  141. Quaternion world_rotation(int32_t node = 0) const;
  142. Matrix4x4 world_pose(int32_t node = 0) const;
  143. void set_local_position(const Vector3& pos, int32_t node = 0);
  144. void set_local_rotation(const Quaternion& rot, int32_t node = 0);
  145. void set_local_pose(const Matrix4x4& pose, int32_t node = 0);
  146. Camera* camera(const char* name);
  147. Mesh* mesh(const char* name);
  148. private:
  149. bool next_free_component(uint32_t& index);
  150. public:
  151. World* m_creator;
  152. UnitResource* m_resource;
  153. UnitId m_id;
  154. int32_t m_root_node;
  155. SceneGraph* m_scene_graph;
  156. ComponentList* m_component;
  157. };
  158. } // namespace crown