scene_test.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. bool in_bounds(math::dim2::point c) const {
  23. return engine::scene::in_bounds(c);
  24. }
  25. bool in_bounds(engine::collidable * c) const {
  26. return engine::scene::in_bounds(c);
  27. }
  28. graphics::manager const & graphics_manager() const {
  29. return engine::scene::graphics_manager();
  30. }
  31. void add_with(engine::collision_t t, engine::collidable & c) {
  32. colliders[t].push_back(&c);
  33. }
  34. void add_as(engine::collision_t t, engine::collidable & c) {
  35. collidables[t].push_back(&c);
  36. }
  37. };
  38. struct mock_collidable : engine::collidable {
  39. mock_collidable(math::dim2::rectangle bounds)
  40. : engine::collidable({bounds, bounds, cast<graphics::material>(1), {}}) {}
  41. MOCK_METHOD1(collide, void(engine::collidable const &));
  42. };
  43. class SceneTest : public testing::Test {
  44. protected:
  45. void SetUp() override;
  46. void TearDown() override;
  47. test_scene & scene() const { return *scene_; }
  48. engine::game_dispatch & game() const { return *dispatch_; }
  49. std::shared_ptr<stub_renderer> renderer_;
  50. std::shared_ptr<engine::game_dispatch> dispatch_;
  51. std::shared_ptr<test_scene> scene_;
  52. };
  53. void SceneTest::SetUp() {
  54. renderer_.reset(new stub_renderer);
  55. math::vec2 screen{{1600.f, 900.f}};
  56. env::resize_screen(math::vec2i(screen));
  57. dispatch_.reset(new engine::game_dispatch(renderer_));
  58. scene_.reset(new test_scene("test", screen, dispatch_));
  59. }
  60. void SceneTest::TearDown() {
  61. scene_.reset();
  62. dispatch_.reset();
  63. renderer_.reset();
  64. }
  65. using testing::Eq;
  66. using testing::Ref;
  67. TEST_F(SceneTest, SharedGraphicsManagerWithDispatch) {
  68. EXPECT_THAT(&scene().graphics_manager(), &game().graphics_manager());
  69. }
  70. TEST_F(SceneTest, WillCollideOverlappingObjects) {
  71. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  72. mock_collidable one{bnds}, two{bnds};
  73. scene().add_with(1, one);
  74. scene().add_as(1, two);
  75. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  76. scene().update(0.f);
  77. }
  78. TEST_F(SceneTest, NonOverlappingDontCollide) {
  79. mock_collidable one{{{{0.f, 0.f}}, {{1.f, 1.f}}}};
  80. mock_collidable two{{{{2.f, 2.f}}, {{1.f, 1.f}}}};
  81. scene().add_with(1, one);
  82. scene().add_as(1, two);
  83. EXPECT_CALL(one, collide(Ref(two))).Times(0);
  84. scene().update(0.f);
  85. }
  86. TEST_F(SceneTest, CollisionIsOneWay) {
  87. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  88. mock_collidable one{bnds}, two{bnds};
  89. scene().add_with(1, one);
  90. scene().add_as(1, two);
  91. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  92. EXPECT_CALL(two, collide(Ref(one))).Times(0);
  93. scene().update(0.f);
  94. }
  95. TEST_F(SceneTest, ObjectCannotCollideSelf) {
  96. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  97. mock_collidable one{bnds};
  98. scene().add_with(1, one);
  99. scene().add_as(1, one);
  100. EXPECT_CALL(one, collide(Ref(one))).Times(0);
  101. scene().update(0.f);
  102. }
  103. TEST_F(SceneTest, CollisionMatchesAsWithKeys) {
  104. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  105. mock_collidable one{bnds}, two{bnds}, three{bnds};
  106. scene().add_with(2, one);
  107. scene().add_as(1, two);
  108. scene().add_as(2, three);
  109. EXPECT_CALL(one, collide(Ref(two))).Times(0);
  110. EXPECT_CALL(one, collide(Ref(three))).Times(1);
  111. scene().update(0.f);
  112. }
  113. TEST_F(SceneTest, ObjectCanBeInMultipleCollidesWithGroups) {
  114. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  115. mock_collidable one{bnds}, two{bnds}, three{bnds};
  116. scene().add_with(1, one);
  117. scene().add_with(2, one);
  118. scene().add_as(1, two);
  119. scene().add_as(2, three);
  120. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  121. EXPECT_CALL(one, collide(Ref(three))).Times(1);
  122. scene().update(0.f);
  123. }
  124. TEST_F(SceneTest, ObjectCanBeInMultipleCollidableAsGroups) {
  125. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  126. mock_collidable one{bnds}, two{bnds}, three{bnds};
  127. scene().add_with(1, one);
  128. scene().add_with(2, three);
  129. scene().add_as(1, two);
  130. scene().add_as(2, two);
  131. EXPECT_CALL(one, collide(Ref(two))).Times(1);
  132. EXPECT_CALL(three, collide(Ref(two))).Times(1);
  133. scene().update(0.f);
  134. }
  135. // TODO: Solve double-collision bug?
  136. TEST_F(SceneTest, DoubleCollisionCanOccur) {
  137. math::dim2::rectangle bnds{{{0.f, 0.f}}, {{1.f, 1.f}}};
  138. mock_collidable one{bnds}, two{bnds};
  139. scene().add_with(1, one);
  140. scene().add_with(2, one);
  141. scene().add_as(1, two);
  142. scene().add_as(2, two);
  143. EXPECT_CALL(one, collide(Ref(two))).Times(2);
  144. scene().update(0.f);
  145. }
  146. struct BoundsTest : SceneTest, testing::WithParamInterface<math::vec2> {};
  147. TEST_F(BoundsTest, BoundsCheckIsPointInScreenZone) {
  148. // Confirm that the size is as expected...
  149. EXPECT_THAT(scene().size(), Eq(make_vector(1600.f, 900.f)));
  150. // Lower-Left Corner
  151. EXPECT_FALSE(scene().in_bounds(make_vector(-1.f, 0.f)));
  152. EXPECT_FALSE(scene().in_bounds(make_vector(0.f, -1.f)));
  153. EXPECT_TRUE(scene().in_bounds(make_vector(0.f, 0.f)));
  154. // Lower-Right Corner
  155. EXPECT_FALSE(scene().in_bounds(make_vector(1600.f, -1.f)));
  156. EXPECT_FALSE(scene().in_bounds(make_vector(1601.f, 0.f)));
  157. EXPECT_TRUE(scene().in_bounds(make_vector(1600.f, 0.f)));
  158. // Upper-Right Corner
  159. EXPECT_FALSE(scene().in_bounds(make_vector(1601.f, 900.f)));
  160. EXPECT_FALSE(scene().in_bounds(make_vector(1600.f, 901.f)));
  161. EXPECT_TRUE(scene().in_bounds(make_vector(1600.f, 900.f)));
  162. // Upper-Left Corner
  163. EXPECT_FALSE(scene().in_bounds(make_vector(0.f, 901.f)));
  164. EXPECT_FALSE(scene().in_bounds(make_vector(-1.f, 900.f)));
  165. EXPECT_TRUE(scene().in_bounds(make_vector(0.f, 900.f)));
  166. }
  167. TEST_P(BoundsTest, BoundsCheckOnCollidableIsAnyPointInBounds) {
  168. math::dim2::square bounds = {GetParam(), 2.f};
  169. engine::collidable collide{{{}, bounds, cast<graphics::material>(1), {}}};
  170. EXPECT_TRUE(scene().in_bounds(&collide));
  171. }
  172. INSTANTIATE_TEST_CASE_P(Collidable, BoundsTest,
  173. ::testing::Values(make_vector(-1.f, -1.f),
  174. make_vector(1599.f, -1.f),
  175. make_vector(1599.f, 899.f),
  176. make_vector(-1.f, 899.f)));