Skip to content

Commit

Permalink
idle for enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 3, 2021
1 parent d38c445 commit a6b36fc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
11 changes: 10 additions & 1 deletion Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void Enemy::Draw(Image &canvas)
if (hp <= 0)alive = false;
}

if (cur_state == E_LiveObjState::Walk) {
if (!mov.Moved())cur_state = E_LiveObjState::Idle;
}

int hp_x = p.x - (max_hp / 20) * 12;
for (int i = 0; i * 10 < max_hp; i ++) {
SpriteManager::Get().DrawHpBlock(canvas, {.x = hp_x + i * 12, .y = p.y - sz.h / 2 - 5}, hp - i * 10);
Expand Down Expand Up @@ -57,7 +61,8 @@ void Enemy::Move(Point player_pos)
if (!alive)return;
int r = get_r(type);
auto pos = mov.CenterPos();
if ((player_pos.x + r * TILE_SZ < pos.x) ||
if ((cur_state == E_LiveObjState::TakeHit || cur_state == E_LiveObjState::Attack) ||
(player_pos.x + r * TILE_SZ < pos.x) ||
(player_pos.x - r * TILE_SZ > pos.x) ||
(player_pos.y + r * TILE_SZ < pos.y) ||
(player_pos.y - r * TILE_SZ > pos.y)) {
Expand All @@ -67,8 +72,12 @@ void Enemy::Move(Point player_pos)
bool x_eq = std::abs(player_pos.x - pos.x) < 3;
bool pl_x_less = player_pos.x < pos.x;


bool y_eq = std::abs(player_pos.y - pos.y) < 3;
bool pl_y_less = player_pos.y < pos.y;

if (!x_eq)cur_dir = pl_x_less ? E_Dir::LEFT : E_Dir::RIGHT;

mov.Move(x_eq ? 0 : pl_x_less ? -1 : 1, y_eq ? 0 : pl_y_less ? -1 : 1, speed);
if (mov.Moved())cur_state = E_LiveObjState::Walk;
}
7 changes: 3 additions & 4 deletions LiveObjSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ enum class E_LiveObjType
struct LiveObjSprite
{

LiveObjSprite(E_LiveObjType must_enemy, int type, int move_frames, int attack_frames, int take_hit_frames, int ms_speed) : spr_states()
LiveObjSprite(E_LiveObjType must_enemy, int type, int move_frames, int attack_frames, int take_hit_frames, int idle_frames, int ms_speed) : spr_states()
{
if (must_enemy != E_LiveObjType::Enemy)error("must enemy. for character use another constructor");
lo_type = must_enemy;
cur_dir = std::rand() % 2 ? E_Dir::LEFT : E_Dir::RIGHT;
std::string path_0 = "../resources/enemies/" + std::to_string(type) + "/";
spr_states.insert({E_LiveObjState::Idle, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "move.png", move_frames, ms_speed)});
spr_states.insert({E_LiveObjState::Walk, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "move.png", move_frames, ms_speed)});
spr_states.insert({E_LiveObjState::Attack, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "attack.png", attack_frames, ms_speed)});
spr_states.insert({E_LiveObjState::TakeHit, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "take_hit.png", take_hit_frames)});
spr_states.insert({E_LiveObjState::Idle, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "idle.png", idle_frames)});
//SetCurSprite();
}

Expand Down Expand Up @@ -75,8 +76,6 @@ struct LiveObjSprite

bool SetState(E_LiveObjState new_state)
{
if (lo_type == E_LiveObjType::Enemy && new_state == E_LiveObjState::Walk)new_state = E_LiveObjState::Idle;

if (cur_state != new_state) {
cur_state = new_state;
SpritePrepare();
Expand Down
9 changes: 8 additions & 1 deletion SpriteManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ struct SpriteManager
private:
std::vector<Image> doors;

static constexpr int e_idle_frames(int type)
{
if (type == 0)return 8;
if (type == 1)return 4;
return 1;
}
static constexpr int e_get_move_frames(int type)
{
if (type == 0)return 8;
Expand Down Expand Up @@ -63,7 +69,8 @@ struct SpriteManager
for (int _type = 0; _type < 2; _type++)
enemy_spr.emplace_back(E_LiveObjType::Enemy, _type,
e_get_move_frames(_type), e_get_attack_frames(_type),
e_get_take_hit_frames(_type), e_get_spr_speed(_type));
e_get_take_hit_frames(_type), e_idle_frames(_type),
e_get_spr_speed(_type));

int sz = HP_SZ;
Image hp_img {sz,sz,4};
Expand Down
Binary file added resources/enemies/0/idle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/enemies/1/idle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a6b36fc

Please sign in to comment.