unit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 "allocator.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(murmur2_32("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. set_controller(physics_resource::controller(pr)->name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
  158. }
  159. // Create actors if any
  160. for (uint32_t i = 0; i < num_actors(pr); i++)
  161. {
  162. const PhysicsActor* actor = physics_resource::actor(pr, i);
  163. ActorId id = m_world.physics_world()->create_actor(pr, i, m_scene_graph, m_scene_graph.node(actor->node), m_id);
  164. add_actor(actor->name, id);
  165. }
  166. // Create joints if any
  167. for (uint32_t i = 0; i < num_joints(pr); i++)
  168. {
  169. const PhysicsJoint* joint = physics_resource::joint(pr, i);
  170. Actor* a1 = actor_by_index(joint->actor_0);
  171. Actor* a2 = actor_by_index(joint->actor_1);
  172. m_world.physics_world()->create_joint(pr, i, *a1, *a2);
  173. }
  174. }
  175. }
  176. void Unit::set_default_material()
  177. {
  178. if (m_num_materials == 0) return;
  179. for (uint32_t i = 0; i < m_num_sprites; i++)
  180. {
  181. Sprite* s = m_world.render_world()->get_sprite(m_sprites[i].component);
  182. s->set_material(m_materials[0].component);
  183. }
  184. }
  185. int32_t Unit::node(const char* name) const
  186. {
  187. return m_scene_graph.node(name);
  188. }
  189. bool Unit::has_node(const char* name) const
  190. {
  191. return m_scene_graph.has_node(name);
  192. }
  193. uint32_t Unit::num_nodes() const
  194. {
  195. return m_scene_graph.num_nodes();
  196. }
  197. Vector3 Unit::local_position(int32_t node) const
  198. {
  199. return m_scene_graph.local_position(node);
  200. }
  201. Quaternion Unit::local_rotation(int32_t node) const
  202. {
  203. return m_scene_graph.local_rotation(node);
  204. }
  205. Matrix4x4 Unit::local_pose(int32_t node) const
  206. {
  207. return m_scene_graph.local_pose(node);
  208. }
  209. Vector3 Unit::world_position(int32_t node) const
  210. {
  211. return m_scene_graph.world_position(node);
  212. }
  213. Quaternion Unit::world_rotation(int32_t node) const
  214. {
  215. return m_scene_graph.world_rotation(node);
  216. }
  217. Matrix4x4 Unit::world_pose(int32_t node) const
  218. {
  219. return m_scene_graph.world_pose(node);
  220. }
  221. void Unit::set_local_position(int32_t node, const Vector3& pos)
  222. {
  223. m_scene_graph.set_local_position(node, pos);
  224. }
  225. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  226. {
  227. m_scene_graph.set_local_rotation(node, rot);
  228. }
  229. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  230. {
  231. m_scene_graph.set_local_pose(node, pose);
  232. }
  233. void Unit::link_node(int32_t child, int32_t parent)
  234. {
  235. m_scene_graph.link(child, parent);
  236. }
  237. void Unit::unlink_node(int32_t child)
  238. {
  239. m_scene_graph.unlink(child);
  240. }
  241. void Unit::update()
  242. {
  243. if (m_sprite_animation)
  244. {
  245. sprite(0u)->set_frame(m_sprite_animation->m_cur_frame);
  246. }
  247. }
  248. void Unit::reload(UnitResource* new_ur)
  249. {
  250. Matrix4x4 m = m_scene_graph.world_pose(0);
  251. destroy_objects();
  252. m_resource = new_ur;
  253. create_objects(m);
  254. }
  255. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  256. {
  257. Component comp;
  258. comp.name = name;
  259. comp.component = component;
  260. array[size] = comp;
  261. size++;
  262. }
  263. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  264. {
  265. const uint32_t name_hash = murmur2_32(name, strlen(name), 0);
  266. return find_component_by_name(name_hash, size, array);
  267. }
  268. Id Unit::find_component_by_index(uint32_t index, uint32_t size, Component* array)
  269. {
  270. Id comp;
  271. comp.id = INVALID_ID;
  272. if (index < size)
  273. {
  274. comp = array[index].component;
  275. }
  276. return comp;
  277. }
  278. Id Unit::find_component_by_name(StringId32 name, uint32_t size, Component* array)
  279. {
  280. Id comp;
  281. comp.id = INVALID_ID;
  282. for (uint32_t i = 0; i < size; i++)
  283. {
  284. if (name == array[i].name)
  285. {
  286. comp = array[i].component;
  287. }
  288. }
  289. return comp;
  290. }
  291. void Unit::add_camera(StringId32 name, CameraId camera)
  292. {
  293. CE_ASSERT(m_num_cameras < CE_MAX_CAMERA_COMPONENTS, "Max camera number reached");
  294. add_component(name, camera, m_num_cameras, m_cameras);
  295. }
  296. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  297. {
  298. CE_ASSERT(m_num_sprites < CE_MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  299. add_component(name, sprite, m_num_sprites, m_sprites);
  300. }
  301. void Unit::add_actor(StringId32 name, ActorId actor)
  302. {
  303. CE_ASSERT(m_num_actors < CE_MAX_ACTOR_COMPONENTS, "Max actor number reached");
  304. add_component(name, actor, m_num_actors, m_actors);
  305. }
  306. void Unit::add_material(StringId32 name, MaterialId material)
  307. {
  308. CE_ASSERT(m_num_materials < CE_MAX_MATERIAL_COMPONENTS, "Max material number reached");
  309. add_component(name, material, m_num_materials, m_materials);
  310. }
  311. void Unit::set_controller(StringId32 name, ControllerId controller)
  312. {
  313. m_controller.name = name;
  314. m_controller.component = controller;
  315. }
  316. Camera* Unit::camera(const char* name)
  317. {
  318. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  319. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  320. return m_world.get_camera(cam);
  321. }
  322. Camera* Unit::camera(uint32_t i)
  323. {
  324. CameraId cam = find_component_by_index(i, m_num_cameras, m_cameras);
  325. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  326. return m_world.get_camera(cam);
  327. }
  328. Sprite* Unit::sprite(const char* name)
  329. {
  330. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  331. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  332. return m_world.render_world()->get_sprite(sprite);
  333. }
  334. Sprite* Unit::sprite(uint32_t i)
  335. {
  336. SpriteId sprite = find_component_by_index(i, m_num_sprites, m_sprites);
  337. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  338. return m_world.render_world()->get_sprite(sprite);
  339. }
  340. Actor* Unit::actor(const char* name)
  341. {
  342. ActorId actor = find_component(name, m_num_actors, m_actors);
  343. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  344. return m_world.physics_world()->get_actor(actor);
  345. }
  346. Actor* Unit::actor(uint32_t i)
  347. {
  348. ActorId actor = find_component_by_index(i, m_num_actors, m_actors);
  349. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with index '%d'", i);
  350. return m_world.physics_world()->get_actor(actor);
  351. }
  352. Actor* Unit::actor_by_index(StringId32 name)
  353. {
  354. ActorId actor = find_component_by_name(name, m_num_actors, m_actors);
  355. // CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", name);
  356. return m_world.physics_world()->get_actor(actor);
  357. }
  358. Controller* Unit::controller()
  359. {
  360. if (m_controller.component.id != INVALID_ID)
  361. {
  362. return m_world.physics_world()->get_controller(m_controller.component);
  363. }
  364. return NULL;
  365. }
  366. Material* Unit::material(const char* name)
  367. {
  368. MaterialId material = find_component(name, m_num_materials, m_materials);
  369. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%s'", name);
  370. return material_manager::get()->lookup_material(material);
  371. }
  372. Material* Unit::material(uint32_t i)
  373. {
  374. MaterialId material = find_component_by_index(i, m_num_materials, m_materials);
  375. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%d'", i);
  376. return material_manager::get()->lookup_material(material);
  377. }
  378. bool Unit::is_a(const char* name)
  379. {
  380. return m_resource_id == ResourceId("unit", name).name;
  381. }
  382. void Unit::play_sprite_animation(const char* name, bool loop)
  383. {
  384. if (m_sprite_animation)
  385. m_sprite_animation->play(murmur2_32(name, strlen(name), 0), loop);
  386. }
  387. void Unit::stop_sprite_animation()
  388. {
  389. if (m_sprite_animation)
  390. m_sprite_animation->stop();
  391. }
  392. bool Unit::has_key(const char* k) const
  393. {
  394. using namespace unit_resource;
  395. return unit_resource::has_key(m_resource, k);
  396. }
  397. ValueType::Enum Unit::value_type(const char* k)
  398. {
  399. using namespace unit_resource;
  400. Key key;
  401. unit_resource::get_key(m_resource, k, key);
  402. return (ValueType::Enum) key.type;
  403. }
  404. bool Unit::get_key(const char* k, bool& v) const
  405. {
  406. using namespace unit_resource;
  407. Key key;
  408. bool has = unit_resource::get_key(m_resource, k, key);
  409. v = *(uint32_t*)(m_values + key.offset);
  410. return has;
  411. }
  412. bool Unit::get_key(const char* k, float& v) const
  413. {
  414. using namespace unit_resource;
  415. Key key;
  416. bool has = unit_resource::get_key(m_resource, k, key);
  417. v = *(float*)(m_values + key.offset);
  418. return has;
  419. }
  420. bool Unit::get_key(const char* k, StringId32& v) const
  421. {
  422. using namespace unit_resource;
  423. Key key;
  424. bool has = unit_resource::get_key(m_resource, k, key);
  425. v = *(StringId32*)(m_values + key.offset);
  426. return has;
  427. }
  428. bool Unit::get_key(const char* k, Vector3& v) const
  429. {
  430. using namespace unit_resource;
  431. Key key;
  432. bool has = unit_resource::get_key(m_resource, k, key);
  433. v = *(Vector3*)(m_values + key.offset);
  434. return has;
  435. }
  436. void Unit::set_key(const char* k, bool v)
  437. {
  438. using namespace unit_resource;
  439. Key key;
  440. unit_resource::get_key(m_resource, k, key);
  441. *(uint32_t*)(m_values + key.offset) = v;
  442. }
  443. void Unit::set_key(const char* k, float v)
  444. {
  445. using namespace unit_resource;
  446. Key key;
  447. unit_resource::get_key(m_resource, k, key);
  448. *(float*)(m_values + key.offset) = v;
  449. }
  450. void Unit::set_key(const char* k, const char* v)
  451. {
  452. using namespace unit_resource;
  453. Key key;
  454. unit_resource::get_key(m_resource, k, key);
  455. *(StringId32*)(m_values + key.offset) = murmur2_32(v, strlen(v));
  456. }
  457. void Unit::set_key(const char* k, const Vector3& v)
  458. {
  459. using namespace unit_resource;
  460. Key key;
  461. unit_resource::get_key(m_resource, k, key);
  462. *(Vector3*)(m_values + key.offset) = v;
  463. }
  464. } // namespace crown