Skip to content

Commit

Permalink
heh normal movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Feb 19, 2021
1 parent 97d2b34 commit fcb26a9
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion General.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct GameTime
//explicit GameTime(double time) : cur_sec(time) {};
//static GameTime NowTime;

static GameTime* Now() { static GameTime ret {}; return &ret; }
static GameTime& Now() { static GameTime ret {}; return ret; }

inline void SetCur(double sec) { cur_sec = sec; }
inline double GetSecAfter(double time_sec) { return cur_sec - time_sec; }
Expand Down
3 changes: 0 additions & 3 deletions Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ void Image::Draw(Image &canvas, Point p, bool flip_OX, bool flip_OY, bool not_mi
int this_x = this_x_zero;
int this_y = (p.y < 0) ? -p.y : 0;

//for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++)
//for(int x = start_x; x < end_x; x++, this_x++)
// canvas.MixPixel(x, y, GetPixel(this_x, this_y));
if (not_mix) {
#define Z(th_x, th_y) \
for(int y = start_y; y < end_y; y++, this_x = this_x_zero, this_y++) \
Expand Down
44 changes: 44 additions & 0 deletions Movement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef MAIN_MVMNT_H
#define MAIN_MVMNT_H

#include "General.h"

struct Movement
{
Movement(int _x, int _y) : x(_x), y(_y) { last_time = GameTime::Now().GetTime(); }
Movement(Point pos) : Movement(pos.x, pos.y) { }
void Move(int dx, int dy, int speed)
{
double now_t = GameTime::Now().GetTime();

if (dx || dy) {
double dt = now_t - last_time;
x += dt * dx * speed;
y += dt * dy * speed;
moved = true;
last_moved_time = now_t;
}

last_time = now_t;
}

Point Pos() { return {(int)x, (int)y}; }

bool Moved()
{
moved = moved && (GameTime::Now().GetSecAfter(last_moved_time) < time_for_unmoved);
return moved;
}

private:
double last_time = 0;
float x;
float y;

double last_moved_time = 0;
bool moved = false;

static constexpr double time_for_unmoved {0.2};
};

#endif
47 changes: 8 additions & 39 deletions Player.cpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,44 @@
#include "Player.h"


bool Player::Moved() const
{
if(coords.x == old_coords.x && coords.y == old_coords.y)
return false;
else
return true;
}

void Player::RefreshMoveState(E_Dir dir)
{
int move_dist = move_speed * 1;
switch (dir) {
case E_Dir::UP:
old_coords.y = coords.y;
coords.y += move_dist;
break;
case E_Dir::DOWN:
old_coords.y = coords.y;
coords.y -= move_dist;
break;
case E_Dir::LEFT:
old_coords.x = coords.x;
coords.x -= move_dist;
break;
case E_Dir::RIGHT:
old_coords.x = coords.x;
coords.x += move_dist;
break;
default:break;
}
}
bool Player::Moved() { return position.Moved(); }

void Player::RefreshMoveState(E_X_Dir x, E_Y_Dir y)
{
old_coords = coords;

position.Move((x == E_X_Dir::Not) ? 0 : ((x == E_X_Dir::Right) ? 1 : -1),
(y == E_Y_Dir::Not) ? 0 : ((y == E_Y_Dir::Up) ? 1 : -1), move_speed);

bool x_move = x != E_X_Dir::Not;
bool y_move = y != E_Y_Dir::Not;

if (x_move && y_move) {
bool change_dir = false;

E_Dir e_dir_x = (x == E_X_Dir::Left) ? E_Dir::LEFT : E_Dir::RIGHT;
E_Dir e_dir_y = (y == E_Y_Dir::Up) ? E_Dir::UP : E_Dir::DOWN;
RefreshMoveState(e_dir_x);
RefreshMoveState(e_dir_y);

if(e_dir_x != back_dir && e_dir_y != back_dir)
spr.SetDir(Is_X_Dir(back_dir) ? e_dir_y : e_dir_x);
}
else if (x_move) {
E_Dir e_dir_x = (x == E_X_Dir::Left) ? E_Dir::LEFT : E_Dir::RIGHT;
RefreshMoveState(e_dir_x);
spr.SetDir(e_dir_x);
back_dir = e_dir_x;
}
else if (y_move) {
E_Dir e_dir_y = (y == E_Y_Dir::Up) ? E_Dir::UP : E_Dir::DOWN;
RefreshMoveState(e_dir_y);
spr.SetDir(e_dir_y);
back_dir = e_dir_y;
}

coords = position.Pos();

//TODO : if (NOT ATTACKED)
spr.SetState(Moved() ? E_LiveObjState::Walk : E_LiveObjState::Idle);
//printf(Moved() ? "move\n" : "not move\n");

}

void Player::Draw(Image &screen)
{
old_coords = coords;
spr.Draw(screen, coords, true);
}
14 changes: 8 additions & 6 deletions Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

#include "LiveObjSprite.h"
#include "General.h"
#include "Movement.h"

struct Player
{
//explicit Player(Point pos = {.x = 10, .y = 10}) : coords(pos), old_coords(coords) {};

explicit Player(Point pos, LiveObjSprite &sprite) : coords(pos), old_coords(coords), spr(sprite) {};
explicit Player(Point pos, LiveObjSprite &sprite) : position(pos), coords(pos), old_coords(pos), spr(sprite) {};

bool Moved() const;
bool Moved();
void Draw(Image &screen);

inline void RefreshMoveState(bool up, bool down, bool left, bool right)
Expand All @@ -20,13 +21,14 @@ struct Player
}
void RefreshMoveState(E_X_Dir x, E_Y_Dir y);
private:
void RefreshMoveState(E_Dir dir);
E_Dir back_dir = E_Dir::DOWN;

Point coords {.x = 10, .y = 10};
Point old_coords {.x = 10, .y = 10};
Movement position;
Point coords {.x = 0, .y = 0};
Point old_coords {.x = 0, .y = 0};

Pixel color {.r = 255, .g = 255, .b = 0, .a = 255};
int move_speed = 4;
int move_speed = 100;

LiveObjSprite &spr;

Expand Down
2 changes: 1 addition & 1 deletion Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Sprite::Sprite(const std::string &path, SpritePixSz frame_sz, int ms_on_frame, i

void Sprite::Draw(Image &canvas, const Point p, bool flip_OX, bool flip_OY)
{
double dt = GameTime::Now()->GetSecAfter(time_start_prev_frame);
double dt = GameTime::Now().GetSecAfter(time_start_prev_frame);
if (dt < 0)dt = 0;

int add_frame = 0;
Expand Down
Binary file modified bin/Debug/main.exe
Binary file not shown.
Binary file modified bin/Debug/main.ilk
Binary file not shown.
Binary file modified bin/Debug/main.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int main(int argc, char** argv)
//GLfloat currentFrame = glfwGetTime();
//deltaTime = currentFrame - lastFrame;
//lastFrame = currentFrame;
GameTime::Now()->SetCur(glfwGetTime());
GameTime::Now().SetCur(glfwGetTime());

glfwPollEvents();

Expand Down

0 comments on commit fcb26a9

Please sign in to comment.