Created Player; not happy with movement
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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; };
|
||||
|
||||
+51
-23
@@ -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<SpriteSheet> 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<SpriteSheet> 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();
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef VECTOR2_H_
|
||||
#define VECTOR2_H_
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
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<typename T> inline Vector2 operator+=(T t) { return *this = *this + t; }
|
||||
template<typename T> inline Vector2 operator-=(T t) { return *this = *this - t; }
|
||||
template<typename T> inline Vector2 operator*=(T t) { return *this = *this * t; }
|
||||
template<typename T> 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<typename T> inline bool operator==(T t) { return (x == t && y == t); }
|
||||
template<typename T> inline bool operator!=(T t) { return (x != t || y != t); }
|
||||
};
|
||||
|
||||
#endif
|
||||
+17
-9
@@ -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
|
||||
|
||||
-------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user