From 8dcd02aba3103f9a4c337ab89597fa7864011abc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 10 Oct 2014 06:00:58 +1100 Subject: [PATCH] Separated some character code into Renderable --- client/character.cpp | 4 -- client/character.hpp | 28 ++------------ client/renderable.cpp | 31 +++++++++++++++ client/renderable.hpp | 56 +++++++++++++++++++++++++++ common/gameplay/character_defines.hpp | 1 + 5 files changed, 92 insertions(+), 28 deletions(-) create mode 100644 client/renderable.cpp create mode 100644 client/renderable.hpp diff --git a/client/character.cpp b/client/character.cpp index 70ee4a9..5970adc 100644 --- a/client/character.cpp +++ b/client/character.cpp @@ -31,10 +31,6 @@ void Character::Update() { sprite.Update(0.016); } -void Character::DrawTo(SDL_Surface* const dest, int camX, int camY) { - sprite.DrawTo(dest, origin.x - camX, origin.y - camY); -} - void Character::CorrectSprite() { //NOTE: These must correspond to the sprite sheet in use if (motion.y > 0) { diff --git a/client/character.hpp b/client/character.hpp index 6be3cc6..1178495 100644 --- a/client/character.hpp +++ b/client/character.hpp @@ -24,28 +24,22 @@ //components #include "character_defines.hpp" -#include "vector2.hpp" -#include "bounding_box.hpp" +#include "renderable.hpp" #include "statistics.hpp" -//graphics -#include "sprite_sheet.hpp" - //std namespace #include #include -class Character { +class Character : public Renderable { public: Character() = default; ~Character() = default; - void Update(); + void Update() override; //graphics - void DrawTo(SDL_Surface* const, int camX, int camY); void CorrectSprite(); - SpriteSheet* GetSprite() { return &sprite; } //gameplay Statistics* GetStats() { return &stats; } @@ -61,17 +55,8 @@ public: std::string GetAvatar() const { return avatar; } //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; } - BoundingBox SetBounds(BoundingBox b) { return bounds = b; } - BoundingBox GetBounds() { return bounds; } - + private: - //graphics - SpriteSheet sprite; - //base statistics Statistics stats; @@ -81,11 +66,6 @@ private: int owner; std::string handle; std::string avatar; - - //position - Vector2 origin = {0.0,0.0}; - Vector2 motion = {0.0,0.0}; - BoundingBox bounds; }; //tmp diff --git a/client/renderable.cpp b/client/renderable.cpp new file mode 100644 index 0000000..46d319d --- /dev/null +++ b/client/renderable.cpp @@ -0,0 +1,31 @@ +/* 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 "renderable.hpp" + +void Renderable::Update() { + origin += motion; + sprite.Update(0.016); +} + +void Renderable::DrawTo(SDL_Surface* const dest, int camX, int camY) { + sprite.DrawTo(dest, origin.x - camX, origin.y - camY); +} \ No newline at end of file diff --git a/client/renderable.hpp b/client/renderable.hpp new file mode 100644 index 0000000..d78cde2 --- /dev/null +++ b/client/renderable.hpp @@ -0,0 +1,56 @@ +/* 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 RENDERABLE_HPP_ +#define RENDERABLE_HPP_ + +#include "bounding_box.hpp" +#include "sprite_sheet.hpp" +#include "vector2.hpp" + +class Renderable { +public: + Renderable() = default; + virtual ~Renderable() = default; + + virtual void Update(); + virtual void DrawTo(SDL_Surface* const, int camX, int camY); + + SpriteSheet* GetSprite() { return &sprite; } + + //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; } + + //collision + BoundingBox SetBounds(BoundingBox b) { return bounds = b; } + BoundingBox GetBounds() { return bounds; } + +protected: //TODO: should be private + SpriteSheet sprite; + Vector2 origin = {0, 0}; + Vector2 motion = {0, 0}; + BoundingBox bounds; +}; + +#endif \ No newline at end of file diff --git a/common/gameplay/character_defines.hpp b/common/gameplay/character_defines.hpp index 484fcfa..d5ca3b6 100644 --- a/common/gameplay/character_defines.hpp +++ b/common/gameplay/character_defines.hpp @@ -27,6 +27,7 @@ //the speeds that the characters move constexpr double CHARACTER_WALKING_SPEED = 2.24; constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0); +constexpr double CHARACTER_WALKING_NEGATIVE_MOD = 1.0 - CHARACTER_WALKING_MOD; //the bounds for the character objects, mapped to the default sprites constexpr int CHARACTER_BOUNDS_X = 0;