scene_test.cxx 6.2 KB

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