From 6428b02d858eb775cd663778feb594fe6901d9c7 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 28 May 2014 21:07:11 +1000 Subject: [PATCH] Refactored some code from InWorld into methods in CharacterData --- client/in_combat.hpp | 2 +- client/in_world.cpp | 18 ++------ common/gameplay/character_data.cpp | 67 ++++++++++++++++++++++++++++++ common/gameplay/character_data.hpp | 7 ++++ common/gameplay/makefile | 4 +- common/gameplay/sanity_check.cpp | 15 +++++++ 6 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 common/gameplay/character_data.cpp create mode 100644 common/gameplay/sanity_check.cpp diff --git a/client/in_combat.hpp b/client/in_combat.hpp index dbd802b..44e91ee 100644 --- a/client/in_combat.hpp +++ b/client/in_combat.hpp @@ -83,7 +83,7 @@ protected: void SendPlayerUpdate(); void RequestDisconnect(); void RequestShutdown(); - //TOOD: more + //TODO: more //shared parameters ConfigUtility& config; diff --git a/client/in_world.cpp b/client/in_world.cpp index ec2d69c..ecddb90 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -112,14 +112,7 @@ void InWorld::Update(double delta) { //update the characters for (auto& it : characterMap) { - if (it.second.motion.x && it.second.motion.y) { - //TODO: refactor this into a method - it.second.position += it.second.motion * delta * CHARACTER_WALKING_MOD; - } - else if (it.second.motion != 0) { - it.second.position += it.second.motion * delta; - } - //TODO: SPRITE: fix sprite + it.second.Update(delta); } //TODO: sort the players and entities by Y position @@ -152,8 +145,7 @@ void InWorld::Render(SDL_Surface* const screen) { //draw characters for (auto& it : characterMap) { - //TODO: refactor this - it.second.sprite.DrawTo(screen, it.second.position.x - camera.x, it.second.position.y - camera.y); + it.second.DrawTo(screen, camera.x, camera.y); } //draw UI @@ -316,8 +308,7 @@ void InWorld::HandleCharacterUpdate(SerialPacket packet) { characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position; characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion; } - //TODO: refactor this -// characterMap[packet.characterInfo.characterIndex].ResetDirection(); + characterMap[packet.characterInfo.characterIndex].CorrectSprite(); } void InWorld::HandleCharacterNew(SerialPacket packet) { @@ -330,8 +321,7 @@ void InWorld::HandleCharacterNew(SerialPacket packet) { characterMap[packet.characterInfo.characterIndex].sprite.LoadSurface(config["dir.sprites"] + packet.characterInfo.avatar, 4, 4); characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position; characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion; - //TODO: refactor this -// characterMap[packet.characterInfo.characterIndex].ResetDirection(); + characterMap[packet.characterInfo.characterIndex].CorrectSprite(); //catch this client's player object if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) { diff --git a/common/gameplay/character_data.cpp b/common/gameplay/character_data.cpp new file mode 100644 index 0000000..e86706c --- /dev/null +++ b/common/gameplay/character_data.cpp @@ -0,0 +1,67 @@ +/* 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 "character_data.hpp" + +void CharacterData::Update(double delta) { + if (motion.x && motion.y) { + position += motion * delta * CHARACTER_WALKING_MOD; + } + else if (motion != 0) { + position += motion * delta; + } +#ifdef GRAPHICS + sprite.Update(delta); +#endif +} + +#ifdef GRAPHICS + +void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) { + sprite.DrawTo(dest, position.x - camX, position.y - camY); +} + +void CharacterData::CorrectSprite() { + //NOTE: These must correspond to the sprite sheet in use + if (motion.y > 0) { + sprite.SetYIndex(0); + } + else if (motion.y < 0) { + sprite.SetYIndex(1); + } + else if (motion.x > 0) { + sprite.SetYIndex(3); + } + else if (motion.x < 0) { + sprite.SetYIndex(2); + } + + //animation + if (motion != 0) { + sprite.SetDelay(0.1); + } + else { + sprite.SetDelay(0); + sprite.SetXIndex(0); + } +} + +#endif \ No newline at end of file diff --git a/common/gameplay/character_data.hpp b/common/gameplay/character_data.hpp index 9c8602a..128fe16 100644 --- a/common/gameplay/character_data.hpp +++ b/common/gameplay/character_data.hpp @@ -59,6 +59,13 @@ struct CharacterData { //TODO: buffs //TODO: debuffs + //methods + void Update(double delta); +#ifdef GRAPHICS + void DrawTo(SDL_Surface* const, int camX, int camY); + void CorrectSprite(); +#endif + //active gameplay members //NOTE: these are lost when unloaded #ifdef GRAPHICS diff --git a/common/gameplay/makefile b/common/gameplay/makefile index 9013447..3be52be 100644 --- a/common/gameplay/makefile +++ b/common/gameplay/makefile @@ -1,7 +1,7 @@ #config -INCLUDES+=. +INCLUDES+=. ../utilities ../graphics LIBS+= -CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -DGRAPHICS #source CXXSRC=$(wildcard *.cpp) diff --git a/common/gameplay/sanity_check.cpp b/common/gameplay/sanity_check.cpp new file mode 100644 index 0000000..a7db44c --- /dev/null +++ b/common/gameplay/sanity_check.cpp @@ -0,0 +1,15 @@ +#include "account_data.hpp" +#include "character_data.hpp" +#include "client_data.hpp" +#include "combat_data.hpp" +#include "enemy_data.hpp" +#include "statistics.hpp" + +/* DOCS: Sanity check, read more + * Since most/all of the files in this directory are header files, I've created + * this source file as a "sanity check", to ensure that the above header files + * are written correctly via make. + * + * Oddly enough, I'm pretty sure this is the first directory compiled in a + * clean build. +*/ \ No newline at end of file