Skip to content

Commit

Permalink
create sprite manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 2, 2021
1 parent 1996547 commit 9dcdb2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Enemy.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
16 changes: 4 additions & 12 deletions Enemy.h
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
9 changes: 8 additions & 1 deletion LiveObjSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);}

Expand Down
31 changes: 31 additions & 0 deletions SpriteManager.h
Original file line number Diff line number Diff line change
@@ -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<LiveObjSprite> 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

0 comments on commit 9dcdb2a

Please sign in to comment.