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