Removed BBox, renamed position to origin

I've replaced the BBox class with a pair of inline functions in
check_bounds.hpp. I've also renamed the 'position' variable to 'origin' in
several locations.

These changes are mostly to alleviate ambiguity.
This commit is contained in:
Kayne Ruse
2014-06-02 21:05:49 +10:00
parent d2f03b98dc
commit fb6fba9564
14 changed files with 107 additions and 120 deletions
+6 -6
View File
@@ -107,8 +107,8 @@ void InWorld::Update(double delta) {
//update the camera //update the camera
if(localCharacter) { if(localCharacter) {
camera.x = localCharacter->position.x - camera.marginX; camera.x = localCharacter->origin.x - camera.marginX;
camera.y = localCharacter->position.y - camera.marginY; camera.y = localCharacter->origin.y - camera.marginY;
} }
//check the map //check the map
@@ -134,7 +134,7 @@ void InWorld::Render(SDL_Surface* const screen) {
//draw characters //draw characters
for (auto& it : characterMap) { 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); 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 //update only if the message didn't originate from here
if (packet.characterInfo.clientIndex != clientIndex) { 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].motion = packet.characterInfo.motion;
} }
characterMap[packet.characterInfo.characterIndex].CorrectSprite(); characterMap[packet.characterInfo.characterIndex].CorrectSprite();
@@ -310,7 +310,7 @@ void InWorld::HandleCharacterNew(SerialPacket packet) {
character.avatar = packet.characterInfo.avatar; character.avatar = packet.characterInfo.avatar;
character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4); character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4);
character.mapIndex = packet.characterInfo.mapIndex; character.mapIndex = packet.characterInfo.mapIndex;
character.position = packet.characterInfo.position; character.origin = packet.characterInfo.origin;
character.motion = packet.characterInfo.motion; character.motion = packet.characterInfo.motion;
character.stats = packet.characterInfo.stats; character.stats = packet.characterInfo.stats;
@@ -366,7 +366,7 @@ void InWorld::SendPlayerUpdate() {
packet.characterInfo.clientIndex = clientIndex; packet.characterInfo.clientIndex = clientIndex;
packet.characterInfo.accountIndex = accountIndex; packet.characterInfo.accountIndex = accountIndex;
packet.characterInfo.characterIndex = characterIndex; packet.characterInfo.characterIndex = characterIndex;
packet.characterInfo.position = localCharacter->position; packet.characterInfo.origin = localCharacter->origin;
packet.characterInfo.motion = localCharacter->motion; packet.characterInfo.motion = localCharacter->motion;
network.SendTo(Channels::SERVER, &packet); network.SendTo(Channels::SERVER, &packet);
+3 -3
View File
@@ -23,10 +23,10 @@
void CharacterData::Update(double delta) { void CharacterData::Update(double delta) {
if (motion.x && motion.y) { if (motion.x && motion.y) {
position += motion * delta * CHARACTER_WALKING_MOD; origin += motion * delta * CHARACTER_WALKING_MOD;
} }
else if (motion != 0) { else if (motion != 0) {
position += motion * delta; origin += motion * delta;
} }
#ifdef GRAPHICS #ifdef GRAPHICS
sprite.Update(delta); sprite.Update(delta);
@@ -36,7 +36,7 @@ void CharacterData::Update(double delta) {
#ifdef GRAPHICS #ifdef GRAPHICS
void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) { 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() { void CharacterData::CorrectSprite() {
+2 -4
View File
@@ -22,8 +22,6 @@
#ifndef CHARACTERDATA_HPP_ #ifndef CHARACTERDATA_HPP_
#define CHARACTERDATA_HPP_ #define CHARACTERDATA_HPP_
//POD members
#include "bbox.hpp"
#include "vector2.hpp" #include "vector2.hpp"
#include "statistics.hpp" #include "statistics.hpp"
@@ -48,7 +46,7 @@ struct CharacterData {
//world position //world position
int mapIndex = 0; int mapIndex = 0;
Vector2 position = {0.0,0.0}; Vector2 origin = {0.0,0.0};
Vector2 motion = {0.0,0.0}; Vector2 motion = {0.0,0.0};
//base statistics //base statistics
@@ -71,7 +69,7 @@ struct CharacterData {
#ifdef GRAPHICS #ifdef GRAPHICS
SpriteSheet sprite; SpriteSheet sprite;
#endif #endif
BBox bbox = {0,0,0,0}; Vector2 bounds = {0.0,0.0};
bool inCombat = false; bool inCombat = false;
int atbGauge = 0; int atbGauge = 0;
//TODO: stored command //TODO: stored command
+2 -4
View File
@@ -22,9 +22,7 @@
#ifndef COMBATDATA_HPP_ #ifndef COMBATDATA_HPP_
#define COMBATDATA_HPP_ #define COMBATDATA_HPP_
//POD members
#include "vector2.hpp" #include "vector2.hpp"
#include "bbox.hpp"
//gameplay members //gameplay members
#include "character_data.hpp" #include "character_data.hpp"
@@ -58,8 +56,8 @@ struct CombatData {
//world interaction //world interaction
int mapIndex = 0; int mapIndex = 0;
Vector2 position = {0.0,0.0}; Vector2 origin = {0.0,0.0};
BBox bbox = {0,0,0,0}; Vector2 bounds = {0.0,0.0};
//time interval //time interval
Clock::time_point lastTick = Clock::now(); Clock::time_point lastTick = Clock::now();
+3 -1
View File
@@ -22,7 +22,7 @@
#ifndef ENEMYDATA_HPP_ #ifndef ENEMYDATA_HPP_
#define ENEMYDATA_HPP_ #define ENEMYDATA_HPP_
//gameplay #include "vector2.hpp"
#include "statistics.hpp" #include "statistics.hpp"
//graphics //graphics
@@ -50,6 +50,8 @@ struct EnemyData {
//NOTE: these are lost when unloaded //NOTE: these are lost when unloaded
#ifdef GRAPHICS #ifdef GRAPHICS
SpriteSheet sprite; SpriteSheet sprite;
Vector2 origin = {0.0,0.0};
Vector2 bounds = {0.0,0.0};
#endif #endif
int tableIndex; int tableIndex;
int atbGauge = 0; int atbGauge = 0;
+8 -8
View File
@@ -108,8 +108,8 @@ void serializeCombat(SerialPacket* packet, char* buffer) {
//position //position
SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int));
SERIALIZE(buffer, &packet->combatInfo.position.x, sizeof(double)); SERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double));
SERIALIZE(buffer, &packet->combatInfo.position.y, sizeof(double)); SERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double));
//TODO: rewards //TODO: rewards
} }
@@ -147,8 +147,8 @@ void serializeCharacter(SerialPacket* packet, char* buffer) {
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
//vectors //vectors
SERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double));
SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double));
SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
@@ -252,8 +252,8 @@ void deserializeCombat(SerialPacket* packet, char* buffer) {
//position //position
DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int));
DESERIALIZE(buffer, &packet->combatInfo.position.x, sizeof(double)); DESERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double));
DESERIALIZE(buffer, &packet->combatInfo.position.y, sizeof(double)); DESERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double));
//TODO: rewards //TODO: rewards
} }
@@ -292,8 +292,8 @@ void deserializeCharacter(SerialPacket* packet, char* buffer) {
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
//vectors //vectors
DESERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double));
DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double));
DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
+2 -2
View File
@@ -150,7 +150,7 @@ union SerialPacket {
int characterArray[COMBAT_MAX_CHARACTER_COUNT]; int characterArray[COMBAT_MAX_CHARACTER_COUNT];
int enemyArray[COMBAT_MAX_ENEMY_COUNT]; int enemyArray[COMBAT_MAX_ENEMY_COUNT];
int mapIndex; int mapIndex;
Vector2 position; Vector2 origin;
//TODO: rewards //TODO: rewards
}combatInfo; }combatInfo;
@@ -163,7 +163,7 @@ union SerialPacket {
char handle[PACKET_STRING_SIZE]; char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE];
int mapIndex; int mapIndex;
Vector2 position; Vector2 origin;
Vector2 motion; Vector2 motion;
Statistics stats; Statistics stats;
}characterInfo; }characterInfo;
-75
View File
@@ -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 <type_traits>
#include <stdexcept>
#include <algorithm>
//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<BBox>::value, "BBox is not a POD");
#endif
+40
View File
@@ -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
);
}
+30
View File
@@ -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
-6
View File
@@ -42,12 +42,6 @@ public:
return x*x+y*y; 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 //Arithmetic operators
Vector2 operator+(Vector2 v) const { Vector2 operator+(Vector2 v) const {
Vector2 ret; Vector2 ret;
+2 -2
View File
@@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS Characters (
--position --position
mapIndex INTEGER DEFAULT 0, mapIndex INTEGER DEFAULT 0,
positionX INTEGER DEFAULT 0, originX INTEGER DEFAULT 0,
positionY INTEGER DEFAULT 0, originY INTEGER DEFAULT 0,
--statistics --statistics
level INTEGER DEFAULT 0, level INTEGER DEFAULT 0,
+6 -6
View File
@@ -32,7 +32,7 @@
//TODO: save and load the statistics //TODO: save and load the statistics
static const char* CREATE_CHARACTER = "INSERT INTO Characters (owner, handle, avatar) VALUES (?, ?, ?);"; 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* 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 = ?;"; 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<const char*>(sqlite3_column_text(statement, 3)); newChar.avatar = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
//Don't cache the birth //Don't cache the birth
//world position //world origin
newChar.mapIndex = sqlite3_column_int(statement, 5); newChar.mapIndex = sqlite3_column_int(statement, 5);
newChar.position.x = (double)sqlite3_column_int(statement, 6); newChar.origin.x = (double)sqlite3_column_int(statement, 6);
newChar.position.y = (double)sqlite3_column_int(statement, 7); newChar.origin.y = (double)sqlite3_column_int(statement, 7);
//statistics //statistics
newChar.stats.level = sqlite3_column_int(statement, 8); newChar.stats.level = sqlite3_column_int(statement, 8);
@@ -176,8 +176,8 @@ int ServerApplication::SaveCharacter(int uid) {
bool ret = false; bool ret = false;
ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK; ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK;
ret |= sqlite3_bind_int(statement, 2, character.mapIndex) != 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, 3, (int)character.origin.x) != SQLITE_OK;
ret |= sqlite3_bind_int(statement, 4, (int)character.position.y) != SQLITE_OK; ret |= sqlite3_bind_int(statement, 4, (int)character.origin.y) != SQLITE_OK;
//TODO: stats, etc. //TODO: stats, etc.
//check for binding errors //check for binding errors
+3 -3
View File
@@ -75,7 +75,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) {
packet.characterInfo.characterIndex = characterIndex; packet.characterInfo.characterIndex = characterIndex;
strncpy(packet.characterInfo.handle, characterMap[characterIndex].handle.c_str(), PACKET_STRING_SIZE); strncpy(packet.characterInfo.handle, characterMap[characterIndex].handle.c_str(), PACKET_STRING_SIZE);
strncpy(packet.characterInfo.avatar, characterMap[characterIndex].avatar.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; packet.characterInfo.motion = characterMap[characterIndex].motion;
PumpPacket(packet); 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.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
snprintf(newPacket.characterInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str()); snprintf(newPacket.characterInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
newPacket.characterInfo.mapIndex = it.second.mapIndex; 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.motion = it.second.motion;
newPacket.characterInfo.stats = it.second.stats; 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 //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; characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion;
PumpPacket(packet); PumpPacket(packet);