From 0c6537fb3676437a16e31d1cc1e7decaecceb8c3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 13 Apr 2014 03:02:26 +1000 Subject: [PATCH] Began work on the server's generic entity system --- common/bbox.hpp | 74 +++++++++++++++++++++++++++++++++++ common/vector2.hpp | 5 ++- server/entity.cpp | 29 ++++++++++++++ server/entity.hpp | 47 ++++++++++++++++++++++ server/player_entity.hpp | 29 ++++++++++++++ server/server_application.cpp | 3 +- server/server_application.hpp | 4 ++ 7 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 common/bbox.hpp create mode 100644 server/entity.cpp create mode 100644 server/entity.hpp create mode 100644 server/player_entity.hpp diff --git a/common/bbox.hpp b/common/bbox.hpp new file mode 100644 index 0000000..ca08e3e --- /dev/null +++ b/common/bbox.hpp @@ -0,0 +1,74 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BBOX_HPP_ +#define BBOX_HPP_ + +#include +#include +#include + +class BBox { +public: + double x, y; + double w, h; + + BBox() = default; + BBox(double i, double j, double k, double l): x(i), y(j), w(k), h(l) {}; + ~BBox() = default; + BBox& operator=(BBox const&) = default; + + double Size() { + return std::max(w*h,0.0); + } + + bool IsCollision(BBox rhs) { + return not ( + x >= rhs.x + rhs.w || + y >= rhs.y + rhs.h || + rhs.x >= x + w || + rhs.y >= y + h + ); + } + + BBox Intersection(BBox rhs) { + if (!IsCollision(rhs)) { + return {0, 0, 0, 0}; + } + BBox ret; + ret.x = std::max(x, rhs.x); + ret.y = std::max(y, rhs.y); + ret.w = std::min(x+w, rhs.x+rhs.w) - ret.x; + ret.h = std::min(y+h, rhs.y+rhs.h) - ret.y; + return ret; + } + + double operator[](size_t i) { + if (i >= 4) + throw(std::domain_error("Out of range")); + return *(&x+i); + } +}; + +//This is explicitly a POD +static_assert(std::is_pod::value, "BBox is not a POD"); + +#endif \ No newline at end of file diff --git a/common/vector2.hpp b/common/vector2.hpp index a7a721d..c898c02 100644 --- a/common/vector2.hpp +++ b/common/vector2.hpp @@ -22,6 +22,7 @@ #ifndef VECTOR2_HPP_ #define VECTOR2_HPP_ +#include #include #include @@ -29,7 +30,6 @@ class Vector2 { public: double x, y; - //This is explicitly a POD Vector2() = default; Vector2(double i, double j): x(i), y(j) {}; ~Vector2() = default; @@ -112,4 +112,7 @@ template Vector2 operator/(T t, Vector2 v) { return v / t; } template bool operator==(T t, Vector2 v) { return v == t; } template bool operator!=(T t, Vector2 v) { return v != t; } +//This is explicitly a POD +static_assert(std::is_pod::value, "Vector2 is not a POD"); + #endif diff --git a/server/entity.cpp b/server/entity.cpp new file mode 100644 index 0000000..abb22ed --- /dev/null +++ b/server/entity.cpp @@ -0,0 +1,29 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "entity.hpp" + +#include + +//This is explicitly a POD +static_assert(std::is_pod::value, "Entity is not a POD"); + +unsigned int Entity::uidCounter; \ No newline at end of file diff --git a/server/entity.hpp b/server/entity.hpp new file mode 100644 index 0000000..e7308df --- /dev/null +++ b/server/entity.hpp @@ -0,0 +1,47 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef ENTITY_HPP_ +#define ENTITY_HPP_ + +//POD members +#include "vector2.hpp" +#include "bbox.hpp" + +struct Entity { + enum Type { + PLAYER, + PORTAL, + ITEMS, + CHEST, + DOOR, + }; + + Type type; + int mapIndex; + Vector2 position; + Vector2 motion; + BBox bbox; + unsigned int uid; + static unsigned int uidCounter; +}; + +#endif diff --git a/server/player_entity.hpp b/server/player_entity.hpp new file mode 100644 index 0000000..b60227e --- /dev/null +++ b/server/player_entity.hpp @@ -0,0 +1,29 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef PLAYERENTITY_HPP_ +#define PLAYERENTITY_HPP_ + +struct PlayerEntity { + //TODO: stuff +}; + +#endif diff --git a/server/server_application.cpp b/server/server_application.cpp index 890809d..25a53bc 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -53,7 +53,8 @@ void ServerApplication::Init(int argc, char** argv) { cout << "Beginning startup" << endl; int ret = 0; - //load config + //initial setup + Entity::uidCounter = 0; config.Load("rsc\\config.cfg"); //Init SDL diff --git a/server/server_application.hpp b/server/server_application.hpp index 97610cf..7c30067 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -22,6 +22,10 @@ #ifndef SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_ +//server specific stuff +#include "entity.hpp" +#include "player_entity.hpp" + //maps #include "map_generator.hpp" #include "map_file_format.hpp"