unit.cpp 11 KB

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