diff --git a/client/character_data.cpp b/client/character.cpp similarity index 89% rename from client/character_data.cpp rename to client/character.cpp index 81e4c75..eaa38d8 100644 --- a/client/character_data.cpp +++ b/client/character.cpp @@ -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); diff --git a/client/character_data.hpp b/client/character.hpp similarity index 58% rename from client/character_data.hpp rename to client/character.hpp index 8756346..ca2d9a4 100644 --- a/client/character_data.hpp +++ b/client/character.hpp @@ -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 #include -//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 +typedef std::map CharacterMap; + #endif diff --git a/client/client_application.cpp b/client/client_application.cpp index 95f529c..991cfd6 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -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")); diff --git a/client/client_application.hpp b/client/client_application.hpp index 54df4ff..2efb742 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -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 @@ -56,9 +54,7 @@ private: int accountIndex = -1; int characterIndex = -1; - std::map combatMap; - std::map characterMap; - std::map enemyMap; + CharacterMap characterMap; }; #endif diff --git a/client/combat_data.hpp b/client/combat_data.hpp deleted file mode 100644 index 1d6eb70..0000000 --- a/client/combat_data.hpp +++ /dev/null @@ -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 -#include -#include - -//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 characterArray; - std::array 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 diff --git a/client/enemy_data.hpp b/client/enemy_data.hpp deleted file mode 100644 index 6cd85f5..0000000 --- a/client/enemy_data.hpp +++ /dev/null @@ -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 - -//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 diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index f504e16..42ab69c 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -35,18 +35,14 @@ CleanUp::CleanUp( int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, - std::map* argCombatMap, - std::map* argCharacterMap, - std::map* 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(); diff --git a/client/scenes/clean_up.hpp b/client/scenes/clean_up.hpp index 5618cc7..5586f5c 100644 --- a/client/scenes/clean_up.hpp +++ b/client/scenes/clean_up.hpp @@ -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* argCombatMap, - std::map* argCharacterMap, - std::map* argEnemyMap + CharacterMap* argCharacterMap ); ~CleanUp(); @@ -79,9 +75,7 @@ protected: int& clientIndex; int& accountIndex; int& characterIndex; - std::map& combatMap; - std::map& characterMap; - std::map& enemyMap; + CharacterMap& characterMap; //graphics Image image; diff --git a/client/scenes/in_combat.cpp b/client/scenes/in_combat.cpp index 3d8b2f3..68c1bec 100644 --- a/client/scenes/in_combat.cpp +++ b/client/scenes/in_combat.cpp @@ -36,18 +36,14 @@ InCombat::InCombat( int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, - std::map* argCombatMap, - std::map* argCharacterMap, - std::map* 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"); diff --git a/client/scenes/in_combat.hpp b/client/scenes/in_combat.hpp index 6b31bc6..6a84851 100644 --- a/client/scenes/in_combat.hpp +++ b/client/scenes/in_combat.hpp @@ -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* argCombatMap, - std::map* argCharacterMap, - std::map* argEnemyMap + CharacterMap* argCharacterMap ); ~InCombat(); @@ -88,9 +84,7 @@ protected: int& clientIndex; int& accountIndex; int& characterIndex; - std::map& combatMap; - std::map& characterMap; - std::map& enemyMap; + CharacterMap& characterMap; //graphics //TODO: graphics diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 5301aa8..4f997a0 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -39,15 +39,13 @@ InWorld::InWorld( int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, - std::map* argCombatMap, - std::map* 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) { - //player movement - case SDLK_LEFT: - if (localCharacter) { - localCharacter->motion.x -= CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_RIGHT: - if (localCharacter) { - localCharacter->motion.x += CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_UP: - if (localCharacter) { - localCharacter->motion.y -= CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_DOWN: - if (localCharacter) { - localCharacter->motion.y += CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; + if (!localCharacter) { + return; } + + //player movement + Vector2 motion = localCharacter->GetMotion(); + switch(key.keysym.sym) { + case SDLK_LEFT: + motion.x -= CHARACTER_WALKING_SPEED; + break; + case SDLK_RIGHT: + motion.x += CHARACTER_WALKING_SPEED; + break; + case SDLK_UP: + motion.y -= CHARACTER_WALKING_SPEED; + break; + case SDLK_DOWN: + motion.y += CHARACTER_WALKING_SPEED; + break; + default: + return; + } + localCharacter->SetMotion(motion); + localCharacter->CorrectSprite(); + SendPlayerUpdate(); } void InWorld::KeyUp(SDL_KeyboardEvent const& key) { - switch(key.keysym.sym) { - //player movement - case SDLK_LEFT: - if (localCharacter) { - localCharacter->motion.x += CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_RIGHT: - if (localCharacter) { - localCharacter->motion.x -= CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_UP: - if (localCharacter) { - localCharacter->motion.y += CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; - - case SDLK_DOWN: - if (localCharacter) { - localCharacter->motion.y -= CHARACTER_WALKING_SPEED; - localCharacter->CorrectSprite(); - SendPlayerUpdate(); - } - break; + 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: + motion.x = std::min(motion.x + CHARACTER_WALKING_SPEED, 0.0); + break; + case SDLK_RIGHT: + motion.x = std::max(motion.x - CHARACTER_WALKING_SPEED, 0.0); + break; + case SDLK_UP: + motion.y = std::min(motion.y + CHARACTER_WALKING_SPEED, 0.0); + break; + case SDLK_DOWN: + motion.y = std::max(motion.y - CHARACTER_WALKING_SPEED, 0.0); + break; + default: + return; + } + localCharacter->SetMotion(motion); + localCharacter->CorrectSprite(); + SendPlayerUpdate(); } //------------------------- @@ -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 diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 156d35e..34ce942 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -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* argCombatMap, - std::map* argCharacterMap + CharacterMap* argCharacterMap ); ~InWorld(); @@ -101,8 +99,7 @@ protected: int& clientIndex; int& accountIndex; int& characterIndex; - std::map& combatMap; - std::map& characterMap; + CharacterMap& characterMap; //graphics Image buttonImage; @@ -124,7 +121,7 @@ protected: FrameRate fps; //game - CharacterData* localCharacter = nullptr; + Character* localCharacter = nullptr; }; #endif