From 7dafb372b1e76b9b82c07571fa14e3415197445d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Apr 2013 08:27:03 +1000 Subject: [PATCH] Created Player; not happy with movement --- client/makefile | 2 +- client/player.cpp | 88 +++++++++++++++++++++++++++++++++++++++++ client/player.h | 40 +++++++++++++++++++ client/sprite_sheet.cpp | 6 +-- client/sprite_sheet.h | 2 +- client/unit.cpp | 74 +++++++++++++++++++++++----------- client/vector2.h | 73 ++++++++++++++++++++++++++++++++++ docs/pseudocode.txt | 26 +++++++----- 8 files changed, 274 insertions(+), 37 deletions(-) create mode 100644 client/player.cpp create mode 100644 client/player.h create mode 100644 client/vector2.h diff --git a/client/makefile b/client/makefile index d9d20a1..3861c6e 100644 --- a/client/makefile +++ b/client/makefile @@ -15,4 +15,4 @@ clean: -$(RM) *.o *.a *.exe unit: - $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp -lmingw32 -lSDLmain -lSDL + $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp player.cpp -lmingw32 -lSDLmain -lSDL diff --git a/client/player.cpp b/client/player.cpp new file mode 100644 index 0000000..d5bbbc4 --- /dev/null +++ b/client/player.cpp @@ -0,0 +1,88 @@ +#include "player.h" + +#define ANIMATION_SPEED 200 +#define WALKING_SPEED 0.14 + +Player::Player(SDL_Surface* s, int w, int h) + : sprite(s, w, h) +{ +} + +void Player::Update(int delta) { + if (motion.y > 0) { + FaceDirection(Direction::SOUTH); + } + else if (motion.y < 0) { + FaceDirection(Direction::NORTH); + } + else if (motion.x < 0) { + FaceDirection(Direction::WEST); + } + else if (motion.x > 0) { + FaceDirection(Direction::EAST); + } + + if (motion != 0) { + sprite.SetInterval(ANIMATION_SPEED); + position += motion * delta; + } + else { + sprite.SetCurrentFrame(0); + sprite.SetInterval(0); + } + sprite.Update(delta); +} + +void Player::WalkInDirection(Direction d) { + switch(d) { + case Direction::NORTH: + if (motion.y >= 0) { + motion.y -= WALKING_SPEED; + } + else { + motion.y = -WALKING_SPEED; + } + break; + case Direction::SOUTH: + if (motion.y <= 0) { + motion.y += WALKING_SPEED; + } + else { + motion.y = WALKING_SPEED; + } + break; + case Direction::WEST: + if (motion.x >= 0) { + motion.x -= WALKING_SPEED; + } + else { + motion.x = -WALKING_SPEED; + } + break; + case Direction::EAST: + if (motion.x <= 0) { + motion.x += WALKING_SPEED; + } + else { + motion.x = WALKING_SPEED; + } + break; + } +} + +void Player::FaceDirection(Direction dir) { + switch(dir) { + case Direction::SOUTH: + sprite.SetCurrentStrip(0); + break; + case Direction::NORTH: + sprite.SetCurrentStrip(1); + break; + case Direction::WEST: + sprite.SetCurrentStrip(2); + break; + case Direction::EAST: + sprite.SetCurrentStrip(3); + break; + } +} \ No newline at end of file diff --git a/client/player.h b/client/player.h new file mode 100644 index 0000000..b994c87 --- /dev/null +++ b/client/player.h @@ -0,0 +1,40 @@ +#ifndef PLAYER_H_ +#define PLAYER_H_ + +#include "vector2.h" +#include "sprite_sheet.h" + +#include "SDL/SDL.h" + +enum class Direction { + NORTH, SOUTH, + EAST, WEST +}; + +class Player { +public: + Player(SDL_Surface*, int w, int h); + + void Update(int delta); + + void WalkInDirection(Direction); + + Vector2 SetPosition(Vector2 v) { return position = v; } + Vector2 ShiftPosition(Vector2 v) { return position += v; } + Vector2 GetPosition() const { return position; }; + + Vector2 SetMotion(Vector2 v) { return motion = v; } + Vector2 ShiftMotion(Vector2 v) { return motion += v; } + Vector2 GetMotion() const { return motion; } + + void DrawTo(SDL_Surface* s) { sprite.DrawTo(s, position.x, position.y); } + void FaceDirection(Direction); + + SpriteSheet* GetSpriteSheet() { return &sprite; }; +private: + Vector2 position; + Vector2 motion; + SpriteSheet sprite; +}; + +#endif diff --git a/client/sprite_sheet.cpp b/client/sprite_sheet.cpp index b4d85e9..c23e983 100644 --- a/client/sprite_sheet.cpp +++ b/client/sprite_sheet.cpp @@ -9,12 +9,12 @@ SpriteSheet::SpriteSheet(SDL_Surface* s, int w, int h) } void SpriteSheet::Update(int delta) { - if ((ticks += delta) > interval) { + if (interval && (ticks += delta) > interval) { if (++currentFrame >= maxFrames) { currentFrame = 0; } ticks = 0; - SetClipX(currentFrame * GetClipW()); - SetClipY(currentStrip * GetClipH()); } + SetClipX(currentFrame * GetClipW()); + SetClipY(currentStrip * GetClipH()); } diff --git a/client/sprite_sheet.h b/client/sprite_sheet.h index 7b89862..5a35005 100644 --- a/client/sprite_sheet.h +++ b/client/sprite_sheet.h @@ -24,7 +24,7 @@ public: int SetCurrentFrame(int i) { return currentFrame = i; }; int SetCurrentStrip(int i) { return currentStrip = i; }; - int SetInterval(int i) { ticks = 0; return interval = i; } + int SetInterval(int i) { return interval = i; } int GetCurrentFrame() const { return currentFrame; }; int GetCurrentStrip() const { return currentStrip; }; diff --git a/client/unit.cpp b/client/unit.cpp index d23cce1..c8811af 100644 --- a/client/unit.cpp +++ b/client/unit.cpp @@ -1,4 +1,5 @@ #include "delta.h" +#include "player.h" #include "sprite_sheet.h" #include "image.h" #include "surface_manager.h" @@ -24,17 +25,19 @@ int go(int, char**) { sMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp"); - SpriteSheet player(sMgr.Get("player"), 32, 48); + Player player(sMgr.Get("player"), 32, 48); Image tiles(sMgr.Get("tileset")); + SpriteSheet rose(sMgr.Get("flower"), 32, 32); - player.SetInterval(200); + player.GetSpriteSheet()->SetInterval(200); + rose.SetInterval(200); - vector flowerVector; - for (int i = 0; i < 100; i++) { - SpriteSheet ss(sMgr.Get("flower"), 32, 32); - ss.SetInterval(i%5 + 95); - flowerVector.push_back(ss); - } +// vector flowerVector; +// for (int i = 0; i < 100; i++) { +// SpriteSheet ss(sMgr.Get("flower"), 32, 32); +// ss.SetInterval(i%5 + 95); +// flowerVector.push_back(ss); +// } Delta delta; @@ -51,17 +54,37 @@ int go(int, char**) { case SDLK_ESCAPE: running = false; break; - case SDLK_1: - player.SetCurrentStrip(0); + case SDLK_w: + case SDLK_UP: + player.WalkInDirection(Direction::NORTH); break; - case SDLK_2: - player.SetCurrentStrip(1); + case SDLK_s: + case SDLK_DOWN: + player.WalkInDirection(Direction::SOUTH); break; - case SDLK_3: - player.SetCurrentStrip(2); + case SDLK_a: + case SDLK_LEFT: + player.WalkInDirection(Direction::WEST); break; - case SDLK_4: - player.SetCurrentStrip(3); + case SDLK_d: + case SDLK_RIGHT: + player.WalkInDirection(Direction::EAST); + break; + } + break; + case SDL_KEYUP: + switch(event.key.keysym.sym) { + case SDLK_w: + player.WalkInDirection(Direction::SOUTH); + break; + case SDLK_s: + player.WalkInDirection(Direction::NORTH); + break; + case SDLK_a: + player.WalkInDirection(Direction::EAST); + break; + case SDLK_d: + player.WalkInDirection(Direction::WEST); break; } } @@ -70,21 +93,26 @@ int go(int, char**) { delta.Calculate(); player.Update(delta.GetDelta()); + rose.Update(0); - for (int i = 0; i < 100; i++) { - flowerVector[i].Update(delta.GetDelta()); - } +// for (int i = 0; i < 100; i++) { +// flowerVector[i].Update(delta.GetDelta()); +// } SDL_FillRect(screen, 0, 0); tiles.DrawTo(screen, 0, 0); - player.DrawTo(screen, 0, 0); + player.DrawTo(screen); + rose.DrawTo(screen, 50, 100); - for (int i = 0; i < 100; i++) { - flowerVector[i].DrawTo(screen, 20+i*4, i*4); - } +// for (int i = 0; i < 100; i++) { +// flowerVector[i].DrawTo(screen, 20+i*4, i*4); +// } SDL_Flip(screen); + + //debugging +// cout << player.GetSpriteSheet()->GetCurrentFrame() << endl; } SDL_Quit(); diff --git a/client/vector2.h b/client/vector2.h new file mode 100644 index 0000000..9e7744e --- /dev/null +++ b/client/vector2.h @@ -0,0 +1,73 @@ +#ifndef VECTOR2_H_ +#define VECTOR2_H_ + +#include +#include + +class Vector2 { +public: + /* Public access members */ + double x, y; + + inline Vector2(): + x(0), y(0) + { + } + + inline Vector2(double d): + x(d), y(d) + { + } + + inline Vector2(double _x, double _y): + x(_x), y(_y) + { + } + + inline double Length() { + return sqrt(x*x+y*y); + } + + inline double SquaredLength() { + return x*x+y*y; + } + + inline double operator[](size_t i) { + if (i >= 2) + throw(std::domain_error("Out of range")); + + return *(&x+i); + } + + /* Arithmetic operators */ + inline Vector2 operator+(Vector2 v) { return Vector2(x + v.x, y + v.y); } + inline Vector2 operator-(Vector2 v) { return Vector2(x - v.x, y - v.y); } + inline Vector2 operator*(Vector2 v) { return Vector2(x * v.x, y * v.y); } + inline Vector2 operator*(double d) { return Vector2(x * d, y * d); } + + inline Vector2 operator/(Vector2 v) { + if (!v.x || !v.y) + throw(std::domain_error("Divide by zero")); + + return Vector2(x / v.x, y / v.y); + } + + inline Vector2 operator/(double d) { + if (!d) + throw(std::domain_error("Divide by zero")); + + return Vector2(x / d, y / d); + } + + template inline Vector2 operator+=(T t) { return *this = *this + t; } + template inline Vector2 operator-=(T t) { return *this = *this - t; } + template inline Vector2 operator*=(T t) { return *this = *this * t; } + template inline Vector2 operator/=(T t) { return *this = *this / t; } + + inline bool operator==(Vector2 v) { return (x == v.x && y == v.y); } + inline bool operator!=(Vector2 v) { return (x != v.x || y != v.y); } + template inline bool operator==(T t) { return (x == t && y == t); } + template inline bool operator!=(T t) { return (x != t || y != t); } +}; + +#endif diff --git a/docs/pseudocode.txt b/docs/pseudocode.txt index 309cd30..df0ec3a 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -19,18 +19,26 @@ Player: ------------------------- -Animations: - multiple cells in one animation (x-axis) - multiple animations in one sheet (y-axis) +Player's movement in a server -Image: - SetSurface(surface) - GetSurface() +#define PLAYER_FACE_DOWN 0 +#define PLAYER_FACE_UP 1 +#define PLAYER_FACE_LEFT 2 +#define PLAYER_FACE_RIGHT 3 - SetClip - GetClip - //clips + +Player: + index --global index on the server + position + motion + image --avatar chosen by the player (later) + ShiftMotion(vector relativeMotion) + + +PlayerManager: + Update(delta) //all player objects + Synchronize(dataArray) //possible -------------------------