scene.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "game/util/env.hpp"
  15. using namespace engine;
  16. scene::scene(std::string const & str, math::vec2 const & bounds,
  17. std::shared_ptr<game_dispatch> dispatch)
  18. : identity<scene, std::string>(str), local_bounds_(bounds),
  19. renderer_(dispatch->make_renderer(size())), dispatch_(dispatch) {}
  20. scene::~scene() {}
  21. bool scene::in_bounds(math::dim2::point const & p) const {
  22. return math::between(p[0], 0.f, local_bounds_[0]) &&
  23. math::between(p[1], 0.f, local_bounds_[1]);
  24. }
  25. bool scene::in_bounds(collidable * c) const {
  26. auto & quad = c->render_info().points;
  27. return in_bounds(quad.ll) || in_bounds(quad.lr) || in_bounds(quad.ur) ||
  28. in_bounds(quad.ul);
  29. }
  30. void scene::check_collisions(std::vector<collidable *> const & from,
  31. std::vector<collidable *> const & to) {
  32. for (auto & ent : from) {
  33. for (auto & hit : to) {
  34. if (ent == hit) continue;
  35. if (math::intersects(ent->render_info().points,
  36. hit->render_info().points)) {
  37. ent->collide(*hit);
  38. }
  39. }
  40. }
  41. }
  42. void scene::check_collisions() {
  43. for (auto & pair : colliders) {
  44. check_collisions(pair.second, collidables[pair.first]);
  45. }
  46. }
  47. // void scene::update(float delta) {
  48. // for (auto & ent : entities) {
  49. // ent.update(delta);
  50. // }
  51. // check_collisions();
  52. //}
  53. //
  54. // void scene::render(graphics::renderer & renderer) {
  55. // for (auto & ent : entities) {
  56. // renderer.draw(ent.render_info());
  57. // }
  58. //}
  59. graphics::manager const & scene::graphics_manager() const {
  60. return dispatch_.lock()->graphics_manager();
  61. }
  62. void scene::handle_key_event(event::key_event evt) {
  63. // TODO (sjaffe): Handle quit binding correctly... (don't test this)
  64. if (evt.type & event::PRESSED_MASK && evt.key == keys::QUIT) {
  65. dispatch_.lock()->quit();
  66. }
  67. }
  68. void scene::handle_mouse_event(event::mouse_event evt) {}
  69. math::vec2 scene::size() const {
  70. return (local_bounds_ == math::vec2()) ? math::vec2(env::screen_size())
  71. : local_bounds_;
  72. }
  73. key_binding const & scene::keys() const { return keys_; }