-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.cpp
215 lines (207 loc) · 6.43 KB
/
Item.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
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
#include "Item.h"
#include "Player.h"
#include "GameMap.h"
constexpr int case_special(const char* s, int sz)
{
int ret = 0;
for (int i = 0; i < sz; i++) ret = ret * 40 + s[i] - 'a' + 10;
return ret;
}
constexpr int case_book = case_special("book", 4);
constexpr int case_ring = case_special("ring", 4);
constexpr int case_boots = case_special("boots", 5);
constexpr int case_potion = case_special("pot", 3);
Item::Item(int i_lvl, Point _pos) : item_lvl(i_lvl), pos(_pos)
{
switch (item_lvl) {
case -1: item_type = E_ItemTypes::Key; break;
case 0: item_type = E_ItemTypes::Other; break;
case 1:
{
int t = std::rand() % 4;
if (t == 0)item_type = E_ItemTypes::Potion;
if (t == 1)item_type = E_ItemTypes::Boots;
if (t == 2)item_type = E_ItemTypes::Book;
if (t == 3)item_type = E_ItemTypes::Ring;
break;
}
case 2:
{
int t = std::rand() % 5;
if (t < 2)item_type = E_ItemTypes::Potion;
if (t == 2)item_type = E_ItemTypes::Book;
if (t == 3)item_type = E_ItemTypes::Ring;
if (t == 4)item_type = E_ItemTypes::Boots;
break;
}
case 3:
{
int t = std::rand() % 5;
if (t < 3)item_type = E_ItemTypes::Potion;
if (t == 3)item_type = E_ItemTypes::Book;
if (t == 4)item_type = E_ItemTypes::Boots;
break;
}
case case_boots:
case case_book:
{
int t = std::rand() % 18;
item_type = (item_lvl == case_book) ? E_ItemTypes::Book : E_ItemTypes::Boots;
if (t < 11)item_lvl = 1;
else if (t < 16)item_lvl = 2;
else item_lvl = 3;
break;
}
case case_potion:
{
int t = std::rand() % 18;
item_type = E_ItemTypes::Potion;
if (t < 8)item_lvl = 1;
else if (t < 14)item_lvl = 2;
else item_lvl = 3;
break;
}
case case_ring:
{
int t = std::rand() % 18;
item_type = E_ItemTypes::Ring;
if (t < 13)item_lvl = 1;
else item_lvl = 2;
break;
}
default:
item_type = E_ItemTypes::Key;
item_lvl = -1;
break;
}
CreateHelper();
}
Item::Item(E_ItemTypes i_type, int i_lvl, Point _pos) : item_type(i_type), item_lvl(i_lvl), pos(_pos)
{
CreateHelper();
}
bool IsPlaceForItem(){return Player::Get().IsPlaceForItem();}
void Item::CreateHelper()
{
switch (item_type) {
case E_ItemTypes::Key:
inventory = false;
item_lvl = -1;
spr = Sprite {"../resources/key.png", 1};
ict = []() { return Player::Get().CanTakeKey(); };
ia = [](bool on) {if (on)Player::Get().KeyInc(); else Player::Get().KeyDec(); };
break;
case E_ItemTypes::Boots:
{
inventory = false;
spr = Sprite {"../resources/boots_" + std::to_string(item_lvl) + ".png", 1};
ict = []() { return true; };
ia = [item_lvl = item_lvl, pos = pos](bool on) {
if (on) {
auto &pl = Player::Get();
int pl_b_lvl = pl.BootsLvl();
if (pl_b_lvl != 0)GameMap::GetCur()->GetItems().emplace_back(E_ItemTypes::Boots, pl_b_lvl, pos);
pl.TakeBoots(item_lvl);
} else Player::Get().KeyDec();
};
break;
}
case E_ItemTypes::Ring:
{
inventory = true;
Image img {0, 0, 0};
if (item_lvl == 1) {
if (std::rand() % 2) {
img = Image {"../resources/ring_hp_1.png"};
ia = [](bool on) {
auto &pl = Player::Get();
if (on) pl.MaxHpChange(10);
else pl.MaxHpChange(-10);
};
} else {
img = Image {"../resources/ring_dmg_1.png"};
ia = [](bool on) {
auto &pl = Player::Get();
if (on) pl.DamageChange(1);
else pl.DamageChange(-1);
};
}
} else {
img = Image {"../resources/ring_dmg_2.png"};
ia = [](bool on) {
auto &pl = Player::Get();
if (on) pl.DamageChange(2);
else pl.DamageChange(-2);
};
}
img.PixelsChange([](auto x) { return (x.a == 0 || x.r == x.b && x.r == x.g && x.r == 255) ? TRANSP_COLOR : x; }, false);
spr = Sprite {img, SpritePixSz(15)};
ict = IsPlaceForItem;
break;
}
case E_ItemTypes::Potion:
{
inventory = true;
can_be_used = true;
spr = Sprite {"../resources/hp_pot_" + std::to_string(item_lvl) + ".png", (item_lvl == 1) ? 7 : 11};
ict = IsPlaceForItem;
ia = [item_lvl = item_lvl, pos = pos](bool on) {};
use = [power = item_lvl]() { Player::Get().HpRestore(4 + power * 8); };
break;
}
case E_ItemTypes::Book:
{
inventory = true;
can_be_used = true;
Image img {0, 0, 0};
if (item_lvl == 3 || item_lvl == 1) {
img = Image {"../resources/book_dmg_" + std::to_string(item_lvl) + ".png"};
use = [power = item_lvl]() { Player::Get().DamageChange(power); };
} else {
int t = std::rand() % 3;
switch (t) {
case 1:
{
img = Image {"../resources/book_hp.png"};
use = []() { Player::Get().MaxHpChange(10); };
break;
}
case 2:
{
img = Image {"../resources/book_inv.png"};
use = []() { Player::Get().InventorySizeInc(); };
break;
}
default:
{
img = Image {"../resources/book_dmg_2.png"};
use = []() { Player::Get().DamageChange(2); };
break;
}
}
}
img.PixelsChange([](auto x) { return (x.a == 0 || x.r == x.b && x.r == x.g && x.r == 255) ? TRANSP_COLOR : x; }, false);
spr = Sprite{img, SpritePixSz(14)};
ict = IsPlaceForItem;
ia = [item_lvl = item_lvl, pos = pos](bool on) {};
break;
}
case E_ItemTypes::Other:
{
inventory = true;
spr = Sprite {"../resources/feat.png", 1};
ict = IsPlaceForItem;
ia = [pos = pos](bool on) {
auto &pl = Player::Get();
if (on) {
pl.AddSpeed(15);
} else {
pl.AddSpeed(-15);
}
};
break;
}
default:
break;
}
}