From d833b768566d49bfce63433145080ccb2eb8e521 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 23 Jun 2013 13:53:55 +1000 Subject: [PATCH] PlayerCharacter is moving around smoothly --- client/character.cpp | 24 -------- client/character.hpp | 32 ----------- client/in_world.cpp | 34 +++++++++++- client/in_world.hpp | 7 ++- client/player_character.cpp | 108 ++++++++++++++++++++++++++++++++++++ client/player_character.hpp | 62 +++++++++++++++++++++ libs/common/defines.hpp | 6 ++ 7 files changed, 212 insertions(+), 61 deletions(-) delete mode 100644 client/character.cpp delete mode 100644 client/character.hpp create mode 100644 client/player_character.cpp create mode 100644 client/player_character.hpp diff --git a/client/character.cpp b/client/character.cpp deleted file mode 100644 index 6fcaf9a..0000000 --- a/client/character.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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.hpp" - - \ No newline at end of file diff --git a/client/character.hpp b/client/character.hpp deleted file mode 100644 index ade049f..0000000 --- a/client/character.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 CHARACTER_HPP_ - #define CHARACTER_HPP_ - -class Character { -public: - Character(); - ~Character(); -private: -}; - - #endif diff --git a/client/in_world.cpp b/client/in_world.cpp index 39a0d3d..27215db 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -34,6 +34,7 @@ InWorld::InWorld() { cout << "entering InWorld" << endl; #endif cout << "Client Index: " << infoMgr->GetClientIndex() << endl; + pc.GetSprite()->SetSurface(surfaceMgr->Get("elliot"), 32, 48); } InWorld::~InWorld() { @@ -52,6 +53,7 @@ void InWorld::FrameStart() { void InWorld::Update(double delta) { while(HandlePacket(popNetworkPacket())); + pc.Update(delta); } void InWorld::FrameEnd() { @@ -59,7 +61,7 @@ void InWorld::FrameEnd() { } void InWorld::Render(SDL_Surface* const screen) { - // + pc.DrawTo(screen); } //------------------------- @@ -89,11 +91,37 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) { case SDLK_ESCAPE: ExitGame(); break; + case SDLK_w: + pc.MoveDirection(CardinalDirection::NORTH); + break; + case SDLK_s: + pc.MoveDirection(CardinalDirection::SOUTH); + break; + case SDLK_a: + pc.MoveDirection(CardinalDirection::EAST); + break; + case SDLK_d: + pc.MoveDirection(CardinalDirection::WEST); + break; } } void InWorld::KeyUp(SDL_KeyboardEvent const& key) { - // + //reversed + switch(key.keysym.sym) { + case SDLK_w: + pc.MoveDirection(CardinalDirection::SOUTH); + break; + case SDLK_s: + pc.MoveDirection(CardinalDirection::NORTH); + break; + case SDLK_a: + pc.MoveDirection(CardinalDirection::WEST); + break; + case SDLK_d: + pc.MoveDirection(CardinalDirection::EAST); + break; + } } //------------------------- @@ -170,4 +198,4 @@ void InWorld::HandleDisconnection(::Disconnect& disconnect) { Disconnect(); SetNextScene(SceneList::MAINMENU); cout << "You have been disconnected" << endl; -} +} \ No newline at end of file diff --git a/client/in_world.hpp b/client/in_world.hpp index d363cff..bc9d745 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -36,6 +36,9 @@ #include "button.hpp" #include "raster_font.hpp" +//debugging +#include "player_character.hpp" + class InWorld : public BaseScene { public: //Public access members @@ -70,7 +73,7 @@ protected: InformationManager* infoMgr = ServiceLocator::Get(); //members - //... + PlayerCharacter pc; }; -#endif +#endif \ No newline at end of file diff --git a/client/player_character.cpp b/client/player_character.cpp new file mode 100644 index 0000000..f3894f0 --- /dev/null +++ b/client/player_character.cpp @@ -0,0 +1,108 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * 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 "player_character.hpp" + +void PlayerCharacter::Update(double delta) { + if (limitSpeed) { + constexpr double d = 1.0/sqrt(2); + position += motion * delta * d; + } + else { + position += motion * delta; + } + sprite.Update(delta); +} + +void PlayerCharacter::MoveDirection(CardinalDirection cd) { + //shift the movement in this direction + switch(cd) { + case CardinalDirection::NORTH: + if (motion.y >= 0) { + motion.y -= WALKING_SPEED; + } + break; + case CardinalDirection::SOUTH: + if (motion.y <= 0) { + motion.y += WALKING_SPEED; + } + break; + case CardinalDirection::EAST: + if (motion.x >= 0) { + motion.x -= WALKING_SPEED; + } + break; + case CardinalDirection::WEST: + if (motion.x <= 0) { + motion.x += WALKING_SPEED; + } + break; + } + //short cut + if (motion.x != 0 && motion.y != 0) { + sprite.SetInterval(0.1); + limitSpeed = true; + } + else if (motion.x != 0 || motion.y != 0) { + sprite.SetInterval(0.1); + limitSpeed = false; + } + else { + sprite.SetInterval(0); + sprite.SetCurrentFrame(0); + limitSpeed = false; + } + //face the correct direction + FaceDirection(); +} + +void PlayerCharacter::FaceDirection(CardinalDirection cd) { + switch(cd) { + case CardinalDirection::NORTH: + sprite.SetCurrentStrip(1); + break; + case CardinalDirection::SOUTH: + sprite.SetCurrentStrip(0); + break; + case CardinalDirection::EAST: + sprite.SetCurrentStrip(2); + break; + case CardinalDirection::WEST: + sprite.SetCurrentStrip(3); + break; + } +} + +void PlayerCharacter::FaceDirection() { + //base the direction on the character's movement + if (motion.y > 0) { + FaceDirection(CardinalDirection::SOUTH); + } + else if (motion.y < 0) { + FaceDirection(CardinalDirection::NORTH); + } + else if (motion.x < 0) { + FaceDirection(CardinalDirection::EAST); + } + else if (motion.x > 0) { + FaceDirection(CardinalDirection::WEST); + } +} diff --git a/client/player_character.hpp b/client/player_character.hpp new file mode 100644 index 0000000..9cfa21e --- /dev/null +++ b/client/player_character.hpp @@ -0,0 +1,62 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * 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 PLAYERCHARACTER_HPP_ +#define PLAYERCHARACTER_HPP_ + +#include "vector2.hpp" +#include "sprite_sheet.hpp" +#include "defines.hpp" + +class PlayerCharacter { +public: + PlayerCharacter() = default; + ~PlayerCharacter() = default; + + void Update(double delta); + + void MoveDirection(CardinalDirection); + void FaceDirection(CardinalDirection); + + void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); } + + //accessors and mutators + Vector2 SetPosition(Vector2 v) { return position = v; } + Vector2 ShiftPosition(Vector2 v) { return position += v; } + Vector2 GetPosition() { return position; } + + Vector2 SetMotion(Vector2 v) { return motion = v; } + Vector2 ShiftMotion(Vector2 v) { return motion += v; } + Vector2 GetMotion() { return motion; } + + SpriteSheet* GetSprite() { return &sprite; } +private: + void FaceDirection(); + + Vector2 position; + Vector2 motion; + SpriteSheet sprite; + + //for moving diagonal + bool limitSpeed = false; +}; + +#endif diff --git a/libs/common/defines.hpp b/libs/common/defines.hpp index 2a7ea39..a940976 100644 --- a/libs/common/defines.hpp +++ b/libs/common/defines.hpp @@ -25,4 +25,10 @@ #define GAME_CHANNEL 0 #define CHAT_CHANNEL 1 +#define WALKING_SPEED 140 + +enum class CardinalDirection { + NORTH, SOUTH, EAST, WEST +}; + #endif