world.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include <new>
  6. #include "assert.h"
  7. #include "world.h"
  8. #include "device.h"
  9. #include "resource_manager.h"
  10. #include "debug_line.h"
  11. #include "actor.h"
  12. #include "lua_environment.h"
  13. #include "level_resource.h"
  14. #include "memory.h"
  15. #include "matrix4x4.h"
  16. namespace crown
  17. {
  18. World::World()
  19. : m_unit_pool(default_allocator(), CE_MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
  20. , m_camera_pool(default_allocator(), CE_MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
  21. , m_physics_world(*this)
  22. , m_events(default_allocator())
  23. {
  24. m_id.id = INVALID_ID;
  25. m_sound_world = SoundWorld::create(default_allocator());
  26. }
  27. World::~World()
  28. {
  29. // Destroy all units
  30. for (uint32_t i = 0; i < id_array::size(m_units); i++)
  31. {
  32. CE_DELETE(m_unit_pool, m_units[i]);
  33. }
  34. SoundWorld::destroy(default_allocator(), m_sound_world);
  35. }
  36. WorldId World::id() const
  37. {
  38. return m_id;
  39. }
  40. void World::set_id(WorldId id)
  41. {
  42. m_id = id;
  43. }
  44. UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
  45. {
  46. const ResourceId id(UNIT_EXTENSION, name);
  47. return spawn_unit(id.name, pos, rot);
  48. }
  49. UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot)
  50. {
  51. UnitResource* ur = (UnitResource*)device()->resource_manager()->get(UNIT_TYPE, name);
  52. Unit* u = (Unit*) m_unit_pool.allocate(sizeof(Unit), CE_ALIGNOF(Unit));
  53. const UnitId unit_id = id_array::create(m_units, u);
  54. new (u) Unit(*this, unit_id, name, ur, Matrix4x4(rot, pos));
  55. post_unit_spawned_event(unit_id);
  56. return unit_id;
  57. }
  58. void World::destroy_unit(UnitId id)
  59. {
  60. CE_DELETE(m_unit_pool, id_array::get(m_units, id));
  61. id_array::destroy(m_units, id);
  62. post_unit_destroyed_event(id);
  63. }
  64. void World::reload_units(UnitResource* old_ur, UnitResource* new_ur)
  65. {
  66. for (uint32_t i = 0; i < id_array::size(m_units); i++)
  67. {
  68. if (m_units[i]->resource() == old_ur)
  69. {
  70. m_units[i]->reload(new_ur);
  71. }
  72. }
  73. }
  74. uint32_t World::num_units() const
  75. {
  76. return id_array::size(m_units);
  77. }
  78. void World::units(Array<UnitId>& units) const
  79. {
  80. for (uint32_t i = 0; i < id_array::size(m_units); i++)
  81. {
  82. array::push_back(units, m_units[i]->id());
  83. }
  84. }
  85. void World::link_unit(UnitId child, UnitId parent, int32_t node)
  86. {
  87. Unit* parent_unit = get_unit(parent);
  88. parent_unit->link_node(0, node);
  89. }
  90. void World::unlink_unit(UnitId /*child*/)
  91. {
  92. }
  93. Unit* World::get_unit(UnitId id)
  94. {
  95. return id_array::get(m_units, id);
  96. }
  97. Camera* World::get_camera(CameraId id)
  98. {
  99. return id_array::get(m_cameras, id);
  100. }
  101. void World::update_animations(float dt)
  102. {
  103. m_sprite_animation_player.update(dt);
  104. }
  105. void World::update_scene(float dt)
  106. {
  107. m_physics_world.update(dt);
  108. m_scenegraph_manager.update();
  109. for (uint32_t i = 0; i < id_array::size(m_units); i++)
  110. {
  111. m_units[i]->update();
  112. }
  113. m_sound_world->update();
  114. process_physics_events();
  115. }
  116. void World::update(float dt)
  117. {
  118. update_animations(dt);
  119. update_scene(dt);
  120. }
  121. void World::render(Camera* camera)
  122. {
  123. m_render_world.update(camera->view_matrix(), camera->projection_matrix(), camera->_view_x, camera->_view_y,
  124. camera->_view_width, camera->_view_height, device()->last_delta_time());
  125. m_physics_world.draw_debug();
  126. }
  127. CameraId World::create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
  128. {
  129. Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node, type, near, far);
  130. return id_array::create(m_cameras, camera);
  131. }
  132. void World::destroy_camera(CameraId id)
  133. {
  134. CE_DELETE(m_camera_pool, id_array::get(m_cameras, id));
  135. id_array::destroy(m_cameras, id);
  136. }
  137. SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
  138. {
  139. ResourceId id(SOUND_EXTENSION, name);
  140. return play_sound(id.name, loop, volume, pos, range);
  141. }
  142. SoundInstanceId World::play_sound(StringId64 name, const bool loop, const float volume, const Vector3& pos, const float range)
  143. {
  144. SoundResource* sr = (SoundResource*)device()->resource_manager()->get(SOUND_TYPE, name);
  145. return m_sound_world->play(sr, loop, volume, pos);
  146. }
  147. void World::stop_sound(SoundInstanceId id)
  148. {
  149. m_sound_world->stop(id);
  150. }
  151. void World::link_sound(SoundInstanceId id, Unit* unit, int32_t node)
  152. {
  153. }
  154. void World::set_listener_pose(const Matrix4x4& pose)
  155. {
  156. m_sound_world->set_listener_pose(pose);
  157. }
  158. void World::set_sound_position(SoundInstanceId id, const Vector3& pos)
  159. {
  160. m_sound_world->set_sound_positions(1, &id, &pos);
  161. }
  162. void World::set_sound_range(SoundInstanceId id, float range)
  163. {
  164. m_sound_world->set_sound_ranges(1, &id, &range);
  165. }
  166. void World::set_sound_volume(SoundInstanceId id, float vol)
  167. {
  168. m_sound_world->set_sound_volumes(1, &id, &vol);
  169. }
  170. GuiId World::create_window_gui(uint16_t width, uint16_t height, const char* material)
  171. {
  172. return m_render_world.create_gui(width, height, material);
  173. }
  174. void World::destroy_gui(GuiId id)
  175. {
  176. m_render_world.destroy_gui(id);
  177. }
  178. Gui* World::get_gui(GuiId id)
  179. {
  180. return m_render_world.get_gui(id);
  181. }
  182. DebugLine* World::create_debug_line(bool depth_test)
  183. {
  184. return CE_NEW(default_allocator(), DebugLine)(depth_test);
  185. }
  186. void World::destroy_debug_line(DebugLine* line)
  187. {
  188. CE_DELETE(default_allocator(), line);
  189. }
  190. void World::load_level(const char* name)
  191. {
  192. const LevelResource* lr = (LevelResource*) device()->resource_manager()->get(LEVEL_EXTENSION, name);
  193. load_level(lr);
  194. }
  195. void World::load_level(const LevelResource* lr)
  196. {
  197. using namespace level_resource;
  198. uint32_t num = level_resource::num_units(lr);
  199. for (uint32_t i = 0; i < num; i++)
  200. {
  201. const LevelUnit* lu = level_resource::get_unit(lr, i);
  202. spawn_unit(lu->name, lu->position, lu->rotation);
  203. }
  204. num = level_resource::num_sounds(lr);
  205. for (uint32_t i = 0; i < num; i++)
  206. {
  207. const LevelSound* ls = level_resource::get_sound(lr, i);
  208. play_sound(ls->name, ls->loop, ls->volume, ls->position, ls->range);
  209. }
  210. post_level_loaded_event();
  211. }
  212. SceneGraphManager* World::scene_graph_manager()
  213. {
  214. return &m_scenegraph_manager;
  215. }
  216. SpriteAnimationPlayer* World::sprite_animation_player()
  217. {
  218. return &m_sprite_animation_player;
  219. }
  220. RenderWorld* World::render_world()
  221. {
  222. return &m_render_world;
  223. }
  224. PhysicsWorld* World::physics_world()
  225. {
  226. return &m_physics_world;
  227. }
  228. SoundWorld* World::sound_world()
  229. {
  230. return m_sound_world;
  231. }
  232. void World::post_unit_spawned_event(UnitId id)
  233. {
  234. UnitSpawnedEvent ev;
  235. ev.unit = id;
  236. event_stream::write(m_events, EventType::UNIT_SPAWNED, ev);
  237. }
  238. void World::post_unit_destroyed_event(UnitId id)
  239. {
  240. UnitDestroyedEvent ev;
  241. ev.unit = id;
  242. event_stream::write(m_events, EventType::UNIT_DESTROYED, ev);
  243. }
  244. void World::post_level_loaded_event()
  245. {
  246. LevelLoadedEvent ev;
  247. event_stream::write(m_events, EventType::LEVEL_LOADED, ev);
  248. }
  249. void World::process_physics_events()
  250. {
  251. EventStream& events = m_physics_world.events();
  252. // Read all events
  253. const char* ee = array::begin(events);
  254. while (ee != array::end(events))
  255. {
  256. event_stream::Header h = *(event_stream::Header*) ee;
  257. // CE_LOGD("=== PHYSICS EVENT ===");
  258. // CE_LOGD("type = %d", h.type);
  259. // CE_LOGD("size = %d", h.size);
  260. const char* event = ee + sizeof(event_stream::Header);
  261. switch (h.type)
  262. {
  263. case physics_world::EventType::COLLISION:
  264. {
  265. physics_world::CollisionEvent coll_ev = *(physics_world::CollisionEvent*) event;
  266. // CE_LOGD("type = %s", coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH ? "begin" : "end");
  267. // CE_LOGD("actor_0 = (%p)", coll_ev.actors[0]);
  268. // CE_LOGD("actor_1 = (%p)", coll_ev.actors[1]);
  269. // CE_LOGD("unit_0 = (%p)", coll_ev.actors[0]->unit());
  270. // CE_LOGD("unit_1 = (%p)", coll_ev.actors[1]->unit());
  271. // CE_LOGD("where = (%f %f %f)", coll_ev.where.x, coll_ev.where.y, coll_ev.where.z);
  272. // CE_LOGD("normal = (%f %f %f)", coll_ev.normal.x, coll_ev.normal.y, coll_ev.normal.z);
  273. device()->lua_environment()->call_physics_callback(
  274. coll_ev.actors[0],
  275. coll_ev.actors[1],
  276. (id_array::has(m_units, coll_ev.actors[0]->unit_id())) ? coll_ev.actors[0]->unit() : NULL,
  277. (id_array::has(m_units, coll_ev.actors[1]->unit_id())) ? coll_ev.actors[1]->unit() : NULL,
  278. coll_ev.where,
  279. coll_ev.normal,
  280. (coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH) ? "begin" : "end");
  281. break;
  282. }
  283. case physics_world::EventType::TRIGGER:
  284. {
  285. physics_world::TriggerEvent trigg_ev = *(physics_world::TriggerEvent*) event;
  286. // CE_LOGD("type = %s", trigg_ev.type == physics_world::TriggerEvent::BEGIN_TOUCH ? "begin" : "end");
  287. // CE_LOGD("trigger = (%p)", trigg_ev.trigger);
  288. // CE_LOGD("other = (%p)", trigg_ev.other);
  289. device()->lua_environment()->call_trigger_callback(
  290. trigg_ev.trigger,
  291. trigg_ev.other,
  292. (trigg_ev.type == physics_world::TriggerEvent::BEGIN_TOUCH ? "begin" : "end"));
  293. break;
  294. }
  295. default:
  296. {
  297. CE_FATAL("Unknown Physics event");
  298. break;
  299. }
  300. }
  301. // CE_LOGD("=====================");
  302. // Next event
  303. ee += sizeof(event_stream::Header) + h.size;
  304. }
  305. array::clear(events);
  306. }
  307. } // namespace crown