Encapsulated the Character class

This commit is contained in:
Kayne Ruse
2014-07-04 00:55:12 +10:00
parent 839c2af940
commit e946a0741d
12 changed files with 143 additions and 269 deletions
@@ -19,9 +19,9 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "character_data.hpp"
#include "character.hpp"
void CharacterData::Update(double delta) {
void Character::Update(double delta) {
if (motion.x && motion.y) {
origin += motion * delta * CHARACTER_WALKING_MOD;
}
@@ -31,11 +31,11 @@ void CharacterData::Update(double delta) {
sprite.Update(delta);
}
void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) {
void Character::DrawTo(SDL_Surface* const dest, int camX, int camY) {
sprite.DrawTo(dest, origin.x - camX, origin.y - camY);
}
void CharacterData::CorrectSprite() {
void Character::CorrectSprite() {
//NOTE: These must correspond to the sprite sheet in use
if (motion.y > 0) {
sprite.SetYIndex(0);
@@ -19,8 +19,8 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef CHARACTERDATA_HPP_
#define CHARACTERDATA_HPP_
#ifndef CHARACTER_HPP_
#define CHARACTER_HPP_
//components
#include "character_defines.hpp"
@@ -34,38 +34,61 @@
#include <string>
#include <cmath>
//TODO: encapsulate this and the server version
struct CharacterData {
class Character {
public:
Character() = default;
~Character() = default;
void Update(double delta);
//graphics
void DrawTo(SDL_Surface* const, int camX, int camY);
void CorrectSprite();
SpriteSheet* GetSprite() { return &sprite; }
//gameplay
Statistics* GetStats() { return &stats; }
//accessors and mutators
//metadata
int owner;
std::string handle;
std::string avatar;
int SetOwner(int i) { return owner = i; }
int GetOwner() { return owner; }
std::string SetHandle(std::string s) { return handle = s; }
std::string GetHandle() const { return handle; }
std::string SetAvatar(std::string s) { return avatar = s; }
std::string GetAvatar() const { return avatar; }
//members
//position
Vector2 SetOrigin(Vector2 v) { return origin = v; }
Vector2 GetOrigin() const { return origin; }
Vector2 SetMotion(Vector2 v) { return motion = v; }
Vector2 GetMotion() const { return motion; }
Vector2 SetBounds(Vector2 v) { return bounds = v; }
Vector2 GetBounds() const { return bounds; }
private:
//graphics
SpriteSheet sprite;
//world position
int roomIndex = 0;
Vector2 origin = {0.0,0.0};
Vector2 motion = {0.0,0.0};
Vector2 bounds = {0.0,0.0};
//base statistics
Statistics stats;
//TODO: gameplay components: equipment, items, buffs, debuffs
//methods
void Update(double delta);
//metadata
int owner;
std::string handle;
std::string avatar;
void DrawTo(SDL_Surface* const, int camX, int camY);
void CorrectSprite();
//active gameplay members
//NOTE: these are lost when unloaded
bool inCombat = false;
int atbGauge = 0;
//TODO: stored command
//position
Vector2 origin = {0.0,0.0};
Vector2 motion = {0.0,0.0};
Vector2 bounds = {CHARACTER_BOUNDS_WIDTH,CHARACTER_BOUNDS_HEIGHT};
};
//tmp
#include <map>
typedef std::map<int, Character> CharacterMap;
#endif
+3 -3
View File
@@ -177,13 +177,13 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex);
break;
case SceneList::INWORLD:
activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap);
activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
break;
case SceneList::INCOMBAT:
activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap, &enemyMap);
activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
break;
case SceneList::CLEANUP:
activeScene = new CleanUp(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap, &enemyMap);
activeScene = new CleanUp(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
break;
default:
throw(std::logic_error("Failed to recognize the scene index"));
+2 -6
View File
@@ -27,9 +27,7 @@
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "character_data.hpp"
#include "combat_data.hpp"
#include "enemy_data.hpp"
#include "character.hpp"
#include <map>
@@ -56,9 +54,7 @@ private:
int accountIndex = -1;
int characterIndex = -1;
std::map<int, CombatData> combatMap;
std::map<int, CharacterData> characterMap;
std::map<int, EnemyData> enemyMap;
CharacterMap characterMap;
};
#endif
-53
View File
@@ -1,53 +0,0 @@
/* 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 COMBATDATA_HPP_
#define COMBATDATA_HPP_
#include "vector2.hpp"
#include "combat_defines.hpp"
//gameplay members
#include "character_data.hpp"
#include "enemy_data.hpp"
//std namespace
#include <chrono>
#include <array>
#include <utility>
//NOTE: This is a placeholder, since it'd break to client too much to remove it
struct CombatData {
typedef std::chrono::steady_clock Clock;
std::array<CharacterData, COMBAT_MAX_CHARACTERS> characterArray;
std::array<EnemyData, COMBAT_MAX_ENEMIES> enemyArray;
//world interaction
int mapIndex = 0;
Vector2 origin = {0.0,0.0};
Vector2 bounds = {0.0,0.0};
//time interval
Clock::time_point lastTick = Clock::now();
};
#endif
-48
View File
@@ -1,48 +0,0 @@
/* 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 ENEMYDATA_HPP_
#define ENEMYDATA_HPP_
#include "vector2.hpp"
#include "statistics.hpp"
//std namespace
#include <string>
//NOTE: This is a placeholder, since it'd break to client too much to remove it
struct EnemyData {
//metadata
std::string handle;
std::string avatar;
//gameplay
Statistics stats;
//TODO: gameplay components: equipment, items, buffs, debuffs, rewards
//active gameplay members
//NOTE: these are lost when unloaded
int tableIndex;
int atbGauge = 0;
};
#endif
+4 -8
View File
@@ -35,18 +35,14 @@ CleanUp::CleanUp(
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap,
std::map<int, EnemyData>* argEnemyMap
CharacterMap* argCharacterMap
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex),
characterIndex(*argCharacterIndex),
combatMap(*argCombatMap),
characterMap(*argCharacterMap),
enemyMap(*argEnemyMap)
characterMap(*argCharacterMap)
{
//setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
@@ -69,9 +65,9 @@ CleanUp::CleanUp(
clientIndex = -1;
accountIndex = -1;
characterIndex = -1;
combatMap.clear();
// combatMap.clear();
characterMap.clear();
enemyMap.clear();
// enemyMap.clear();
//auto return
startTick = std::chrono::steady_clock::now();
+3 -9
View File
@@ -34,9 +34,7 @@
#include "config_utility.hpp"
#include "frame_rate.hpp"
#include "combat_data.hpp"
#include "character_data.hpp"
#include "enemy_data.hpp"
#include "character.hpp"
//client
#include "base_scene.hpp"
@@ -53,9 +51,7 @@ public:
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap,
std::map<int, EnemyData>* argEnemyMap
CharacterMap* argCharacterMap
);
~CleanUp();
@@ -79,9 +75,7 @@ protected:
int& clientIndex;
int& accountIndex;
int& characterIndex;
std::map<int, CombatData>& combatMap;
std::map<int, CharacterData>& characterMap;
std::map<int, EnemyData>& enemyMap;
CharacterMap& characterMap;
//graphics
Image image;
+2 -6
View File
@@ -36,18 +36,14 @@ InCombat::InCombat(
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap,
std::map<int, EnemyData>* argEnemyMap
CharacterMap* argCharacterMap
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex),
characterIndex(*argCharacterIndex),
combatMap(*argCombatMap),
characterMap(*argCharacterMap),
enemyMap(*argEnemyMap)
characterMap(*argCharacterMap)
{
/* //setup the utility objects
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
+3 -9
View File
@@ -34,9 +34,7 @@
#include "config_utility.hpp"
#include "frame_rate.hpp"
#include "combat_data.hpp"
#include "character_data.hpp"
#include "enemy_data.hpp"
#include "character.hpp"
//client
#include "base_scene.hpp"
@@ -50,9 +48,7 @@ public:
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap,
std::map<int, EnemyData>* argEnemyMap
CharacterMap* argCharacterMap
);
~InCombat();
@@ -88,9 +84,7 @@ protected:
int& clientIndex;
int& accountIndex;
int& characterIndex;
std::map<int, CombatData>& combatMap;
std::map<int, CharacterData>& characterMap;
std::map<int, EnemyData>& enemyMap;
CharacterMap& characterMap;
//graphics
//TODO: graphics
+53 -74
View File
@@ -39,15 +39,13 @@ InWorld::InWorld(
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap
CharacterMap* argCharacterMap
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex),
characterIndex(*argCharacterIndex),
combatMap(*argCombatMap),
characterMap(*argCharacterMap)
{
//setup the utility objects
@@ -111,8 +109,8 @@ void InWorld::Update(double delta) {
//update the camera
if(localCharacter) {
camera.x = localCharacter->origin.x - camera.marginX;
camera.y = localCharacter->origin.y - camera.marginY;
camera.x = localCharacter->GetOrigin().x - camera.marginX;
camera.y = localCharacter->GetOrigin().y - camera.marginY;
}
//check the map
@@ -178,77 +176,60 @@ void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
}
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) {
if (!localCharacter) {
return;
}
//player movement
Vector2 motion = localCharacter->GetMotion();
switch(key.keysym.sym) {
case SDLK_LEFT:
if (localCharacter) {
localCharacter->motion.x -= CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.x -= CHARACTER_WALKING_SPEED;
break;
case SDLK_RIGHT:
if (localCharacter) {
localCharacter->motion.x += CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.x += CHARACTER_WALKING_SPEED;
break;
case SDLK_UP:
if (localCharacter) {
localCharacter->motion.y -= CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.y -= CHARACTER_WALKING_SPEED;
break;
case SDLK_DOWN:
if (localCharacter) {
localCharacter->motion.y += CHARACTER_WALKING_SPEED;
motion.y += CHARACTER_WALKING_SPEED;
break;
default:
return;
}
localCharacter->SetMotion(motion);
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
break;
}
}
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) {
if (!localCharacter) {
return;
}
//player movement
Vector2 motion = localCharacter->GetMotion();
switch(key.keysym.sym) {
//NOTE: The use of min/max here are to prevent awkward movements
case SDLK_LEFT:
if (localCharacter) {
localCharacter->motion.x += CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.x = std::min(motion.x + CHARACTER_WALKING_SPEED, 0.0);
break;
case SDLK_RIGHT:
if (localCharacter) {
localCharacter->motion.x -= CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.x = std::max(motion.x - CHARACTER_WALKING_SPEED, 0.0);
break;
case SDLK_UP:
if (localCharacter) {
localCharacter->motion.y += CHARACTER_WALKING_SPEED;
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
motion.y = std::min(motion.y + CHARACTER_WALKING_SPEED, 0.0);
break;
case SDLK_DOWN:
if (localCharacter) {
localCharacter->motion.y -= CHARACTER_WALKING_SPEED;
motion.y = std::max(motion.y - CHARACTER_WALKING_SPEED, 0.0);
break;
default:
return;
}
localCharacter->SetMotion(motion);
localCharacter->CorrectSprite();
SendPlayerUpdate();
}
break;
}
}
//-------------------------
@@ -289,36 +270,35 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
}
//create the character object
CharacterData& character = characterMap[argPacket->characterIndex];
Character& newCharacter = characterMap[argPacket->characterIndex];
//fill out the character's members
character.handle = argPacket->handle;
character.avatar = argPacket->avatar;
newCharacter.SetHandle(argPacket->handle);
newCharacter.SetAvatar(argPacket->avatar);
character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4);
character.bounds = {CHARACTER_BOUNDS_WIDTH, CHARACTER_BOUNDS_HEIGHT};
newCharacter.GetSprite()->LoadSurface(config["dir.sprites"] + newCharacter.GetAvatar(), 4, 4);
character.roomIndex = argPacket->roomIndex;
character.origin = argPacket->origin;
character.motion = argPacket->motion;
newCharacter.SetOrigin(argPacket->origin);
newCharacter.SetMotion(argPacket->motion);
character.stats = argPacket->stats;
(*newCharacter.GetStats()) = argPacket->stats;
//bookkeeping code
character.CorrectSprite();
newCharacter.CorrectSprite();
//catch this client's player object
if (argPacket->accountIndex == accountIndex && !localCharacter) {
characterIndex = argPacket->characterIndex;
localCharacter = &character;
localCharacter = &newCharacter;
//setup the camera
//TODO: move this?
camera.width = GetScreen()->w;
camera.height = GetScreen()->h;
//center on the player's character
camera.marginX = (GetScreen()->w / 2 - localCharacter->sprite.GetImage()->GetClipW() / 2);
camera.marginY = (GetScreen()->h / 2 - localCharacter->sprite.GetImage()->GetClipH() / 2);
camera.marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2);
camera.marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2);
}
}
@@ -341,13 +321,12 @@ void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
return;
}
CharacterData& character = characterMap[argPacket->characterIndex];
Character& character = characterMap[argPacket->characterIndex];
//other characters moving
if (argPacket->characterIndex != characterIndex) {
character.roomIndex = argPacket->roomIndex;
character.origin = argPacket->origin;
character.motion = argPacket->motion;
character.SetOrigin(argPacket->origin);
character.SetMotion(argPacket->motion);
character.CorrectSprite();
}
}
@@ -388,10 +367,10 @@ void InWorld::SendPlayerUpdate() {
newPacket.characterIndex = characterIndex;
//NOTE: omitting the handle and avatar here
newPacket.accountIndex = accountIndex;
newPacket.roomIndex = localCharacter->roomIndex;
newPacket.origin = localCharacter->origin;
newPacket.motion = localCharacter->motion;
newPacket.stats = localCharacter->stats;
newPacket.roomIndex = 0; //TODO: room index
newPacket.origin = localCharacter->GetOrigin();
newPacket.motion = localCharacter->GetMotion();
newPacket.stats = *localCharacter->GetStats();
//TODO: gameplay components: equipment, items, buffs, debuffs
+4 -7
View File
@@ -38,8 +38,7 @@
#include "config_utility.hpp"
#include "frame_rate.hpp"
#include "combat_data.hpp"
#include "character_data.hpp"
#include "character.hpp"
//client
#include "base_scene.hpp"
@@ -56,8 +55,7 @@ public:
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
std::map<int, CombatData>* argCombatMap,
std::map<int, CharacterData>* argCharacterMap
CharacterMap* argCharacterMap
);
~InWorld();
@@ -101,8 +99,7 @@ protected:
int& clientIndex;
int& accountIndex;
int& characterIndex;
std::map<int, CombatData>& combatMap;
std::map<int, CharacterData>& characterMap;
CharacterMap& characterMap;
//graphics
Image buttonImage;
@@ -124,7 +121,7 @@ protected:
FrameRate fps;
//game
CharacterData* localCharacter = nullptr;
Character* localCharacter = nullptr;
};
#endif