Skip to content

Commit

Permalink
enemies started
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 2, 2021
1 parent da84e12 commit 1996547
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 69 deletions.
14 changes: 14 additions & 0 deletions Enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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)
{
hp = get_hp(_type);
sz = get_sz(_type);
speed = get_speed(_type);
}

void Enemy::Draw(Image &canvas)
{
auto p = mov.Pos();
spr.Draw(canvas, {.x = p.x - sz.w / 2, .y = p.y - sz.h / 2});
}
47 changes: 47 additions & 0 deletions Enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef MAIN_I3z_ENE_H
#define MAIN_I3z_ENE_H

#include "LiveObjSprite.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;
return 35;
}
static constexpr Size get_sz(int type)
{
if (type == 0)return Size {.w = 41, .h = 34};
return Size {2,2};
}
static constexpr int get_speed(int type)
{
if (type == 0)return 160;
return 80;
}
public:
Enemy(Point center_pos, int _type);

void Draw(Image &canvas);

Size sz {.w = 0,.h = 0};
int hp;
Movement mov;
LiveObjSprite spr;
int speed = 50;

};
#endif
3 changes: 3 additions & 0 deletions GameMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void GameMap::Draw(Image &canvas)
now_room->room_holst.FastDraw(canvas, now_room->room_holst.Height());

now_room->map_objects.DrawItems(canvas);
now_room->map_objects.DrawEnemies(canvas);
}

GameMap::GameRoom::GameRoom(GameMap::GameRoomInfo &_gri, GameMap &_parent) : gri(_gri), parent(_parent), map_objects(_parent),
Expand Down Expand Up @@ -108,6 +109,7 @@ GameMap::GameRoom::GameRoom(GameMap::GameRoomInfo &_gri, GameMap &_parent) : gri

for (int i = 0; i < gri.keys_pos.size(); i++)map_objects.AddKey(gri.keys_pos[i]);
for (int i = 0; i < gri.items.size(); i++)map_objects.AddItem(gri.items[i].first, gri.items[i].second);
for (int i = 0; i < gri.enemies.size(); i++)map_objects.AddEnemy(gri.enemies[i].first, gri.enemies[i].second);

}

Expand Down Expand Up @@ -217,4 +219,5 @@ GameMap::GameRoomInfo::GameRoomInfo(char room_type)

for (int i = 0; i < keys_pos.size(); i++)keys_pos[i].y = map_height - 1 - keys_pos[i].y;
for (int i = 0; i < items.size(); i++)items[i].first.y = map_height - 1 - items[i].first.y;
for (int i = 0; i < enemies.size(); i++)enemies[i].first.y = map_height - 1 - enemies[i].first.y;
}
5 changes: 5 additions & 0 deletions GameMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@

struct GameMap;
#include "Item.h"
#include "Enemy.h"

struct MapObj
{
MapObj(GameMap &_parent) : items(), parent(_parent) {};
//void AddItem(Point pos, int rarity);
void AddKey(Point pos);
void AddItem(Point pos, int lvl);
void AddEnemy(Point pos, int type);

void PressE();

void DrawItems(Image &canvas);
void DrawEnemies(Image &canvas);
std::vector<Item> &GetItems() { return items; }
private:
std::vector<Item> items {};
std::vector<Enemy> enemies {};
GameMap &parent;
int ind_E = -1;
};
Expand Down
11 changes: 11 additions & 0 deletions General.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ inline bool operator== (const Point a, const Point b)
return (a.x == b.x) && (a.y == b.y);
}

inline Point operator* (const Point a, const int mul)
{
return {a.x * mul, a.y * mul};
}

inline Point operator+ (const Point a, const Point b)
{
return {a.x + b.x, a.y + b.y};
}
struct Size
{
int w;
Expand All @@ -34,6 +43,8 @@ struct Size
};


static constexpr Point TILE_2 {TILE_SZ / 2 , TILE_SZ / 2};

struct GameTime
{
//explicit GameTime(double time) : cur_sec(time) {};
Expand Down
43 changes: 38 additions & 5 deletions LiveObjSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,26 @@ enum class E_LiveObjState
Attack
};

enum class E_LiveObjType
{
Character,
Enemy
};

struct LiveObjSprite
{

LiveObjSprite(E_LiveObjType must_enemy, int type, int move_frames, int attack_frames) : 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)});
spr_states.insert({E_LiveObjState::Attack, std::map<E_Dir, Sprite>{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "attack.png", attack_frames)});
//SetCurSprite();
}

LiveObjSprite(const std::string &path0, SpritePixSz spr_sz, int ms_on_frame = 125, int scale = 1) : spr_states()
{
E_LiveObjState lo_states[] {E_LiveObjState::Idle, E_LiveObjState::Walk, E_LiveObjState::Attack};
Expand All @@ -38,16 +55,26 @@ struct LiveObjSprite

bool SetDir(E_Dir new_dir)
{
if (cur_dir != new_dir) {
if (lo_type == E_LiveObjType::Character) {
if (cur_dir != new_dir) {
cur_dir = new_dir;
SpritePrepare();
return true;
}
return false;
} else {
if (new_dir == E_Dir::UP || new_dir == E_Dir::DOWN)return false;
if (new_dir == cur_dir)return false;
cur_dir = new_dir;
SpritePrepare();
return true;
}
return false;

}

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 All @@ -61,7 +88,9 @@ struct LiveObjSprite

void Draw(Image &canvas, const Point p, bool flip_OX = true, bool flip_OY = false)
{
cur_spr->Draw(canvas, p, flip_OX, flip_OY);
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));
}

const Image& GetImage(E_LiveObjState state, E_Dir dir, int frame) const {return spr_states.find(state)->second.find(dir)->second.GetFrame(frame);}
Expand All @@ -76,7 +105,9 @@ struct LiveObjSprite

inline void SetCurSprite()
{
cur_spr = &spr_states.find(cur_state)->second.find(cur_dir)->second;
if(lo_type == E_LiveObjType::Character) cur_spr = &spr_states.find(cur_state)->second.find(cur_dir)->second;
else cur_spr = &spr_states.find(cur_state)->second.find(E_Dir::RIGHT)->second;

if (cur_spr == nullptr) {
cur_spr = &spr_states.find(E_LiveObjState::Idle)->second.find(cur_dir)->second;
}
Expand All @@ -87,6 +118,8 @@ struct LiveObjSprite
E_LiveObjState cur_state = E_LiveObjState::Idle;

Sprite *cur_spr = nullptr;

E_LiveObjType lo_type = E_LiveObjType::Character;
};

#endif
8 changes: 8 additions & 0 deletions MapObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

void MapObj::AddKey(Point pos){items.emplace_back(E_ItemTypes::Key, -1, pos);}
void MapObj::AddItem(Point pos, int lvl){items.emplace_back(lvl, pos);}
void MapObj::AddEnemy(Point pos, int type) { enemies.emplace_back(pos * TILE_SZ + TILE_2, type); }

void MapObj::DrawItems(Image &canvas)
{
Expand Down Expand Up @@ -41,6 +42,13 @@ void MapObj::DrawItems(Image &canvas)
ind_E = ind_e;
}


void MapObj::DrawEnemies(Image &canvas)
{
for (int i = 0; i < enemies.size(); i++)
enemies[i].Draw(canvas);
}

void MapObj::PressE()
{
if (ind_E < 0)return;
Expand Down
65 changes: 65 additions & 0 deletions Movement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "GameMap.h"
#include "Movement.h"

void Movement:: Move(int dx, int dy, int speed)
{
double now_t = GameTime::Now().GetTime();

if (dx || dy) {
double dt = now_t - last_time;
if (obj_size.IsEmpty()) {
x += dt * dx * speed;
y += dt * dy * speed;
last_moved_time = now_t;
moved = true;
} else {

bool move_x = false;
if (dx) {
double x_back = x;
double x_temp = x + dt * dx * speed;

for (; (dx > 0) ? (x_temp > x_back) : (x_temp < x_back); x_temp += (dx > 0) ? -1 : 1) {
bool can_stay = GameMap::GetCur()->CanStay(
(dx > 0) ? GameMap::E_CanStayType::Right : GameMap::E_CanStayType::Left,
Point {.x = (int)x_temp, .y = (int)y}, obj_size, can_stay_on_empty
);
if (can_stay) {
move_x = true;
break;
}
}

if (move_x)x = x_temp;
}

bool move_y = false;
if (dy) {
double y_back = y;
double y_temp = y + dt * dy * speed;

for (; (dy > 0) ? (y_temp > y_back) : (y_temp < y_back); y_temp += (dy > 0) ? -1 : 1) {
bool can_stay = GameMap::GetCur()->CanStay(
(dy > 0) ? GameMap::E_CanStayType::Up : GameMap::E_CanStayType::Down,
Point {.x = (int)x, .y = (int)y_temp}, obj_size, can_stay_on_empty
);
if (can_stay) {
move_y = true;
break;
}
}

if (move_y)y = y_temp;
}

if (move_x || move_y) {
last_moved_time = now_t;
moved = true;
}

}

}

last_time = now_t;
}
64 changes: 1 addition & 63 deletions Movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define MAIN_MVMNT_H

#include "General.h"
#include "GameMap.h"

struct Movement
{
Expand All @@ -14,68 +13,7 @@ struct Movement

void UpdateLastTime() { last_time = GameTime::Now().GetTime(); }

void Move(int dx, int dy, int speed)
{
double now_t = GameTime::Now().GetTime();

if (dx || dy) {
double dt = now_t - last_time;
if (obj_size.IsEmpty()) {
x += dt * dx * speed;
y += dt * dy * speed;
last_moved_time = now_t;
moved = true;
} else {

bool move_x = false;
if (dx) {
double x_back = x;
double x_temp = x + dt * dx * speed;

for (; (dx > 0) ? (x_temp > x_back) : (x_temp < x_back); x_temp += (dx > 0) ? -1 : 1) {
bool can_stay = GameMap::GetCur()->CanStay(
(dx > 0) ? GameMap::E_CanStayType::Right : GameMap::E_CanStayType::Left,
Point {.x = (int)x_temp, .y = (int)y}, obj_size, can_stay_on_empty
);
if (can_stay) {
move_x = true;
break;
}
}

if (move_x)x = x_temp;
}

bool move_y = false;
if (dy) {
double y_back = y;
double y_temp = y + dt * dy * speed;

for (; (dy > 0) ? (y_temp > y_back) : (y_temp < y_back); y_temp += (dy > 0) ? -1 : 1) {
bool can_stay = GameMap::GetCur()->CanStay(
(dy > 0) ? GameMap::E_CanStayType::Up : GameMap::E_CanStayType::Down,
Point {.x = (int)x, .y = (int)y_temp}, obj_size, can_stay_on_empty
);
if (can_stay) {
move_y = true;
break;
}
}

if (move_y)y = y_temp;
}

if (move_x || move_y) {
last_moved_time = now_t;
moved = true;
}

}

}

last_time = now_t;
}
void Move(int dx, int dy, int speed);

Point Pos() const { return {(int)x, (int)y}; }
Point CenterPos() const { return {(int)(x + obj_size.w/2), (int)(y + obj_size.h/2)}; }
Expand Down
1 change: 1 addition & 0 deletions Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "LiveObjSprite.h"
#include "General.h"
#include "Movement.h"
#include "GameMap.h"

struct Player
{
Expand Down
Loading

0 comments on commit 1996547

Please sign in to comment.