2
0

unit.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 "unit_resource.h"
  28. #include "scene_graph_manager.h"
  29. #include "actor.h"
  30. #include "controller.h"
  31. #include "physics_resource.h"
  32. #include "device.h"
  33. #include "resource_manager.h"
  34. #include "sprite.h"
  35. #include "sprite_animation_player.h"
  36. namespace crown
  37. {
  38. //-----------------------------------------------------------------------------
  39. Unit::Unit(World& w, UnitId unit_id, const ResourceId id, const UnitResource* ur, const Matrix4x4& pose)
  40. : m_world(w)
  41. , m_scene_graph(*w.scene_graph_manager()->create_scene_graph())
  42. , m_sprite_animation(NULL)
  43. , m_resource_id(id)
  44. , m_resource(ur)
  45. , m_id(unit_id)
  46. , m_num_cameras(0)
  47. , m_num_meshes(0)
  48. , m_num_sprites(0)
  49. , m_num_actors(0)
  50. , m_num_materials(0)
  51. , m_values(NULL)
  52. {
  53. m_controller.component.id = INVALID_ID;
  54. create_objects(pose);
  55. }
  56. //-----------------------------------------------------------------------------
  57. Unit::~Unit()
  58. {
  59. destroy_objects();
  60. m_world.scene_graph_manager()->destroy_scene_graph(&m_scene_graph);
  61. }
  62. //-----------------------------------------------------------------------------
  63. void Unit::set_id(const UnitId id)
  64. {
  65. m_id = id;
  66. }
  67. //-----------------------------------------------------------------------------
  68. UnitId Unit::id()
  69. {
  70. return m_id;
  71. }
  72. //-----------------------------------------------------------------------------
  73. const UnitResource* Unit::resource() const
  74. {
  75. return m_resource;
  76. }
  77. //-----------------------------------------------------------------------------
  78. void Unit::create_objects(const Matrix4x4& pose)
  79. {
  80. // Create the scene graph
  81. m_scene_graph.create(pose, m_resource->num_scene_graph_nodes(), m_resource->scene_graph_nodes());
  82. create_camera_objects();
  83. create_renderable_objects();
  84. create_physics_objects();
  85. set_default_material();
  86. m_values = (char*) default_allocator().allocate(m_resource->values_size());
  87. memcpy(m_values, m_resource->values(), m_resource->values_size());
  88. ResourceId anim_id = m_resource->sprite_animation();
  89. if (anim_id.name != 0)
  90. {
  91. m_sprite_animation = m_world.sprite_animation_player()->create_sprite_animation((SpriteAnimationResource*) device()->resource_manager()->get(anim_id));
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. void Unit::destroy_objects()
  96. {
  97. default_allocator().deallocate(m_values);
  98. // Destroy cameras
  99. for (uint32_t i = 0; i < m_num_cameras; i++)
  100. {
  101. m_world.destroy_camera(m_cameras[i].component);
  102. }
  103. m_num_cameras = 0;
  104. // Destroy meshes
  105. for (uint32_t i = 0; i < m_num_meshes; i++)
  106. {
  107. m_world.render_world()->destroy_mesh(m_meshes[i].component);
  108. }
  109. m_num_meshes = 0;
  110. // Destroy sprites
  111. for (uint32_t i = 0; i < m_num_sprites; i++)
  112. {
  113. m_world.render_world()->destroy_sprite(m_sprites[i].component);
  114. }
  115. m_num_sprites = 0;
  116. // Destroy actors
  117. for (uint32_t i = 0; i < m_num_actors; i++)
  118. {
  119. m_world.physics_world()->destroy_actor(m_actors[i].component);
  120. }
  121. m_num_actors = 0;
  122. // Destroy controller
  123. if (m_controller.component.id != INVALID_ID)
  124. {
  125. m_world.physics_world()->destroy_controller(m_controller.component);
  126. m_controller.component.id = INVALID_ID;
  127. }
  128. // Destroy materials
  129. for (uint32_t i = 0; i < m_num_materials; i++)
  130. {
  131. material_manager::get()->destroy_material(m_materials[i].component);
  132. }
  133. m_num_materials = 0;
  134. // Destroy scene graph
  135. m_scene_graph.destroy();
  136. }
  137. //-----------------------------------------------------------------------------
  138. void Unit::create_camera_objects()
  139. {
  140. for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
  141. {
  142. const UnitCamera cam = m_resource->get_camera(i);
  143. const CameraId id = m_world.create_camera(m_scene_graph, cam.node, cam.type, cam.near, cam.far);
  144. add_camera(cam.name, id);
  145. }
  146. }
  147. //-----------------------------------------------------------------------------
  148. void Unit::create_renderable_objects()
  149. {
  150. // Create renderables
  151. for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
  152. {
  153. const UnitRenderable renderable = m_resource->get_renderable(i);
  154. if (renderable.type == UnitRenderable::MESH)
  155. {
  156. MeshResource* mr = (MeshResource*) device()->resource_manager()->get(renderable.resource);
  157. MeshId mesh = m_world.render_world()->create_mesh(mr, m_scene_graph, renderable.node);
  158. add_mesh(renderable.name, mesh);
  159. }
  160. else if (renderable.type == UnitRenderable::SPRITE)
  161. {
  162. SpriteResource* sr = (SpriteResource*) device()->resource_manager()->get(renderable.resource);
  163. SpriteId sprite = m_world.render_world()->create_sprite(sr, m_scene_graph, renderable.node);
  164. add_sprite(renderable.name, sprite);
  165. }
  166. else
  167. {
  168. CE_FATAL("Oops, bad renderable type");
  169. }
  170. }
  171. for (uint32_t i = 0; i < m_resource->num_materials(); i++)
  172. {
  173. const UnitMaterial material = m_resource->get_material(i);
  174. add_material(string::murmur2_32("default", string::strlen("default"), 0), material_manager::get()->create_material(material.id));
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. void Unit::create_physics_objects()
  179. {
  180. if (m_resource->physics_resource().type != 0)
  181. {
  182. const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->get(m_resource->physics_resource());
  183. // Create controller if any
  184. if (pr->has_controller())
  185. {
  186. set_controller(pr->controller().name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
  187. }
  188. // Create actors if any
  189. for (uint32_t i = 0; i < pr->num_actors(); i++)
  190. {
  191. const PhysicsActor& actor = pr->actor(i);
  192. ActorId id = m_world.physics_world()->create_actor(pr, i, m_scene_graph, m_scene_graph.node(actor.node), m_id);
  193. add_actor(actor.name, id);
  194. }
  195. // Create joints if any
  196. for (uint32_t i = 0; i < pr->num_joints(); i++)
  197. {
  198. const PhysicsJoint& joint = pr->joint(i);
  199. Actor* a1 = actor_by_index(joint.actor_0);
  200. Actor* a2 = actor_by_index(joint.actor_1);
  201. m_world.physics_world()->create_joint(pr, i, *a1, *a2);
  202. }
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. void Unit::set_default_material()
  207. {
  208. if (m_num_materials == 0) return;
  209. for (uint32_t i = 0; i < m_num_sprites; i++)
  210. {
  211. Sprite* s = m_world.render_world()->get_sprite(m_sprites[i].component);
  212. s->set_material(m_materials[0].component);
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. int32_t Unit::node(const char* name) const
  217. {
  218. return m_scene_graph.node(name);
  219. }
  220. //-----------------------------------------------------------------------------
  221. bool Unit::has_node(const char* name) const
  222. {
  223. return m_scene_graph.has_node(name);
  224. }
  225. //-----------------------------------------------------------------------------
  226. uint32_t Unit::num_nodes() const
  227. {
  228. return m_scene_graph.num_nodes();
  229. }
  230. //-----------------------------------------------------------------------------
  231. Vector3 Unit::local_position(int32_t node) const
  232. {
  233. return m_scene_graph.local_position(node);
  234. }
  235. //-----------------------------------------------------------------------------
  236. Quaternion Unit::local_rotation(int32_t node) const
  237. {
  238. return m_scene_graph.local_rotation(node);
  239. }
  240. //-----------------------------------------------------------------------------
  241. Matrix4x4 Unit::local_pose(int32_t node) const
  242. {
  243. return m_scene_graph.local_pose(node);
  244. }
  245. //-----------------------------------------------------------------------------
  246. Vector3 Unit::world_position(int32_t node) const
  247. {
  248. return m_scene_graph.world_position(node);
  249. }
  250. //-----------------------------------------------------------------------------
  251. Quaternion Unit::world_rotation(int32_t node) const
  252. {
  253. return m_scene_graph.world_rotation(node);
  254. }
  255. //-----------------------------------------------------------------------------
  256. Matrix4x4 Unit::world_pose(int32_t node) const
  257. {
  258. return m_scene_graph.world_pose(node);
  259. }
  260. //-----------------------------------------------------------------------------
  261. void Unit::set_local_position(int32_t node, const Vector3& pos)
  262. {
  263. m_scene_graph.set_local_position(node, pos);
  264. }
  265. //-----------------------------------------------------------------------------
  266. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  267. {
  268. m_scene_graph.set_local_rotation(node, rot);
  269. }
  270. //-----------------------------------------------------------------------------
  271. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  272. {
  273. m_scene_graph.set_local_pose(node, pose);
  274. }
  275. //-----------------------------------------------------------------------------
  276. void Unit::link_node(int32_t child, int32_t parent)
  277. {
  278. m_scene_graph.link(child, parent);
  279. }
  280. //-----------------------------------------------------------------------------
  281. void Unit::unlink_node(int32_t child)
  282. {
  283. m_scene_graph.unlink(child);
  284. }
  285. //-----------------------------------------------------------------------------
  286. void Unit::update()
  287. {
  288. if (m_sprite_animation)
  289. {
  290. sprite(0u)->set_frame(m_sprite_animation->m_cur_frame);
  291. }
  292. }
  293. //-----------------------------------------------------------------------------
  294. void Unit::reload(UnitResource* new_ur)
  295. {
  296. Matrix4x4 m = m_scene_graph.world_pose(0);
  297. destroy_objects();
  298. m_resource = new_ur;
  299. create_objects(m);
  300. }
  301. //-----------------------------------------------------------------------------
  302. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  303. {
  304. Component comp;
  305. comp.name = name;
  306. comp.component = component;
  307. array[size] = comp;
  308. size++;
  309. }
  310. //-----------------------------------------------------------------------------
  311. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  312. {
  313. const uint32_t name_hash = string::murmur2_32(name, string::strlen(name), 0);
  314. return find_component_by_name(name_hash, size, array);
  315. }
  316. //-----------------------------------------------------------------------------
  317. Id Unit::find_component_by_index(uint32_t index, uint32_t size, Component* array)
  318. {
  319. Id comp;
  320. comp.id = INVALID_ID;
  321. if (index < size)
  322. {
  323. comp = array[index].component;
  324. }
  325. return comp;
  326. }
  327. //-----------------------------------------------------------------------------
  328. Id Unit::find_component_by_name(StringId32 name, uint32_t size, Component* array)
  329. {
  330. Id comp;
  331. comp.id = INVALID_ID;
  332. for (uint32_t i = 0; i < size; i++)
  333. {
  334. if (name == array[i].name)
  335. {
  336. comp = array[i].component;
  337. }
  338. }
  339. return comp;
  340. }
  341. //-----------------------------------------------------------------------------
  342. void Unit::add_camera(StringId32 name, CameraId camera)
  343. {
  344. CE_ASSERT(m_num_cameras < CE_MAX_CAMERA_COMPONENTS, "Max camera number reached");
  345. add_component(name, camera, m_num_cameras, m_cameras);
  346. }
  347. //-----------------------------------------------------------------------------
  348. void Unit::add_mesh(StringId32 name, MeshId mesh)
  349. {
  350. CE_ASSERT(m_num_meshes < CE_MAX_MESH_COMPONENTS, "Max mesh number reached");
  351. add_component(name, mesh, m_num_meshes, m_meshes);
  352. }
  353. //-----------------------------------------------------------------------------
  354. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  355. {
  356. CE_ASSERT(m_num_sprites < CE_MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  357. add_component(name, sprite, m_num_sprites, m_sprites);
  358. }
  359. //-----------------------------------------------------------------------------
  360. void Unit::add_actor(StringId32 name, ActorId actor)
  361. {
  362. CE_ASSERT(m_num_actors < CE_MAX_ACTOR_COMPONENTS, "Max actor number reached");
  363. add_component(name, actor, m_num_actors, m_actors);
  364. }
  365. //-----------------------------------------------------------------------------
  366. void Unit::add_material(StringId32 name, MaterialId material)
  367. {
  368. CE_ASSERT(m_num_materials < CE_MAX_MATERIAL_COMPONENTS, "Max material number reached");
  369. add_component(name, material, m_num_materials, m_materials);
  370. }
  371. //-----------------------------------------------------------------------------
  372. void Unit::set_controller(StringId32 name, ControllerId controller)
  373. {
  374. m_controller.name = name;
  375. m_controller.component = controller;
  376. }
  377. //-----------------------------------------------------------------------------
  378. Camera* Unit::camera(const char* name)
  379. {
  380. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  381. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  382. return m_world.get_camera(cam);
  383. }
  384. //-----------------------------------------------------------------------------
  385. Camera* Unit::camera(uint32_t i)
  386. {
  387. CameraId cam = find_component_by_index(i, m_num_cameras, m_cameras);
  388. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  389. return m_world.get_camera(cam);
  390. }
  391. //-----------------------------------------------------------------------------
  392. Mesh* Unit::mesh(const char* name)
  393. {
  394. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  395. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  396. return m_world.render_world()->get_mesh(mesh);
  397. }
  398. //-----------------------------------------------------------------------------
  399. Mesh* Unit::mesh(uint32_t i)
  400. {
  401. MeshId mesh = find_component_by_index(i, m_num_meshes, m_meshes);
  402. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  403. return m_world.render_world()->get_mesh(mesh);
  404. }
  405. //-----------------------------------------------------------------------------
  406. Sprite* Unit::sprite(const char* name)
  407. {
  408. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  409. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  410. return m_world.render_world()->get_sprite(sprite);
  411. }
  412. //-----------------------------------------------------------------------------
  413. Sprite* Unit::sprite(uint32_t i)
  414. {
  415. SpriteId sprite = find_component_by_index(i, m_num_sprites, m_sprites);
  416. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  417. return m_world.render_world()->get_sprite(sprite);
  418. }
  419. //-----------------------------------------------------------------------------
  420. Actor* Unit::actor(const char* name)
  421. {
  422. ActorId actor = find_component(name, m_num_actors, m_actors);
  423. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  424. return m_world.physics_world()->get_actor(actor);
  425. }
  426. //-----------------------------------------------------------------------------
  427. Actor* Unit::actor(uint32_t i)
  428. {
  429. ActorId actor = find_component_by_index(i, m_num_actors, m_actors);
  430. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with index '%d'", i);
  431. return m_world.physics_world()->get_actor(actor);
  432. }
  433. //-----------------------------------------------------------------------------
  434. Actor* Unit::actor_by_index(StringId32 name)
  435. {
  436. ActorId actor = find_component_by_name(name, m_num_actors, m_actors);
  437. // CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", name);
  438. return m_world.physics_world()->get_actor(actor);
  439. }
  440. //-----------------------------------------------------------------------------
  441. Controller* Unit::controller()
  442. {
  443. if (m_controller.component.id != INVALID_ID)
  444. {
  445. return m_world.physics_world()->get_controller(m_controller.component);
  446. }
  447. return NULL;
  448. }
  449. //-----------------------------------------------------------------------------
  450. Material* Unit::material(const char* name)
  451. {
  452. MaterialId material = find_component(name, m_num_materials, m_materials);
  453. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%s'", name);
  454. return material_manager::get()->lookup_material(material);
  455. }
  456. //-----------------------------------------------------------------------------
  457. Material* Unit::material(uint32_t i)
  458. {
  459. MaterialId material = find_component_by_index(i, m_num_materials, m_materials);
  460. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%d'", i);
  461. return material_manager::get()->lookup_material(material);
  462. }
  463. //-----------------------------------------------------------------------------
  464. bool Unit::is_a(const char* name)
  465. {
  466. return m_resource_id == ResourceId("unit", name);
  467. }
  468. //-----------------------------------------------------------------------------
  469. void Unit::play_sprite_animation(const char* name, bool loop)
  470. {
  471. if (m_sprite_animation)
  472. m_sprite_animation->play(string::murmur2_32(name, string::strlen(name), 0), loop);
  473. }
  474. //-----------------------------------------------------------------------------
  475. void Unit::stop_sprite_animation()
  476. {
  477. if (m_sprite_animation)
  478. m_sprite_animation->stop();
  479. }
  480. //-----------------------------------------------------------------------------
  481. bool Unit::has_key(const char* k) const
  482. {
  483. return m_resource->has_key(k);
  484. }
  485. //-----------------------------------------------------------------------------
  486. ValueType::Enum Unit::value_type(const char* k)
  487. {
  488. Key key;
  489. m_resource->get_key(k, key);
  490. return (ValueType::Enum) key.type;
  491. }
  492. //-----------------------------------------------------------------------------
  493. bool Unit::get_key(const char* k, bool& v) const
  494. {
  495. Key key;
  496. bool has = m_resource->get_key(k, key);
  497. v = *(uint32_t*)(m_values + key.offset);
  498. return has;
  499. }
  500. //-----------------------------------------------------------------------------
  501. bool Unit::get_key(const char* k, float& v) const
  502. {
  503. Key key;
  504. bool has = m_resource->get_key(k, key);
  505. v = *(float*)(m_values + key.offset);
  506. return has;
  507. }
  508. //-----------------------------------------------------------------------------
  509. bool Unit::get_key(const char* k, StringId32& v) const
  510. {
  511. Key key;
  512. bool has = m_resource->get_key(k, key);
  513. v = *(StringId32*)(m_values + key.offset);
  514. return has;
  515. }
  516. //-----------------------------------------------------------------------------
  517. bool Unit::get_key(const char* k, Vector3& v) const
  518. {
  519. Key key;
  520. bool has = m_resource->get_key(k, key);
  521. v = *(Vector3*)(m_values + key.offset);
  522. return has;
  523. }
  524. //-----------------------------------------------------------------------------
  525. void Unit::set_key(const char* k, bool v)
  526. {
  527. Key key;
  528. m_resource->get_key(k, key);
  529. *(uint32_t*)(m_values + key.offset) = v;
  530. }
  531. //-----------------------------------------------------------------------------
  532. void Unit::set_key(const char* k, float v)
  533. {
  534. Key key;
  535. m_resource->get_key(k, key);
  536. *(float*)(m_values + key.offset) = v;
  537. }
  538. //-----------------------------------------------------------------------------
  539. void Unit::set_key(const char* k, const char* v)
  540. {
  541. Key key;
  542. m_resource->get_key(k, key);
  543. *(StringId32*)(m_values + key.offset) = string::murmur2_32(v, string::strlen(v));
  544. }
  545. //-----------------------------------------------------------------------------
  546. void Unit::set_key(const char* k, const Vector3& v)
  547. {
  548. Key key;
  549. m_resource->get_key(k, key);
  550. *(Vector3*)(m_values + key.offset) = v;
  551. }
  552. } // namespace crown