world.cpp 9.4 KB

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