Unit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #include "Unit.h"
  24. #include "World.h"
  25. #include "Allocator.h"
  26. #include "Log.h"
  27. #include "UnitResource.h"
  28. #include "SceneGraphManager.h"
  29. #include "Actor.h"
  30. #include "Controller.h"
  31. #include "PhysicsResource.h"
  32. #include "Device.h"
  33. #include "ResourceManager.h"
  34. namespace crown
  35. {
  36. typedef Id CameraId;
  37. typedef Id SpriteId;
  38. //-----------------------------------------------------------------------------
  39. Unit::Unit(World& w, SceneGraph& sg, const UnitResource* ur, const Matrix4x4& pose)
  40. : m_world(w)
  41. , m_scene_graph(sg)
  42. , m_resource(ur)
  43. , m_num_cameras(0)
  44. , m_num_meshes(0)
  45. , m_num_sprites(0)
  46. {
  47. m_controller.component.id = INVALID_ID;
  48. create(pose);
  49. }
  50. //-----------------------------------------------------------------------------
  51. Unit::~Unit()
  52. {
  53. // Destroy cameras
  54. for (uint32_t i = 0; i < m_num_cameras; i++)
  55. {
  56. m_world.destroy_camera(m_cameras[i].component);
  57. }
  58. // Destroy meshes
  59. for (uint32_t i = 0; i < m_num_meshes; i++)
  60. {
  61. m_world.render_world()->destroy_mesh(m_meshes[i].component);
  62. }
  63. // Destroy sprites
  64. for (uint32_t i = 0; i < m_num_sprites; i++)
  65. {
  66. m_world.render_world()->destroy_sprite(m_sprites[i].component);
  67. }
  68. // Destroy actors
  69. for (uint32_t i = 0; i < m_num_actors; i++)
  70. {
  71. m_world.physics_world()->destroy_actor(m_actors[i].component);
  72. }
  73. // Destroy controller
  74. if (m_controller.component.id != INVALID_ID)
  75. {
  76. m_world.physics_world()->destroy_controller(m_controller.component);
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. void Unit::set_id(const UnitId id)
  81. {
  82. m_id = id;
  83. }
  84. //-----------------------------------------------------------------------------
  85. UnitId Unit::id()
  86. {
  87. return m_id;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void Unit::create(const Matrix4x4& pose)
  91. {
  92. // Create the scene graph
  93. m_scene_graph.create(m_resource->num_scene_graph_nodes(), m_resource->scene_graph_names(),
  94. m_resource->scene_graph_poses(), m_resource->scene_graph_parents());
  95. // Set root node's pose
  96. m_scene_graph.set_local_pose(0, pose);
  97. create_camera_objects();
  98. create_renderable_objects();
  99. create_physics_objects();
  100. }
  101. //-----------------------------------------------------------------------------
  102. void Unit::create_camera_objects()
  103. {
  104. for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
  105. {
  106. const UnitCamera camera = m_resource->get_camera(i);
  107. const CameraId cam = m_world.create_camera(m_scene_graph, camera.node);
  108. add_camera(camera.name, cam);
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. void Unit::create_renderable_objects()
  113. {
  114. for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
  115. {
  116. const UnitRenderable renderable = m_resource->get_renderable(i);
  117. switch (renderable.type)
  118. {
  119. case UnitRenderable::MESH: add_mesh(renderable.name, m_world.render_world()->create_mesh(renderable.resource, m_scene_graph, renderable.node)); break;
  120. case UnitRenderable::SPRITE: add_sprite(renderable.name, m_world.render_world()->create_sprite(renderable.resource, m_scene_graph, renderable.node)); break;
  121. default: CE_FATAL("Oops, bad renderable type"); break;
  122. }
  123. }
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Unit::create_physics_objects()
  127. {
  128. if (m_resource->physics_resource().id != 0)
  129. {
  130. const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->data(m_resource->physics_resource());
  131. // Create controller if any
  132. if (pr->has_controller())
  133. {
  134. set_controller(pr->controller().name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
  135. }
  136. // Create actors if any
  137. for (uint32_t i = 0; i < pr->num_actors(); i++)
  138. {
  139. const PhysicsActor actor = pr->actor(i);
  140. add_actor(actor.name, m_world.physics_world()->create_actor(m_scene_graph, m_scene_graph.node(actor.node), ActorType::DYNAMIC_PHYSICAL));
  141. }
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. int32_t Unit::node(const char* name) const
  146. {
  147. return m_scene_graph.node(name);
  148. }
  149. //-----------------------------------------------------------------------------
  150. bool Unit::has_node(const char* name) const
  151. {
  152. return m_scene_graph.has_node(name);
  153. }
  154. //-----------------------------------------------------------------------------
  155. uint32_t Unit::num_nodes() const
  156. {
  157. return m_scene_graph.num_nodes();
  158. }
  159. //-----------------------------------------------------------------------------
  160. Vector3 Unit::local_position(int32_t node) const
  161. {
  162. return m_scene_graph.local_position(node);
  163. }
  164. //-----------------------------------------------------------------------------
  165. Quaternion Unit::local_rotation(int32_t node) const
  166. {
  167. return m_scene_graph.local_rotation(node);
  168. }
  169. //-----------------------------------------------------------------------------
  170. Matrix4x4 Unit::local_pose(int32_t node) const
  171. {
  172. return m_scene_graph.local_pose(node);
  173. }
  174. //-----------------------------------------------------------------------------
  175. Vector3 Unit::world_position(int32_t node) const
  176. {
  177. return m_scene_graph.world_position(node);
  178. }
  179. //-----------------------------------------------------------------------------
  180. Quaternion Unit::world_rotation(int32_t node) const
  181. {
  182. return m_scene_graph.world_rotation(node);
  183. }
  184. //-----------------------------------------------------------------------------
  185. Matrix4x4 Unit::world_pose(int32_t node) const
  186. {
  187. return m_scene_graph.world_pose(node);
  188. }
  189. //-----------------------------------------------------------------------------
  190. void Unit::set_local_position(int32_t node, const Vector3& pos)
  191. {
  192. m_scene_graph.set_local_position(node, pos);
  193. }
  194. //-----------------------------------------------------------------------------
  195. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  196. {
  197. m_scene_graph.set_local_rotation(node, rot);
  198. }
  199. //-----------------------------------------------------------------------------
  200. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  201. {
  202. m_scene_graph.set_local_pose(node, pose);
  203. }
  204. //-----------------------------------------------------------------------------
  205. void Unit::link_node(int32_t child, int32_t parent)
  206. {
  207. m_scene_graph.link(child, parent);
  208. }
  209. //-----------------------------------------------------------------------------
  210. void Unit::unlink_node(int32_t child)
  211. {
  212. m_scene_graph.unlink(child);
  213. }
  214. //-----------------------------------------------------------------------------
  215. void Unit::update()
  216. {
  217. }
  218. //-----------------------------------------------------------------------------
  219. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  220. {
  221. Component comp;
  222. comp.name = name;
  223. comp.component = component;
  224. array[size] = comp;
  225. size++;
  226. }
  227. //-----------------------------------------------------------------------------
  228. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  229. {
  230. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  231. Id comp;
  232. comp.id = INVALID_ID;
  233. for (uint32_t i = 0; i < size; i++)
  234. {
  235. if (name_hash == array[i].name)
  236. {
  237. comp = array[i].component;
  238. }
  239. }
  240. return comp;
  241. }
  242. //-----------------------------------------------------------------------------
  243. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  244. {
  245. Id comp;
  246. comp.id = INVALID_ID;
  247. if (index < size)
  248. {
  249. comp = array[index].component;
  250. }
  251. return comp;
  252. }
  253. //-----------------------------------------------------------------------------
  254. void Unit::add_camera(StringId32 name, CameraId camera)
  255. {
  256. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  257. add_component(name, camera, m_num_cameras, m_cameras);
  258. }
  259. //-----------------------------------------------------------------------------
  260. void Unit::add_mesh(StringId32 name, MeshId mesh)
  261. {
  262. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  263. add_component(name, mesh, m_num_meshes, m_meshes);
  264. }
  265. //-----------------------------------------------------------------------------
  266. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  267. {
  268. CE_ASSERT(m_num_sprites < MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  269. add_component(name, sprite, m_num_sprites, m_sprites);
  270. }
  271. //-----------------------------------------------------------------------------
  272. void Unit::add_actor(StringId32 name, ActorId actor)
  273. {
  274. CE_ASSERT(m_num_actors < MAX_ACTOR_COMPONENTS, "Max actor number reached");
  275. add_component(name, actor, m_num_actors, m_actors);
  276. }
  277. //-----------------------------------------------------------------------------
  278. void Unit::set_controller(StringId32 name, ControllerId controller)
  279. {
  280. m_controller.name = name;
  281. m_controller.component = controller;
  282. }
  283. //-----------------------------------------------------------------------------
  284. Camera* Unit::camera(const char* name)
  285. {
  286. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  287. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  288. return m_world.lookup_camera(cam);
  289. }
  290. //-----------------------------------------------------------------------------
  291. Camera* Unit::camera(uint32_t i)
  292. {
  293. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  294. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  295. return m_world.lookup_camera(cam);
  296. }
  297. //-----------------------------------------------------------------------------
  298. Mesh* Unit::mesh(const char* name)
  299. {
  300. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  301. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  302. return m_world.render_world()->lookup_mesh(mesh);
  303. }
  304. //-----------------------------------------------------------------------------
  305. Mesh* Unit::mesh(uint32_t i)
  306. {
  307. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  308. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  309. return m_world.render_world()->lookup_mesh(mesh);
  310. }
  311. //-----------------------------------------------------------------------------
  312. Sprite* Unit::sprite(const char* name)
  313. {
  314. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  315. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  316. return m_world.render_world()->lookup_sprite(sprite);
  317. }
  318. //-----------------------------------------------------------------------------
  319. Sprite* Unit::sprite(uint32_t i)
  320. {
  321. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  322. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  323. return m_world.render_world()->lookup_sprite(sprite);
  324. }
  325. //-----------------------------------------------------------------------------
  326. Actor* Unit::actor(const char* name)
  327. {
  328. ActorId actor = find_component(name, m_num_actors, m_actors);
  329. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  330. return m_world.physics_world()->lookup_actor(actor);
  331. }
  332. //-----------------------------------------------------------------------------
  333. Actor* Unit::actor(uint32_t i)
  334. {
  335. ActorId actor = find_component(i, m_num_actors, m_actors);
  336. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
  337. return m_world.physics_world()->lookup_actor(actor);
  338. }
  339. //-----------------------------------------------------------------------------
  340. Controller* Unit::controller()
  341. {
  342. if (m_controller.component.id != INVALID_ID)
  343. {
  344. return m_world.physics_world()->lookup_controller(m_controller.component);
  345. }
  346. return NULL;
  347. }
  348. } // namespace crown