Skip to content

Commit

Permalink
dif anim start time
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 3, 2021
1 parent 48e9556 commit aa975cf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
24 changes: 18 additions & 6 deletions Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Enemy::Enemy(Point center_pos, int _type) : type(_type), mov(center_pos)
speed = get_speed(_type);
cur_dir = std::rand() % 2 ? E_Dir::LEFT : E_Dir::RIGHT;
cur_state = E_LiveObjState::Idle;
time_start += (std::rand() % 1000) / 1000.0;
}

void Enemy::Draw(Image &canvas)
Expand All @@ -27,7 +28,8 @@ void Enemy::Draw(Image &canvas)
if (cur_state == E_LiveObjState::Attack && GameTime::Now().TimeCome(attack_end_time)) {
//TODO:check player pos
attack_cd = true;
attack_cd_time = GameTime::Now().GetTime() + get_cd(type);
time_start = GameTime::Now().GetTime();
attack_cd_time = time_start + get_cd(type);
cur_state = E_LiveObjState::Idle;
}

Expand All @@ -40,7 +42,7 @@ void Enemy::Draw(Image &canvas)
SpriteManager::Get().DrawHpBlock(canvas, {.x = hp_x + i * 12, .y = p.y - sz.h / 2 - 5}, hp - i * 10);
}

SpriteManager::Get().enemy_spr[type].Draw(canvas, {.x = p.x - sz.w / 2, .y = p.y - sz.h / 2}, cur_state, cur_dir);
SpriteManager::Get().enemy_spr[type].Draw(canvas, {.x = p.x - sz.w / 2, .y = p.y - sz.h / 2}, cur_state, cur_dir, time_start);
}

bool Enemy::IsCollide(Point pos, int border_sz)
Expand All @@ -61,7 +63,8 @@ bool Enemy::WasAttacked(int dmg)
hp -= dmg;
if (cur_state != E_LiveObjState::Attack) {
cur_state = E_LiveObjState::TakeHit;
hit_take_time = GameTime::Now().GetTime() + SpriteManager::Get().enemy_spr[type].GetAnimTime(cur_state);
time_start = GameTime::Now().GetTime();
hit_take_time = time_start + SpriteManager::Get().enemy_spr[type].GetAnimTime(cur_state);
}
return hp <= 0;
}
Expand Down Expand Up @@ -105,13 +108,22 @@ void Enemy::Move(Point player_pos)
time_when_try_attack = get_time_before_attack(type) + GameTime::Now().GetTime();
}
} else {
if (GameTime::Now().TimeCome(time_when_try_attack)) {
if (attack_cd && GameTime::Now().TimeCome(attack_cd_time)) {
attack_cd = false;
/*auto can_a = fn_can_attack(pl, pos);
if (can_a.first) {
walk_to_player = true;
time_when_try_attack = GameTime::Now().GetTime();
} else walk_to_player = false;*/
}
else if (!attack_cd && GameTime::Now().TimeCome(time_when_try_attack)) {
auto can_a = fn_can_attack(pl, pos);
if (can_a.first) {
attack_dir = can_a.second;
cur_state = E_LiveObjState::Attack;
attack_end_time = GameTime::Now().GetTime() + SpriteManager::Get().enemy_spr[type].GetAnimTime(cur_state);
}
time_start = GameTime::Now().GetTime();
attack_end_time = time_start + SpriteManager::Get().enemy_spr[type].GetAnimTime(cur_state);
} else walk_to_player = false;
}
}

Expand Down
4 changes: 3 additions & 1 deletion Enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Enemy
static constexpr double get_time_before_attack(int type)
{
if (type == 0)return 0.3;
if (type == 1)return 0.9;
if (type == 1)return 3;// 0.9;
return 0.5;
}
static constexpr bool is_fly(int type)
Expand Down Expand Up @@ -93,5 +93,7 @@ struct Enemy
bool walk_to_player = false;
E_Dir attack_dir = E_Dir::DOWN;

double time_start = 0;

};
#endif
6 changes: 6 additions & 0 deletions LiveObjSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ struct LiveObjSprite
spr_states.find(state)->second.find(E_Dir::RIGHT)->second.Draw(canvas, p, flip_OX, flip_OY ^ (dir == E_Dir::LEFT ? true : false));
}

void Draw(Image &canvas, const Point p, E_LiveObjState state, E_Dir dir, double start_time, bool flip_OX = true, bool flip_OY = false)
{
if (lo_type == E_LiveObjType::Character)error("not for character");
spr_states.find(state)->second.find(E_Dir::RIGHT)->second.Draw(canvas, p, start_time, flip_OX, flip_OY ^ (dir == E_Dir::LEFT ? true : false));
}

const Image& GetImage(E_LiveObjState state, E_Dir dir, int frame) const {return spr_states.find(state)->second.find(dir)->second.GetFrame(frame);}
const Image& GetImage(E_LiveObjState state, int frame) const {return spr_states.find(state)->second.find(cur_dir)->second.GetFrame(frame);}

Expand Down
8 changes: 8 additions & 0 deletions Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ Sprite::Sprite(const std::string &path, SpritePixSz frame_sz, int ms_on_frame, i
SpriteFromImg(img0, p_frames, ms_on_frame, scale);
}

void Sprite::Draw(Image &canvas, const Point p, double time_start, bool flip_OX, bool flip_OY)
{
if (frames == 0)return;
double dt = GameTime::Now().GetSecAfter(time_start);
if (dt < 0)dt = 0;
int _frame = (int)(dt / s_per_frame);
imgs[_frame % frames].Draw(canvas, p, flip_OX, flip_OY);
}

void Sprite::Draw(Image &canvas, const Point p, bool flip_OX, bool flip_OY)
{
Expand Down
1 change: 1 addition & 0 deletions Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Sprite
Sprite(const std::string &path, SpritePixSz frame_sz, int ms_on_frame = 125, int scale = 1, bool _loop = true);

void Draw(Image &canvas, const Point p, bool flip_OX = false, bool flip_OY = false);
void Draw(Image &canvas, const Point p, double time_start, bool flip_OX = false, bool flip_OY = false);

void Restart() { time_start_prev_frame = GameTime::Now().GetTime(); frame_now = 0; }

Expand Down
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <ctime>

constexpr GLsizei WINDOW_WIDTH = W_WIDTH, WINDOW_HEIGHT = W_HEIGHT;

Expand Down Expand Up @@ -219,12 +220,12 @@ int main(int argc, char** argv)
while (gl_error != GL_NO_ERROR)
gl_error = glGetError();

std::srand(std::time(nullptr));

GameMap gmap{};

LiveObjSprite player_img {HERO_0, SpritePixSz{16}, 125, 2};
Player player {gmap.GetPos(GameMap::E_MapPos::Center, Size{.w = 30, .h = 32}), player_img
};
Player player {gmap.GetPos(GameMap::E_MapPos::Center, Size{.w = 30, .h = 32}), player_img};

//Image img(HERO_0 + "Walk/down.png");
/*
Expand Down

0 comments on commit aa975cf

Please sign in to comment.