-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.cpp
343 lines (287 loc) · 7.79 KB
/
unit.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "unit.hpp"
namespace TerraNova {
std::string Unit::GenerateGivenName(){
return std::string("Urist");
}
std::string Unit::GenerateSurname(){
return std::string("McColonist");
}
Unit::Unit(SDL_Renderer* ren,
const std::shared_ptr<UnitType> spec, const char faction):
GFXObject(ren,
File::SpritePath() / "units" / spec->PathName() / "sprite", 0,
0, true), spec(spec), faction(faction), location({{-1u,-1u}}) {
givenName = GenerateGivenName();
surname = GenerateSurname();
srand(time(NULL));
female = std::rand() % 2;
health = MaxHealth();
movesLeft = MoveSpeed()+1;
healthBackground = GFXManager::RequestSprite(
File::SpritePath() / "healthbar_background" );
healthBar = GFXManager::RequestSprite(
File::SpritePath() / ("healthbar_p" + std::to_string(faction)) );
SetOrders(ORDER_PATROL);
}
void Unit::ChangeName(const std::string givenName, const std::string surname){
this->givenName = givenName;
this->surname = surname;
}
void Unit::ChangeName(const std::string fullName){
givenName = fullName.substr(0, fullName.find(' '));
surname = fullName.substr(fullName.find(' ') + 1);
}
void Unit::ChangeGender(const std::string gender){
if(gender == "female") female = true;
if(gender == "male") female = false;
}
void Unit::ChangeSpec(const std::shared_ptr<UnitType> spec){
this->spec = spec;
sprite = GFXManager::RequestSprite(File::SpritePath() / spec->PathName());
//selectedSprite = GFXManager::RequestSprite(
//"units/" + spec->Name() + "/sprite_selected");
}
bool Unit::TakeDamage(const int damage){
if(damage >= health){
health = 0;
Die();
return true;
}
health -= damage;
if(health > MaxHealth()) health = MaxHealth();
return false;
}
// this corresponds to things which consume the unit but don't trigger death effects
void Unit::BeConsumed(){
health = 0;
Die(); // should probably be something else that also gets the unit deleted
}
void Unit::Die(){
std::cout << Name() << " belonging to player " << (int)faction << " has died!"
<< std::endl;
visible = false;
}
void Unit::AddItem(const std::string item){
inventory.push_back(item);
}
void Unit::MoveSpriteToTile(const int X, const int Y, const int W, const int H){
MoveTo((MAPDISP_ORIGIN_X + X + (W - layout.w)/2),
(MAPDISP_ORIGIN_Y + Y + (H - layout.w)/2));
}
void Unit::Render() const{
//std::cout << Dead() << std::endl;
if(!visible) return;
if(Dead()) return;
if(myPath) myPath->RenderStartingFrom(X() + PERSON_WIDTH/2,
Y() - PERSON_HEIGHT/2);
GFXObject::Render();
SDL_Rect healthLayout;
healthLayout.w = HEALTH_BAR_WIDTH;
healthLayout.h = HEALTH_BAR_HEIGHT;
healthLayout.x = layout.x + (layout.w - healthLayout.w)/2;
healthLayout.y = layout.y - healthLayout.h;
healthBackground->RenderTo(ren, healthLayout);
healthLayout.x += 1;
healthLayout.y += 1;
healthLayout.h -= 2;
healthLayout.w -= 2;
healthLayout.w *= Health();
healthLayout.w /= MaxHealth();
healthBar->RenderTo(ren, healthLayout);
if(orderIcon){
SDL_Rect orderLayout = layout;
orderIcon->MakeDefaultSize(orderLayout);
orderLayout.y += layout.h - orderLayout.h;
orderIcon->RenderTo(orderLayout);
}
}
void Unit::ProcessTurn(){
movesLeft = MoveSpeed();
}
std::string Unit::Name() const{
std::string name(givenName);
name.append(" ");
name.append(surname);
return name;
}
std::string Unit::GivenName() const{
return givenName;
}
std::string Unit::Surname() const{
return surname;
}
std::string Unit::Species() const{
return "human";
}
std::string Unit::Gender() const{
if(female) return std::string("female");
return std::string("male");
}
std::string Unit::NomPronoun() const{
if(female) return std::string("she");
return std::string("he");
}
std::string Unit::NomPronCap() const{
if(female) return std::string("She");
return std::string("He");
}
std::string Unit::AccPronoun() const{
if(female) return std::string("her");
return std::string("him");
}
std::string Unit::AccPronCap() const{
if(female) return std::string("Her");
return std::string("Him");
}
std::string Unit::PosPronoun() const{
if(female) return std::string("her");
return std::string("his");
}
std::string Unit::PosPronCap() const{
if(female) return std::string("Her");
return std::string("His");
}
std::shared_ptr<UnitType> Unit::Spec() const{
return spec;
}
int Unit::MaxHealth() const{
return spec->MaxHealth();
}
int Unit::Health() const{
return health;
}
std::vector<std::string> Unit::Inventory() const{
return inventory;
}
int Unit::MoveSpeed() const{
return spec->MoveSpeed();
}
int Unit::MovesLeft() const{
return movesLeft;
}
void Unit::SetLocation(const unsigned int row, const unsigned int colm,
const bool usesMove){
/*std::cout << "Setting location of " << Name() << " to (" << row << ","
<< colm << ")." << std::endl;*/
location[0] = row;
location[1] = colm;
if(usesMove) movesLeft--;
}
std::array<unsigned int, 2> Unit::Location() const{
return location;
}
int Unit::Row() const{
return location[0];
}
int Unit::Colm() const{
return location[1];
}
std::array<unsigned int, 2> Unit::NextStep(){
if(!myPath){
std::cerr << "Error: asked for the next step along a Path, but the "
<< "Unit does not have one." << std::endl;
return std::array<unsigned int, 2>({{-1u, -1u}});
}
return myPath->NextStep();
}
bool Unit::AdvancePath(){
if(!myPath) return true;
bool arrived(myPath->Advance());
if(arrived){
SetOrders(ORDER_PATROL);
myPath.reset();
}
return arrived;
}
void Unit::SetOrders(const order_t newOrders){
orders = newOrders;
std::string orderName = "order_";
switch(newOrders){
case ORDER_ADVANCE:
orderName += "move";
break;
case ORDER_PATROL:
orderName += "patrol";
break;
case ORDER_HARVEST:
orderName += "harvest";
break;
}
orderIcon = GFXManager::RequestSprite(File::SpritePath() / orderName);
}
void Unit::OrderMove(std::unique_ptr<Path> newPath){
if(newPath){
SetOrders(ORDER_ADVANCE);
myPath = std::move(newPath);
} else {
// empty Path means it's an order to move to the place you already are
OrderPatrol();
}
}
void Unit::OrderPatrol(){
SetOrders(ORDER_PATROL);
myPath = nullptr;
}
void Unit::OrderHarvest(){
SetOrders(ORDER_HARVEST);
myPath = nullptr;
}
std::vector<Order> Unit::AvailableOrders() {
std::vector<Order> ret;
ret.emplace_back("patrol", std::bind(&Unit::OrderPatrol, this));
ret.emplace_back("harvest", std::bind(&Unit::OrderHarvest, this));
return ret;
}
std::vector<std::shared_ptr<AttackType>> Unit::Attacks() const{
return spec->Attacks();
}
int Unit::Damage(const unsigned int num) const{
if(num < Attacks().size()) return Attacks()[num]->Damage();
return 0;
}
float Unit::Accuracy(const unsigned int num) const{
if(num < Attacks().size()) return Attacks()[num]->Accuracy();
return 0;
}
int Unit::AttackRate(const unsigned int num) const{
if(num < Attacks().size()) return Attacks()[num]->AttackRate();
return 0;
}
std::string Unit::DamageType(const unsigned int num) const {
if(num < Attacks().size()) return Attacks()[num]->DamageType();
return "";
}
// return true if target was killed
bool Unit::Attack(Unit* target) const{
std::random_device dev;
std::mt19937 gen(dev());
std::uniform_real_distribution<> dist(0,1);
for(auto& atk : Attacks()){
for(int hit = 0; hit < atk->AttackRate(); ++hit){
if(dist(gen) <= atk->Accuracy()){
if(target->TakeDamage(atk->Damage())) return true;
}
}
}
return false;
}
void Unit::Fight(Unit* attacker, Unit* target){
if(!attacker){
std::cerr << "Error: attempting a fight but there is no attacker." << std::endl;
return;
}
if(!target){
std::cerr << "Error: attempting a fight but there is no target." << std::endl;
return;
}
/*if(attacker->MovesLeft() < 1){
std::cout << "Fighting requires an available movement point." << std::endl;
return;
}*/
bool targetDead = attacker->Attack(target);
if(!targetDead){
attacker->movesLeft--;
target->Attack(attacker);
}
}
} // namespace TerraNova