|
|
@@ -0,0 +1,105 @@
|
|
|
+//
|
|
|
+// entity_test.cxx
|
|
|
+// engine-test
|
|
|
+//
|
|
|
+// Created by Sam Jaffe on 8/3/19.
|
|
|
+// Copyright © 2019 Sam Jaffe. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#include <sstream>
|
|
|
+
|
|
|
+#include <gmock/gmock.h>
|
|
|
+#include <json/json.h>
|
|
|
+
|
|
|
+#include "game/engine/entity.hpp"
|
|
|
+
|
|
|
+#include "mock_renderer.h"
|
|
|
+
|
|
|
+using namespace engine;
|
|
|
+
|
|
|
+using testing::Eq;
|
|
|
+using testing::Ne;
|
|
|
+
|
|
|
+namespace math { namespace dim2 {
|
|
|
+ bool operator==(rectangle const & lhs, rectangle const & rhs) {
|
|
|
+ return lhs.origin == rhs.origin && lhs.size == rhs.size;
|
|
|
+ }
|
|
|
+ bool operator!=(rectangle const & lhs, rectangle const & rhs) {
|
|
|
+ return !(lhs == rhs);
|
|
|
+ }
|
|
|
+ bool operator==(quad const & lhs, quad const & rhs) {
|
|
|
+ return lhs.ll == rhs.ll && lhs.lr == rhs.lr && lhs.ul == rhs.ul
|
|
|
+ && lhs.ur == rhs.ur;
|
|
|
+ }
|
|
|
+ bool operator!=(quad const & lhs, quad const & rhs) {
|
|
|
+ return !(lhs == rhs);
|
|
|
+ }
|
|
|
+}}
|
|
|
+
|
|
|
+inline Json::Value to_json(std::string const & str) {
|
|
|
+ Json::Value json;
|
|
|
+ std::stringstream(str) >> json;
|
|
|
+ return json;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(CollidableTest, ConstructsUsingGraphics) {
|
|
|
+ math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
|
|
|
+ graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
|
|
|
+
|
|
|
+ collidable collide{obj};
|
|
|
+ EXPECT_THAT(collide.render_info().location, Eq(obj.location));
|
|
|
+}
|
|
|
+
|
|
|
+std::string const data = R"(
|
|
|
+{
|
|
|
+ "velocity": [ 1.0, 2.0 ],
|
|
|
+ "size": 1.0
|
|
|
+}
|
|
|
+)";
|
|
|
+
|
|
|
+TEST(EntityTest, ConstructsFromJson) {
|
|
|
+ math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
|
|
|
+ graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
|
|
|
+
|
|
|
+ entity ent{to_json(data), obj};
|
|
|
+ EXPECT_THAT(ent.render_info().location, Eq(obj.location));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+TEST(EntityTest, SizeParamAltersLocation) {
|
|
|
+ math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
|
|
|
+ graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
|
|
|
+
|
|
|
+ Json::Value json = to_json(data);
|
|
|
+ json["size"] = 2.f;
|
|
|
+ entity ent{json, obj};
|
|
|
+ math::dim2::rectangle expected{make_vector(0.f, 0.f), make_vector(2.f, 2.f)};
|
|
|
+ EXPECT_THAT(ent.render_info().location, Ne(obj.location));
|
|
|
+ EXPECT_THAT(ent.render_info().location, Eq(expected));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(EntityTest, MoveWillAdjustPointsAndBounds) {
|
|
|
+ math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
|
|
|
+ graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
|
|
|
+
|
|
|
+ entity ent{to_json(data), obj};
|
|
|
+ ent.update(1.f);
|
|
|
+
|
|
|
+ math::dim2::rectangle expected{make_vector(1.f, 2.f), make_vector(1.f, 1.f)};
|
|
|
+ EXPECT_THAT(ent.render_info().location, Eq(expected));
|
|
|
+ EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(EntityTest, MoveIsAFunctionOfVelocity) {
|
|
|
+ math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
|
|
|
+ graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
|
|
|
+
|
|
|
+ entity ent{to_json(data), obj};
|
|
|
+ ent.update(0.5f);
|
|
|
+
|
|
|
+ math::dim2::rectangle expected{make_vector(0.5f, 1.f), make_vector(1.f, 1.f)};
|
|
|
+ EXPECT_THAT(ent.render_info().location, Eq(expected));
|
|
|
+ EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
|
|
|
+}
|
|
|
+
|
|
|
+// TODO: Test Acceleration and Angular-Velocity
|