unit.cpp 14 KB

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