unit.cpp 13 KB

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