unit.cpp 13 KB

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