Skip to content

Commit

Permalink
+ enemies move
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Mar 3, 2021
1 parent 770c090 commit d38c445
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 25 deletions.
31 changes: 28 additions & 3 deletions Enemy.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "Enemy.h"

Enemy::Enemy(Point center_pos, int _type) : type(_type), mov(center_pos + Point{.x = 0, .y = 5})
Enemy::Enemy(Point center_pos, int _type) : type(_type), mov(center_pos)
{
if (is_fly(type))mov.SetCanStay(true);
mov.SetSize(get_mov_sz(_type));
id = mov.GetId();
max_hp = get_hp(_type);
hp = max_hp;
sz = get_sz(_type);
Expand All @@ -13,7 +16,8 @@ Enemy::Enemy(Point center_pos, int _type) : type(_type), mov(center_pos + Point{
void Enemy::Draw(Image &canvas)
{
if (!alive)return;
auto p = mov.Pos();
auto p = mov.CenterPos();
p.y += 6;
if (cur_state == E_LiveObjState::TakeHit && GameTime::Now().TimeCome(hit_take_time)) {
cur_state = E_LiveObjState::Idle;
if (hp <= 0)alive = false;
Expand All @@ -30,7 +34,7 @@ void Enemy::Draw(Image &canvas)
bool Enemy::IsCollide(Point pos, int border_sz)
{
if (!alive)return false;
auto p = mov.Pos();
auto p = mov.CenterPos();
if (pos.x <= p.x - sz.w/2 + border_sz)return false;
if (pos.x >= p.x + sz.w/2 - border_sz)return false;
if (pos.y <= p.y - sz.h/2 + border_sz)return false;
Expand All @@ -47,3 +51,24 @@ bool Enemy::WasAttacked(int dmg)
hit_take_time = GameTime::Now().GetTime() + SpriteManager::Get().enemy_spr[type].GetAnimTime(cur_state);
return hp <= 0;
}

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) ||
(player_pos.x - r * TILE_SZ > pos.x) ||
(player_pos.y + r * TILE_SZ < pos.y) ||
(player_pos.y - r * TILE_SZ > pos.y)) {
mov.UpdateLastTime();
return;
}
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;

mov.Move(x_eq ? 0 : pl_x_less ? -1 : 1, y_eq ? 0 : pl_y_less ? -1 : 1, speed);
}
19 changes: 18 additions & 1 deletion Enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ struct Enemy
if (type == 1)return 40;
return 30;
}
static constexpr bool is_fly(int type)
{
if (type == 0)return true;
return false;
}
static constexpr Size get_sz(int type)
{
if (type == 0)return Size {.w = 41, .h = 34};
if (type == 1)return Size {.w = 28, .h = 38};
return Size {2,2};
}
static constexpr Size get_mov_sz(int type)
{
return Size {.w = 26, .h = 26};
}
static constexpr int get_speed(int type)
{
if (type == 0)return 160;
if (type == 1)return 80;
return 80;
}
static constexpr int get_r(int type)
{
if (type == 0)return 5;
if (type == 1)return 3;
return 4;
}

public:
Enemy(Point center_pos, int _type);
Expand All @@ -33,7 +48,9 @@ struct Enemy

void Draw(Image &canvas);
bool WasAttacked(int dmg); // return was die?

void Move(Point player_pos);

int id;
Size sz {.w = 0,.h = 0};
int hp;
int max_hp;
Expand Down
13 changes: 13 additions & 0 deletions GameMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ Point GameMap::GetPos(E_MapPos map_pos, Size obj_sz)
}
}

bool GameMap::CanPointStay(Point pos, bool empty_can, int enemy_id) const
{
auto t_type = now_room->gri.TileType(pos.x / TILE_SZ, pos.y / TILE_SZ);
if (t_type == GameMap::E_TileType::Wall)return false;
if (!empty_can && t_type == GameMap::E_TileType::Empty)return false;
if (enemy_id != Movement::PlayerId) {
Point cp = Player::Get().GetCenter();
if ((cp.x - 16 < pos.x && pos.x < cp.x + 16) &&
(cp.y - 16 < pos.y && pos.y < cp.y + 16))return false;
}
if (!now_room->map_objects.CanStay(pos, enemy_id))return false;
return true;
}

bool GameMap::CanThrowItem(Point pos)
{
Expand Down
23 changes: 9 additions & 14 deletions GameMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ struct MapObj

void PressE();

void EnemiesMove(Point player_pos);
void DrawItems(Image &canvas);
void DrawEnemies(Image &canvas);
std::vector<Item> &GetItems() { return items; }

bool CanStay(Point pos);
bool CanStay(Point pos, int enemy_id);

bool TryAttack(Point pos, int dmg);
private:
Expand Down Expand Up @@ -96,8 +97,9 @@ private: static GameMap *cur_map;
GameMap();

bool CheckChangeMap();
void EnemiesMove(Point player_pos) { now_room->map_objects.EnemiesMove(player_pos); }

bool CanStay(E_CanStayType stay_type, Point pos, Size obj_sz, bool empty_can)
bool CanStay(E_CanStayType stay_type, Point pos, Size obj_sz, bool empty_can, int enemy_id)
{
Point LD {.x = pos.x, .y = pos.y};
Point LU {.x = pos.x, .y = pos.y + obj_sz.h - 1};
Expand All @@ -111,25 +113,18 @@ private: static GameMap *cur_map;

switch (stay_type) {
case GameMap::E_CanStayType::Right:
return CanPointStay(RU, empty_can) && CanPointStay(RD, empty_can) && now_room->map_objects.CanStay(R_UD);
return CanPointStay(RU, empty_can, enemy_id) && CanPointStay(RD, empty_can, enemy_id) && now_room->map_objects.CanStay(R_UD, enemy_id);
case GameMap::E_CanStayType::Left:
return CanPointStay(LD, empty_can) && CanPointStay(LU, empty_can) && now_room->map_objects.CanStay(L_UD);
return CanPointStay(LD, empty_can, enemy_id) && CanPointStay(LU, empty_can, enemy_id) && now_room->map_objects.CanStay(L_UD, enemy_id);
case GameMap::E_CanStayType::Up:
return CanPointStay(LU, empty_can) && CanPointStay(RU, empty_can) && now_room->map_objects.CanStay(LR_U);
return CanPointStay(LU, empty_can, enemy_id) && CanPointStay(RU, empty_can, enemy_id) && now_room->map_objects.CanStay(LR_U, enemy_id);
case GameMap::E_CanStayType::Down:
return CanPointStay(LD, empty_can) && CanPointStay(RD, empty_can) && now_room->map_objects.CanStay(LR_D);
return CanPointStay(LD, empty_can, enemy_id) && CanPointStay(RD, empty_can, enemy_id) && now_room->map_objects.CanStay(LR_D, enemy_id);
default: error("wrong enum state"); return false;
}
}

bool CanPointStay(Point pos, bool empty_can) const
{
auto t_type = now_room->gri.TileType(pos.x / TILE_SZ, pos.y / TILE_SZ);
if (t_type == GameMap::E_TileType::Wall)return false;
if (!empty_can && t_type == GameMap::E_TileType::Empty)return false;
if (!now_room->map_objects.CanStay(pos))return false;
return true;
}
bool CanPointStay(Point pos, bool empty_can, int enemy_id) const;

GameMap::E_TileType PointType(Point pos) const { return now_room->gri.TileType(pos.x / TILE_SZ, pos.y / TILE_SZ); }

Expand Down
11 changes: 8 additions & 3 deletions MapObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

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::AddEnemy(Point pos, int type) { enemies.emplace_back(pos * TILE_SZ /*+ TILE_2*/, type); }

void MapObj::EnemiesMove(Point player_pos)
{
for (int i = 0; i < enemies.size(); i++)enemies[i].Move(player_pos);
}

void MapObj::DrawItems(Image &canvas)
{
Expand Down Expand Up @@ -90,12 +95,12 @@ void MapObj::PressE()
ind_E = -1;
}

bool MapObj::CanStay(Point pos)
bool MapObj::CanStay(Point pos, int enemy_id)
{
for (int i = 0; i < doors.size(); i++)
if (!doors[i].open && (doors[i].pos == pos / TILE_SZ))return false;

for (int i = 0; i < enemies.size(); i++)if (enemies[i].IsCollide(pos, 5))return false;
for (int i = 0; i < enemies.size(); i++)if (enemies[i].id != enemy_id && enemies[i].IsCollide(pos, 5))return false;
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions Movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void Movement:: Move(int dx, int dy, int 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
Point {.x = (int)x_temp, .y = (int)y}, obj_size, can_stay_on_empty, Id
);
if (can_stay) {
move_x = true;
Expand All @@ -41,7 +41,7 @@ void Movement:: Move(int dx, int dy, int 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
Point {.x = (int)x, .y = (int)y_temp}, obj_size, can_stay_on_empty, Id
);
if (can_stay) {
move_y = true;
Expand Down
10 changes: 8 additions & 2 deletions Movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

struct Movement
{
Movement(int _x, int _y) : x(_x), y(_y) { last_time = GameTime::Now().GetTime(); }
Movement(Point pos) : Movement(pos.x, pos.y) { }
static constexpr int PlayerId = -5;
Movement(int _x, int _y) : x(_x), y(_y) { last_time = GameTime::Now().GetTime(); Id = Get__Id(); }
Movement(Point pos) : Movement(pos.x, pos.y) {}

void SetAsPlayer() { Id = PlayerId; }
void SetSize(Size obj_sz) { obj_size = obj_sz; }
void SetCanStay(bool on_empty) { can_stay_on_empty = on_empty; }

Expand All @@ -20,13 +22,17 @@ struct Movement

void SetPos(Point pos) { x = pos.x; y = pos.y; }

int GetId()const { return Id; }

bool Moved()
{
moved = moved && (GameTime::Now().GetSecAfter(last_moved_time) < time_for_unmoved);
return moved;
}

private:
static int Get__Id() { static int id = 1; return id++; }
int Id {0};
double last_time = 0;
double x;
double y;
Expand Down
1 change: 1 addition & 0 deletions Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private: static Player *player_getter;
{
position.SetSize(Size {.w = 29, .h = 29});
position.SetCanStay(true);
position.SetAsPlayer();
player_getter = this;
};

Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ int main(int argc, char** argv)
if (is_alive) {

gmap.CheckChangeMap();//TODO: add if(...) for effect
gmap.EnemiesMove(player.GetCenter());

gmap.Draw(screenBuffer);

Expand Down

0 comments on commit d38c445

Please sign in to comment.