diff --git a/client/in_world.cpp b/client/in_world.cpp index 8063de6..01a8ed0 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -107,8 +107,8 @@ void InWorld::Update(double delta) { //update the camera if(localCharacter) { - camera.x = localCharacter->position.x - camera.marginX; - camera.y = localCharacter->position.y - camera.marginY; + camera.x = localCharacter->origin.x - camera.marginX; + camera.y = localCharacter->origin.y - camera.marginY; } //check the map @@ -134,7 +134,7 @@ void InWorld::Render(SDL_Surface* const screen) { //draw characters for (auto& it : characterMap) { - //TODO: drawing order according to Y position + //TODO: drawing order according to Y origin it.second.DrawTo(screen, camera.x, camera.y); } @@ -291,7 +291,7 @@ void InWorld::HandleCharacterUpdate(SerialPacket packet) { //update only if the message didn't originate from here if (packet.characterInfo.clientIndex != clientIndex) { - characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position; + characterMap[packet.characterInfo.characterIndex].origin = packet.characterInfo.origin; characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion; } characterMap[packet.characterInfo.characterIndex].CorrectSprite(); @@ -310,7 +310,7 @@ void InWorld::HandleCharacterNew(SerialPacket packet) { character.avatar = packet.characterInfo.avatar; character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4); character.mapIndex = packet.characterInfo.mapIndex; - character.position = packet.characterInfo.position; + character.origin = packet.characterInfo.origin; character.motion = packet.characterInfo.motion; character.stats = packet.characterInfo.stats; @@ -366,7 +366,7 @@ void InWorld::SendPlayerUpdate() { packet.characterInfo.clientIndex = clientIndex; packet.characterInfo.accountIndex = accountIndex; packet.characterInfo.characterIndex = characterIndex; - packet.characterInfo.position = localCharacter->position; + packet.characterInfo.origin = localCharacter->origin; packet.characterInfo.motion = localCharacter->motion; network.SendTo(Channels::SERVER, &packet); diff --git a/common/gameplay/character_data.cpp b/common/gameplay/character_data.cpp index e86706c..9824c79 100644 --- a/common/gameplay/character_data.cpp +++ b/common/gameplay/character_data.cpp @@ -23,10 +23,10 @@ void CharacterData::Update(double delta) { if (motion.x && motion.y) { - position += motion * delta * CHARACTER_WALKING_MOD; + origin += motion * delta * CHARACTER_WALKING_MOD; } else if (motion != 0) { - position += motion * delta; + origin += motion * delta; } #ifdef GRAPHICS sprite.Update(delta); @@ -36,7 +36,7 @@ void CharacterData::Update(double delta) { #ifdef GRAPHICS void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) { - sprite.DrawTo(dest, position.x - camX, position.y - camY); + sprite.DrawTo(dest, origin.x - camX, origin.y - camY); } void CharacterData::CorrectSprite() { diff --git a/common/gameplay/character_data.hpp b/common/gameplay/character_data.hpp index 128fe16..9547d4e 100644 --- a/common/gameplay/character_data.hpp +++ b/common/gameplay/character_data.hpp @@ -22,8 +22,6 @@ #ifndef CHARACTERDATA_HPP_ #define CHARACTERDATA_HPP_ -//POD members -#include "bbox.hpp" #include "vector2.hpp" #include "statistics.hpp" @@ -48,7 +46,7 @@ struct CharacterData { //world position int mapIndex = 0; - Vector2 position = {0.0,0.0}; + Vector2 origin = {0.0,0.0}; Vector2 motion = {0.0,0.0}; //base statistics @@ -71,7 +69,7 @@ struct CharacterData { #ifdef GRAPHICS SpriteSheet sprite; #endif - BBox bbox = {0,0,0,0}; + Vector2 bounds = {0.0,0.0}; bool inCombat = false; int atbGauge = 0; //TODO: stored command diff --git a/common/gameplay/combat_data.hpp b/common/gameplay/combat_data.hpp index 23bf1f4..db87851 100644 --- a/common/gameplay/combat_data.hpp +++ b/common/gameplay/combat_data.hpp @@ -22,9 +22,7 @@ #ifndef COMBATDATA_HPP_ #define COMBATDATA_HPP_ -//POD members #include "vector2.hpp" -#include "bbox.hpp" //gameplay members #include "character_data.hpp" @@ -58,8 +56,8 @@ struct CombatData { //world interaction int mapIndex = 0; - Vector2 position = {0.0,0.0}; - BBox bbox = {0,0,0,0}; + Vector2 origin = {0.0,0.0}; + Vector2 bounds = {0.0,0.0}; //time interval Clock::time_point lastTick = Clock::now(); diff --git a/common/gameplay/enemy_data.hpp b/common/gameplay/enemy_data.hpp index d0edd0a..62de98c 100644 --- a/common/gameplay/enemy_data.hpp +++ b/common/gameplay/enemy_data.hpp @@ -22,7 +22,7 @@ #ifndef ENEMYDATA_HPP_ #define ENEMYDATA_HPP_ -//gameplay +#include "vector2.hpp" #include "statistics.hpp" //graphics @@ -50,6 +50,8 @@ struct EnemyData { //NOTE: these are lost when unloaded #ifdef GRAPHICS SpriteSheet sprite; + Vector2 origin = {0.0,0.0}; + Vector2 bounds = {0.0,0.0}; #endif int tableIndex; int atbGauge = 0; diff --git a/common/network/serial.cpp b/common/network/serial.cpp index 44c4bfd..961afd4 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -108,8 +108,8 @@ void serializeCombat(SerialPacket* packet, char* buffer) { //position SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->combatInfo.position.x, sizeof(double)); - SERIALIZE(buffer, &packet->combatInfo.position.y, sizeof(double)); + SERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); + SERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); //TODO: rewards } @@ -147,8 +147,8 @@ void serializeCharacter(SerialPacket* packet, char* buffer) { SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); //vectors - SERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); + SERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); + SERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); @@ -252,8 +252,8 @@ void deserializeCombat(SerialPacket* packet, char* buffer) { //position DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->combatInfo.position.x, sizeof(double)); - DESERIALIZE(buffer, &packet->combatInfo.position.y, sizeof(double)); + DESERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); + DESERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); //TODO: rewards } @@ -292,8 +292,8 @@ void deserializeCharacter(SerialPacket* packet, char* buffer) { DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); //vectors - DESERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); + DESERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); + DESERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index f157df1..540494b 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -150,7 +150,7 @@ union SerialPacket { int characterArray[COMBAT_MAX_CHARACTER_COUNT]; int enemyArray[COMBAT_MAX_ENEMY_COUNT]; int mapIndex; - Vector2 position; + Vector2 origin; //TODO: rewards }combatInfo; @@ -163,7 +163,7 @@ union SerialPacket { char handle[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE]; int mapIndex; - Vector2 position; + Vector2 origin; Vector2 motion; Statistics stats; }characterInfo; diff --git a/common/utilities/bbox.hpp b/common/utilities/bbox.hpp deleted file mode 100644 index 5b29944..0000000 --- a/common/utilities/bbox.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* 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 - -//TODO: This is supposed to interact with the vector -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/utilities/check_bounds.cpp b/common/utilities/check_bounds.cpp new file mode 100644 index 0000000..b0d42f8 --- /dev/null +++ b/common/utilities/check_bounds.cpp @@ -0,0 +1,40 @@ +/* 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 "check_bounds.hpp" + +bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point) { + return !( + point.x < origin.x || + point.y < origin.y || + point.x >= origin.x + bound.x || + point.y >= origin.y + bound.y + ); +} + +bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo) { + return !( + originOne.x >= originTwo.x + boundTwo.x || + originOne.x + boundOne.x >= originTwo.x || + originOne.y >= originTwo.y + boundTwo.y || + originOne.y + boundOne.y >= originTwo.y + ); +} diff --git a/common/utilities/check_bounds.hpp b/common/utilities/check_bounds.hpp new file mode 100644 index 0000000..e02d6cd --- /dev/null +++ b/common/utilities/check_bounds.hpp @@ -0,0 +1,30 @@ +/* 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 CHECKBOUNDS_HPP_ +#define CHECKBOUNDS_HPP_ + +#include "vector2.hpp" + +bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); +bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); + +#endif diff --git a/common/utilities/vector2.hpp b/common/utilities/vector2.hpp index a165018..1d8882d 100644 --- a/common/utilities/vector2.hpp +++ b/common/utilities/vector2.hpp @@ -42,12 +42,6 @@ public: return x*x+y*y; } - double operator[](size_t i) { - if (i >= 2) - throw(std::domain_error("Out of range")); - return *(&x+i); - } - //Arithmetic operators Vector2 operator+(Vector2 v) const { Vector2 ret; diff --git a/rsc/scripts/setup_server.sql b/rsc/scripts/setup_server.sql index 9ea5498..7d041bf 100644 --- a/rsc/scripts/setup_server.sql +++ b/rsc/scripts/setup_server.sql @@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS Characters ( --position mapIndex INTEGER DEFAULT 0, - positionX INTEGER DEFAULT 0, - positionY INTEGER DEFAULT 0, + originX INTEGER DEFAULT 0, + originY INTEGER DEFAULT 0, --statistics level INTEGER DEFAULT 0, diff --git a/server/character_management.cpp b/server/character_management.cpp index fe7de3e..4414e13 100644 --- a/server/character_management.cpp +++ b/server/character_management.cpp @@ -32,7 +32,7 @@ //TODO: save and load the statistics static const char* CREATE_CHARACTER = "INSERT INTO Characters (owner, handle, avatar) VALUES (?, ?, ?);"; static const char* LOAD_CHARACTER = "SELECT * FROM Characters WHERE handle = ?;"; -static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET mapIndex = ?2, positionX = ?3, positionY = ?4 WHERE uid = ?1;"; +static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET mapIndex = ?2, originX = ?3, originY = ?4 WHERE uid = ?1;"; static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;"; //------------------------- @@ -117,10 +117,10 @@ int ServerApplication::LoadCharacter(int owner, std::string handle, std::string newChar.avatar = reinterpret_cast(sqlite3_column_text(statement, 3)); //Don't cache the birth - //world position + //world origin newChar.mapIndex = sqlite3_column_int(statement, 5); - newChar.position.x = (double)sqlite3_column_int(statement, 6); - newChar.position.y = (double)sqlite3_column_int(statement, 7); + newChar.origin.x = (double)sqlite3_column_int(statement, 6); + newChar.origin.y = (double)sqlite3_column_int(statement, 7); //statistics newChar.stats.level = sqlite3_column_int(statement, 8); @@ -176,8 +176,8 @@ int ServerApplication::SaveCharacter(int uid) { bool ret = false; ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK; ret |= sqlite3_bind_int(statement, 2, character.mapIndex) != SQLITE_OK; - ret |= sqlite3_bind_int(statement, 3, (int)character.position.x) != SQLITE_OK; - ret |= sqlite3_bind_int(statement, 4, (int)character.position.y) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 3, (int)character.origin.x) != SQLITE_OK; + ret |= sqlite3_bind_int(statement, 4, (int)character.origin.y) != SQLITE_OK; //TODO: stats, etc. //check for binding errors diff --git a/server/server_connections.cpp b/server/server_connections.cpp index 11e99ce..6b7f296 100644 --- a/server/server_connections.cpp +++ b/server/server_connections.cpp @@ -75,7 +75,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { packet.characterInfo.characterIndex = characterIndex; strncpy(packet.characterInfo.handle, characterMap[characterIndex].handle.c_str(), PACKET_STRING_SIZE); strncpy(packet.characterInfo.avatar, characterMap[characterIndex].avatar.c_str(), PACKET_STRING_SIZE); - packet.characterInfo.position = characterMap[characterIndex].position; + packet.characterInfo.origin = characterMap[characterIndex].origin; packet.characterInfo.motion = characterMap[characterIndex].motion; PumpPacket(packet); @@ -99,7 +99,7 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) { snprintf(newPacket.characterInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str()); snprintf(newPacket.characterInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str()); newPacket.characterInfo.mapIndex = it.second.mapIndex; - newPacket.characterInfo.position = it.second.position; + newPacket.characterInfo.origin = it.second.origin; newPacket.characterInfo.motion = it.second.motion; newPacket.characterInfo.stats = it.second.stats; @@ -155,7 +155,7 @@ void ServerApplication::HandleCharacterUpdate(SerialPacket packet) { } //TODO: the server needs it's own movement system too - characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position; + characterMap[packet.characterInfo.characterIndex].origin = packet.characterInfo.origin; characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion; PumpPacket(packet);