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
+3 -3
View File
@@ -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() {
+2 -4
View File
@@ -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
+2 -4
View File
@@ -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();
+3 -1
View File
@@ -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;
+8 -8
View File
@@ -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));
+2 -2
View File
@@ -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;
-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;
}
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;