-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.h
290 lines (239 loc) · 9.67 KB
/
Player.h
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef MAIN_PLAYER_H
#define MAIN_PLAYER_H
#include "LiveObjSprite.h"
#include "SpriteManager.h"
#include "General.h"
#include "Movement.h"
#include "GameMap.h"
struct Player
{
//explicit Player(Point pos = {.x = 10, .y = 10}) : coords(pos), old_coords(coords) {};
private: static Player *player_getter;
public:
enum class E_DieType
{
Kill,
EmptyStay,
};
static Player &Get() { return *player_getter; }
explicit Player(Point pos, LiveObjSprite &sprite) : position(pos), coords(pos), old_coords(pos), spr(sprite), grave("../resources/grave.png", 4, 150, 1, false)
{
position.SetSize(Size {.w = 29, .h = 29});
position.SetCanStay(true);
position.SetAsPlayer();
player_getter = this;
};
void Attack();
bool Moved();
void Draw(Image &screen);
void DieDraw(Image &canvas, double proc);
inline void RefreshMoveState(bool up, bool down, bool left, bool right, bool ctrl)
{
RefreshMoveState((left == right) ? E_X_Dir::Not : (left ? E_X_Dir::Left : E_X_Dir::Right),
(up == down) ? E_Y_Dir::Not : (up ? E_Y_Dir::Up : E_Y_Dir::Down), ctrl);
}
void RefreshMoveState(E_X_Dir x, E_Y_Dir y, bool ctrl);
void KeyInc() { invent.keys++; }
void KeyDec() { invent.keys--; }
bool HaveKey() { return 0 < invent.keys; }
bool CanTakeKey() const { return invent.keys < 3; }
bool IsPlaceForItem() const { return invent.inv_item.size() < invent.inv_size; }
std::vector<Item> &GetInv() { return invent.inv_item; }
int BootsLvl() const {
return invent.boots_lvl;
}
void TakeBoots(int lvl)
{
move_speed += lvl * 40 - invent.boots_lvl * 40;
invent.boots_lvl = lvl;
}
void CheckStayOnEmpty(Point cur_pos)
{
auto empty = GameMap::E_TileType::Empty;
if ((GameMap::GetCur()->PointType(Point {.x = cur_pos.x + 12, .y = cur_pos.y + 2}) == empty) &&
(GameMap::GetCur()->PointType(Point {.x = cur_pos.x + 19, .y = cur_pos.y + 2}) == empty)) {
die_pos = coords;
died = true;
die_type = E_DieType::EmptyStay;
}
}
bool PointIn(Point p, int border = 16)
{
auto cp = GetCenter();
return
(cp.x - border < p.x && p.x < cp.x + border) &&
(cp.y - border < p.y && p.y < cp.y + border);
}
E_Dir GetCurDir()const { return spr.GetCurDir(); }
Point GetCenter()const { return position.CenterPos(); }
Point GetPos()const { return coords; }
bool GetIsDied()const { return died; }
E_DieType GetDiedType()const { return die_type; }
void SetPosY(int y) { coords.y = y; }
void SetPos(Point pos) { position.SetPos(pos); coords = position.Pos(); }
void InventoryDraw(Image &canvas) { invent.Draw(canvas); }
void AddSpeed(int add) { move_speed += add; }
void HpRestore(int add) { hp += add; if (add > max_hp)hp = max_hp; }
void GetDamage(int dmg)
{
hp -= dmg;
if (hp <= 0) {
die_pos = coords;
died = true;
die_type = E_DieType::Kill;
}
}
void PressI() { invent.open = !invent.open; invent.inv_pos = -1; }
void PressLA() { invent.inv_pos = (invent.inv_pos == -1) ? invent.inv_size - 1 : (invent.inv_pos - 1 + invent.inv_size) % invent.inv_size; }
void PressRA() { invent.inv_pos = (invent.inv_pos + 1) % invent.inv_size; }
void PressU() { invent.Use(); }
void PressO() { invent.Throw(position.CenterPos()); }
void DamageChange(int delta) { damage += delta; }
void InventorySizeInc() { invent.SizeInc(); }
void MaxHpChange(int delta)
{
if (hp == max_hp) hp += delta;
else hp = (int)(hp * (1 + delta / (double)max_hp));
max_hp += delta;
if (hp <= 0)hp = 1;
}
std::pair<int, int> GetHp()const { return {hp, max_hp}; }
private:
struct Inventory
{
bool open = false;
int inv_pos = -1;
std::vector<Item> inv_item;
int boots_lvl = 0;
int armor_lvl = 0;
int keys = 0;
int inv_size = 3;
Inventory() : micro_inv_canvas(W_WIDTH, TILE_SZ, 4), inv_cell_canvas(W_WIDTH, TILE_SZ, 4),
micro_inv("../resources/micro_inv.png"), key("../resources/key.png"),
inv_cell("../resources/inv_cell.png"), inv_bg("../resources/inv_bg.png"),
inv_cell_active("../resources/inv_cell_now.png"), dmg_text_img("../resources/dmg__text.png")
{
for (int i = 0; i < (W_WIDTH + TILE_SZ - 1) / TILE_SZ; i++) {
micro_inv.Draw(micro_inv_canvas, {i * TILE_SZ, 0}, true);
inv_bg.Draw(inv_cell_canvas, {i * TILE_SZ, 0});
}
for (int i = 0; i < inv_size; i++) {
inv_cell.Draw(inv_cell_canvas, {i * TILE_SZ, 0});
}
auto shadow_func = [](auto x) { return (x.a == 255) ? SHADOW_COLOR : x; };
auto shadow_func_wh = [](auto x) { return (x.a == 0 || x.r == x.b && x.r == x.g && x.r == 255) ? TRANSP_COLOR : SHADOW_COLOR; };
//damage +++
dmg_text_img.PixelsChange(shadow_func_wh, false);
for (int i = 0; i < 3; i++) {
damage_img.emplace_back("../resources/dmg_"+std::to_string(i + 1) + ".png");
damage_img[i].PixelsChange(shadow_func_wh, false);
}
//damage ---
//boots +++
Image boots_shadow {"../resources/boots_3.png"};
boots_shadow.PixelsChange(shadow_func, false);
boots_imgs.push_back(boots_shadow);
for (int i = 0; i < 3; i++)boots_imgs.emplace_back("../resources/boots_" + std::to_string(i + 1) + ".png");
//boots ---
key_black = Image(key);
key_black.PixelsChange(shadow_func, false);
}
void SizeInc()
{
inv_cell.Draw(inv_cell_canvas, {inv_size * TILE_SZ, 0});
inv_size++;
}
void Use()
{
//Player::Get().GetDamage(5);//TODO:DEL
if (inv_pos < 0)return;
if (inv_item.size() <= inv_pos)return;
if (!inv_item[inv_pos].can_be_used)return;
inv_item[inv_pos].use();
inv_item.erase(inv_item.begin() + inv_pos);
inv_pos = -1;
}
void Throw(Point pos)
{
if (inv_pos < 0)return;
if (inv_item.size() <= inv_pos)return;
if (!GameMap::GetCur()->CanThrowItem(pos))return;
auto &itm = inv_item[inv_pos];
itm.pos = Point {pos.x / TILE_SZ, pos.y / TILE_SZ};
itm.ia(false);
GameMap::GetCur()->GetItems().push_back(std::move(itm));
inv_item.erase(inv_item.begin() + inv_pos);
inv_pos = -1;
}
void Draw(Image &canvas)
{
if (open) {
inv_cell_canvas.FastDraw(canvas, TILE_SZ, W_HEIGHT - TILE_SZ);
if (inv_pos != -1)inv_cell_active.Draw(canvas, {inv_pos * TILE_SZ + 4, Y + 4}, true);
for (int i = 0; i < inv_item.size(); i++) {
auto &spr = inv_item[i].spr;
spr.Draw(canvas, Point {i * TILE_SZ + (TILE_SZ - spr.Width()) / 2, Y + (TILE_SZ - spr.Height()) / 2}, true);
}
}
micro_inv_canvas.FastDraw(canvas, TILE_SZ, W_HEIGHT - TILE_SZ - (open ? TILE_SZ : 0));
int now_x_pos = 0;
int now_y_pos = Y + 5 - (open ? TILE_SZ : 0);
for (int i = 0; i < 3; i++) {
int p_draw = 10 + 40 * i;
const Image &k_draw = (i < keys) ? key : key_black;
k_draw.Draw(canvas, Point {p_draw, now_y_pos}, true);
now_x_pos = p_draw + 50;
}
boots_imgs[boots_lvl].Draw(canvas, Point {now_x_pos, now_y_pos + 3}, true);
now_x_pos += 50;
// damage +++
dmg_text_img.Draw(canvas, Point {now_x_pos, now_y_pos - 8 + dmg_text_img.Height() / 2}, true);
now_x_pos += dmg_text_img.Width() + 7;
for (int i = Player::Get().damage, dmg_dr = 0; (i > 0) && (dmg_dr < 7); i-=3, dmg_dr++) {
damage_img[(i > 3) ? 2 : i - 1].Draw(canvas, Point {now_x_pos, now_y_pos}, true);
now_x_pos += 42;
}
// damage ---
auto hp_info = Player::Get().GetHp();
for (int i = 0; i < hp_info.second / 10; i++) {
int px = W_WIDTH - 18 - (i / 2) * SpriteManager::HP_SZ * 3;
int py = now_y_pos + 2 + (i % 2) * 12;
SpriteManager::Get().DrawHpBlock(canvas, {px, py}, hp_info.first);
SpriteManager::Get().DrawHpBlock(canvas, {px - SpriteManager::HP_SZ, py}, hp_info.first);
hp_info.first -= 10;
}
}
private:
Image micro_inv_canvas;
Image inv_cell_canvas;
Image micro_inv;
Image inv_cell;
Image inv_cell_active;
Image inv_bg;
Image key_black;
Image key;
Image dmg_text_img;
std::vector<Image> damage_img;
std::vector<Image> boots_imgs;
static constexpr int Y = W_HEIGHT - TILE_SZ;
}invent;
E_Dir back_dir = E_Dir::DOWN;
Movement position;
Point coords {.x = 0, .y = 0};
Point old_coords {.x = 0, .y = 0};
Pixel color {.r = 255, .g = 255, .b = 0, .a = 255};
int move_speed = 90;
int move_speed_ctrl = 40;
LiveObjSprite &spr;
Sprite grave;
bool now_attack = false;
double blocked_to_time = -0.0;
bool died = false;
Point die_pos {-1, -1};
E_DieType die_type = E_DieType::EmptyStay;
int hp = 40;
int max_hp = 40;
int damage = 3;
};
#endif
//MAIN_PLAYER_H