unit.cpp 13 KB

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