-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97d2b34
commit fcb26a9
Showing
10 changed files
with
63 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters