scene.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // scene.cpp
  3. // engine
  4. //
  5. // Created by Sam Jaffe on 9/2/16.
  6. //
  7. #include "game/engine/scene.hpp"
  8. #include "game/engine/entity.hpp"
  9. #include "game/engine/events.hpp"
  10. #include "game/engine/game_dispatch.hpp"
  11. #include "game/graphics/renderer.hpp"
  12. #include "game/math/common.hpp"
  13. #include "game/math/compare.hpp"
  14. using namespace engine;
  15. scene::scene(std::string const & str, std::shared_ptr<game_dispatch> dispatch)
  16. : identity<scene, std::string>(str), dispatch_(dispatch) {}
  17. scene::~scene() {}
  18. bool scene::in_bounds(math::dim2::point p) const {
  19. return math::between(p[0], 0.f, local_scene_dimension_[0]) &&
  20. math::between(p[1], 0.f, local_scene_dimension_[1]);
  21. }
  22. bool scene::in_bounds(collidable * c) const {
  23. auto & quad = c->render_info().points;
  24. return in_bounds(quad.ll) || in_bounds(quad.lr) || in_bounds(quad.ur) ||
  25. in_bounds(quad.ul);
  26. }
  27. void scene::check_collisions(std::vector<collidable *> const & from,
  28. std::vector<collidable *> const & to) {
  29. for (auto & ent : from) {
  30. for (auto & hit : to) {
  31. if (math::intersects(ent->render_info().points,
  32. hit->render_info().points)) {
  33. ent->collide(*hit);
  34. }
  35. }
  36. }
  37. }
  38. void scene::check_collisions() {
  39. for (auto & pair : colliders) {
  40. check_collisions(pair.second, collidables[pair.first]);
  41. }
  42. }
  43. // void scene::update(float delta) {
  44. // for (auto & ent : entities) {
  45. // ent.update(delta);
  46. // }
  47. // check_collisions();
  48. //}
  49. //
  50. // void scene::render(graphics::renderer & renderer) {
  51. // for (auto & ent : entities) {
  52. // renderer.draw(ent.render_info());
  53. // }
  54. //}
  55. graphics::manager const & scene::graphics_manager() const {
  56. return dispatch_.lock()->graphics_manager();
  57. }
  58. void scene::handle_key_event(event::key_event evt) {
  59. if (evt.type & event::PRESSED_MASK && evt.key == keys::QUIT) {
  60. dispatch_.lock()->quit();
  61. }
  62. }
  63. void scene::handle_mouse_event(event::mouse_event evt) {}
  64. math::vec2 scene::size() const { return local_scene_dimension_; }
  65. key_binding const & scene::keys() const { return keys_; }