-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapObj.cpp
130 lines (113 loc) · 4.04 KB
/
MapObj.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "GameMap.h"
#include "Player.h"
#include <iterator>
#include <utility>
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::EnemiesMove(Point player_pos) { for (int i = 0; i < enemies.size(); i++)enemies[i].Move(player_pos); }
void MapObj::AfterLoad(int tile_w, int tile_h)
{
for (int i = 0; i < enemies.size(); i++) {
enemies[i].mov.UpdateLastTime();
auto pos = enemies[i].mov.CenterPos();
bool change_pos = false;
if (pos.x < TILE_SZ) { pos.x += TILE_SZ; change_pos = true; }
if (pos.y < TILE_SZ) { pos.y += TILE_SZ; change_pos = true; }
if (pos.x / TILE_SZ >= tile_w - 1) { pos.x -= TILE_SZ; change_pos = true; }
if (pos.y / TILE_SZ >= tile_h - 1) { pos.y -= TILE_SZ; change_pos = true; }
if (change_pos)enemies[i].mov.SetCenter(pos);
}
ind_E = -1;
ind_D = -1;
}
void MapObj::DrawItems(Image &canvas)
{
E_Dir pl_dir = Player::Get().GetCurDir();
auto pl_c = Player::Get().GetCenter();
int ind_e = -1;
int ind_d = -1;
Point pl_check_min {0,0};
Size pl_check_sz {15,15};
switch (pl_dir) {
case E_Dir::UP: pl_check_min = {.x = pl_c.x - 10, .y = pl_c.y - 12}; pl_check_sz.w += 5; break;
case E_Dir::DOWN: pl_check_min = {.x = pl_c.x - 10, .y = pl_c.y - 16}; pl_check_sz.w += 5; break;
case E_Dir::LEFT: pl_check_min = {.x = pl_c.x - 15, .y = pl_c.y - 10}; pl_check_sz.h += 5; break;
case E_Dir::RIGHT: pl_check_min = {.x = pl_c.x + 5, .y = pl_c.y - 10}; pl_check_sz.h += 5; break;
default:break;
}
Point pl_check_door_min = pl_check_min;
if (pl_dir == E_Dir::UP)pl_check_door_min.y += 24;
bool check_points = true;
bool player_have_key = Player::Get().HaveKey();
for (int i = 0; i < doors.size(); i++) {
auto &door= doors[i];
if (player_have_key && check_points) {
if (door.IsRectIn(pl_check_door_min, pl_check_sz)) {
parent.Draw_E(canvas, door.pos);
check_points = false;
ind_d = i;
}
}
if(!door.open)SpriteManager::Get().DrawDoor(canvas, door.pos, door.dir);
}
for (int i = 0; i < items.size(); i++) {
auto &itm = items[i];
if (check_points) {
if (itm.IsRectIn(pl_check_min, pl_check_sz) && itm.ict()) {
parent.Draw_E(canvas, itm.pos);
check_points = false;
ind_e = i;
}
}
parent.DrawOn<Sprite>(itm.spr, itm.pos, canvas);
}
ind_E = ind_e;
ind_D = ind_d;
}
void MapObj::DrawEnemies(Image &canvas)
{
for (int i = 0; i < enemies.size(); i++)
enemies[i].Draw(canvas);
}
void MapObj::PressE()
{
if (ind_D != -1) {
if (Player::Get().HaveKey())Player::Get().KeyDec();
doors[ind_D].open = true;
ind_D = -1;
ind_E = -1;
return;
}
if (ind_E < 0)return;
auto &itm = items[ind_E];
if (!itm.ict())return;
itm.ia(true);
if (itm.inventory) {
auto &inv = Player::Get().GetInv();
//inv.insert(inv.end(), std::make_move_iterator(items.begin() + ind_E), std::make_move_iterator(items.begin() + ind_E + 1));
inv.emplace_back(std::move(itm));
items.erase(items.begin() + ind_E);
} else {
items.erase(items.begin() + ind_E);
}
ind_E = -1;
}
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].id != enemy_id && enemies[i].IsCollide(pos, 5))return false;
return true;
}
bool MapObj::TryAttack(Point pos, int dmg)
{
for (int i = 0; i < enemies.size(); i++) {
auto &e = enemies[i];
if (e.IsCollide(pos, 0)) {
e.WasAttacked(dmg);//die
return true;
}
}
return false;
}