unit.cpp 15 KB

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