Unit.cpp 19 KB

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