PlayerCharacter is moving around smoothly

This commit is contained in:
Kayne Ruse
2013-06-23 13:53:55 +10:00
parent 3232925ccd
commit d833b76856
7 changed files with 212 additions and 61 deletions
-24
View File
@@ -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"
-32
View File
@@ -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
+31 -3
View File
@@ -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;
}
}
+5 -2
View File
@@ -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<InformationManager>::Get();
//members
//...
PlayerCharacter pc;
};
#endif
#endif
+108
View File
@@ -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);
}
}
+62
View File
@@ -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
+6
View File
@@ -25,4 +25,10 @@
#define GAME_CHANNEL 0
#define CHAT_CHANNEL 1
#define WALKING_SPEED 140
enum class CardinalDirection {
NORTH, SOUTH, EAST, WEST
};
#endif