world.cpp 9.8 KB

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