scene_test.cxx 6.4 KB

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