diff --git a/Enemy.cpp b/Enemy.cpp index 704bbe1..36f371d 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -1,14 +1,15 @@ #include "Enemy.h" -Enemy::Enemy(Point center_pos, int _type) : spr(E_LiveObjType::Enemy, _type, get_move_frames(_type), get_attack_frames(_type)), mov(center_pos) +Enemy::Enemy(Point center_pos, int _type) : type(_type), mov(center_pos) { hp = get_hp(_type); sz = get_sz(_type); speed = get_speed(_type); + cur_dir = std::rand() % 2 ? E_Dir::LEFT : E_Dir::RIGHT; } void Enemy::Draw(Image &canvas) { auto p = mov.Pos(); - spr.Draw(canvas, {.x = p.x - sz.w / 2, .y = p.y - sz.h / 2}); + SpriteManager::Get().enemy_spr[type].Draw(canvas, {.x = p.x - sz.w / 2, .y = p.y - sz.h / 2}, cur_dir); } \ No newline at end of file diff --git a/Enemy.h b/Enemy.h index ae42b04..62a7f18 100644 --- a/Enemy.h +++ b/Enemy.h @@ -1,22 +1,12 @@ #ifndef MAIN_I3z_ENE_H #define MAIN_I3z_ENE_H -#include "LiveObjSprite.h" +#include "SpriteManager.h" #include "Movement.h" struct Enemy { private: - static constexpr int get_move_frames(int type) - { - if (type == 0)return 8; - return 1; - } - static constexpr int get_attack_frames(int type) - { - if (type == 0)return 8; - return 1; - } static constexpr int get_hp(int type) { if (type == 0)return 20; @@ -40,8 +30,10 @@ struct Enemy Size sz {.w = 0,.h = 0}; int hp; Movement mov; - LiveObjSprite spr; int speed = 50; + int type; + + E_Dir cur_dir; }; #endif \ No newline at end of file diff --git a/LiveObjSprite.h b/LiveObjSprite.h index 91a145f..8103609 100644 --- a/LiveObjSprite.h +++ b/LiveObjSprite.h @@ -88,11 +88,18 @@ struct LiveObjSprite void Draw(Image &canvas, const Point p, bool flip_OX = true, bool flip_OY = false) { - if (cur_spr == nullptr)SpritePrepare(); + if (cur_spr == nullptr)SpritePrepare(); if(lo_type == E_LiveObjType::Character)cur_spr->Draw(canvas, p, flip_OX, flip_OY); else cur_spr->Draw(canvas, p, flip_OX, flip_OY ^ (cur_dir == E_Dir::LEFT ? true : false)); } + void Draw(Image &canvas, const Point p, E_Dir dir, bool flip_OX = true, bool flip_OY = false) + { + if (cur_spr == nullptr)SpritePrepare(); + if (lo_type == E_LiveObjType::Character)error("not for character"); + else cur_spr->Draw(canvas, p, 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);} diff --git a/SpriteManager.h b/SpriteManager.h new file mode 100644 index 0000000..b3357fc --- /dev/null +++ b/SpriteManager.h @@ -0,0 +1,31 @@ +#ifndef MAIN_SPMz_ENE_H +#define MAIN_SPMz_ENE_H + +#include "LiveObjSprite.h" + +struct SpriteManager +{ + static SpriteManager &Get() { static SpriteManager spr {}; return spr; } + + std::vector enemy_spr; + +private: + static constexpr int e_get_move_frames(int type) + { + if (type == 0)return 8; + return 1; + } + static constexpr int e_get_attack_frames(int type) + { + if (type == 0)return 8; + return 1; + } + SpriteManager() : enemy_spr() + { + for (int _type = 0; _type < 1; _type++) + enemy_spr.emplace_back(E_LiveObjType::Enemy, _type, e_get_move_frames(_type), e_get_attack_frames(_type)); + } + +}; + +#endif \ No newline at end of file