scene_test.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // scene_test.cxx
  3. // engine-test
  4. //
  5. // Created by Sam Jaffe on 7/12/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include <gmock/gmock.h>
  9. #include "game/engine/entity.hpp"
  10. #include "game/engine/game_dispatch.hpp"
  11. #include "game/engine/scene.hpp"
  12. #include "mock_renderer.h"
  13. namespace env {
  14. void resize_screen(math::vec2i const & size);
  15. }
  16. struct test_scene : engine::scene {
  17. using engine::scene::scene;
  18. void update(float) override { check_collisions(); }
  19. void render() override {}
  20. void handle_key_event(engine::event::key_event) override {}
  21. void handle_mouse_event(engine::event::mouse_event) override {}
  22. void add_with(engine::collision_t t, engine::collidable & c) {
  23. colliders[t].push_back(&c);
  24. }
  25. void add_as(engine::collision_t t, engine::collidable & c) {
  26. collidables[t].push_back(&c);
  27. }
  28. };
  29. struct mock_collidable : engine::collidable {
  30. mock_collidable(math::dim2::rectangle bounds)
  31. : engine::collidable({bounds, bounds, cast<graphics::material>(1), {}}) {}
  32. MOCK_METHOD1(collide, void(engine::collidable const &));
  33. };
  34. class SceneTest : public testing::Test {
  35. protected:
  36. void SetUp() override;
  37. void TearDown() override;
  38. test_scene & scene() const { return *scene_; }
  39. engine::game_dispatch & game() const { return *dispatch_; }
  40. std::shared_ptr<stub_renderer> renderer_;
  41. std::shared_ptr<engine::game_dispatch> dispatch_;
  42. std::shared_ptr<test_scene> scene_;
  43. };
  44. void SceneTest::SetUp() {
  45. renderer_.reset(new stub_renderer);
  46. math::vec2 screen{{1600.f, 900.f}};
  47. env::resize_screen(math::vec2i(screen));
  48. dispatch_.reset(new engine::game_dispatch(renderer_));
  49. scene_.reset(new test_scene("test", screen, dispatch_));
  50. }
  51. void SceneTest::TearDown() {
  52. scene_.reset();
  53. dispatch_.reset();
  54. renderer_.reset();
  55. }
  56. using testing::Ref;
  57. TEST_F(SceneTest, WillCollideOverlappingObjects) {
  58. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  59. mock_collidable one{bnds}, two{bnds};
  60. scene().add_with(1, one);
  61. scene().add_as(1, two);
  62. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  63. scene().update(0.f);
  64. }
  65. TEST_F(SceneTest, NonOverlappingDontCollide) {
  66. mock_collidable one{{{{0.f, 0.f}}, {{1.f, 1.f}}}};
  67. mock_collidable two{{{{2.f, 2.f}}, {{1.f, 1.f}}}};
  68. scene().add_with(1, one);
  69. scene().add_as(1, two);
  70. EXPECT_CALL(one, collide(Ref(two))).Times(0);
  71. scene().update(0.f);
  72. }
  73. TEST_F(SceneTest, CollisionIsOneWay) {
  74. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  75. mock_collidable one{bnds}, two{bnds};
  76. scene().add_with(1, one);
  77. scene().add_as(1, two);
  78. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  79. EXPECT_CALL(two, collide(Ref(one))).Times(0);
  80. scene().update(0.f);
  81. }
  82. TEST_F(SceneTest, ObjectCannotCollideSelf) {
  83. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  84. mock_collidable one{bnds};
  85. scene().add_with(1, one);
  86. scene().add_as(1, one);
  87. EXPECT_CALL(one, collide(Ref(one))).Times(0);
  88. scene().update(0.f);
  89. }
  90. TEST_F(SceneTest, CollisionMatchesAsWithKeys) {
  91. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  92. mock_collidable one{bnds}, two{bnds}, three{bnds};
  93. scene().add_with(2, one);
  94. scene().add_as(1, two);
  95. scene().add_as(2, three);
  96. EXPECT_CALL(one, collide(Ref(two))).Times(0);
  97. EXPECT_CALL(one, collide(Ref(three))).Times(1);
  98. scene().update(0.f);
  99. }
  100. TEST_F(SceneTest, ObjectCanBeInMultipleCollidesWithGroups) {
  101. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  102. mock_collidable one{bnds}, two{bnds}, three{bnds};
  103. scene().add_with(1, one);
  104. scene().add_with(2, one);
  105. scene().add_as(1, two);
  106. scene().add_as(2, three);
  107. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  108. EXPECT_CALL(one, collide(Ref(three))).Times(1);
  109. scene().update(0.f);
  110. }
  111. TEST_F(SceneTest, ObjectCanBeInMultipleCollidableAsGroups) {
  112. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  113. mock_collidable one{bnds}, two{bnds}, three{bnds};
  114. scene().add_with(1, one);
  115. scene().add_with(2, three);
  116. scene().add_as(1, two);
  117. scene().add_as(2, two);
  118. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  119. EXPECT_CALL(three, collide(Ref(two))).Times(1);
  120. scene().update(0.f);
  121. }
  122. TEST_F(SceneTest, DoubleCollisionCanOccur) {
  123. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  124. mock_collidable one{bnds}, two{bnds};
  125. scene().add_with(1, one);
  126. scene().add_with(2, one);
  127. scene().add_as(1, two);
  128. scene().add_as(2, two);
  129. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  130. scene().update(0.f);
  131. }