-
Notifications
You must be signed in to change notification settings - Fork 0
/
Soldier.cpp
199 lines (168 loc) · 3.97 KB
/
Soldier.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
#include "Soldier.h"
#include "Model.h"
#include <math.h>
#include "Person.h"
#include "Cart_Point.h"
#include <fstream>
Model smodel;
Soldier::Soldier(): Person('S')
{
state = 's';
attack_strength = 2;
range = 2.0;
target = NULL;
cout << "Soldier default constructed." << endl;
}
Soldier::Soldier(int in_id, Cart_Point in_loc): Person('S', in_id, in_loc)
{
state = 's';
attack_strength = 2;
range = 2.0;
target = NULL;
cout << "Soldier constructed." << endl;
}
void Soldier::start_attack(Person* in_target)
{
if (is_alive() == true)
{
Cart_Point a, b;
a = (*in_target).get_location();
b = get_location();
if (cart_distance(a, b) <= range)
{
target = in_target;
state = 'a';
cout << "Attacking." << endl;
}
else if (cart_distance((*in_target).get_location(), get_location()) > range)
{
cout << "Target is out of range." << endl;
}
}
else
cout << "I am dead. You do not have the ability to summon the undead." << endl;
}
bool Soldier::update()
{
if (state == 'm')
{
if (update_location() == true)
{
state = 's';
return true;
}
else
{
return false;
}
}
else if (state == 'x')
{
return false;
}
else if (state == 's')
{
return false;
}
else if (state == 'a')
{
if (cart_distance((*target).get_location(), get_location()) > range)
{
cout << "Target is out of range." << endl;
state = 's';
return true;
}
if (cart_distance((*target).get_location(), get_location()) <= range)
{
if ((*target).is_alive() == true)
{
cout << "Take that!" << endl;
(*target).take_hit(attack_strength, this);
state = 'a';
return false;
}
else if ((*target).is_alive() == false)
{
cout << "I win." << endl;
state = 's';
return true;
}
}
}
else
{
return false;
}
}
void Soldier::show_status()
{
cout << "Soldier status: ";
Person::show_status();
if (state == 'a')
cout << " Attacking" << endl;
}
bool Soldier::update_location()
{
Cart_Vector vect;
vect = (get_destination() - location);
if (fabs(vect.x) <= fabs((get_delta()).x) && fabs(vect.y) <= fabs((get_delta()).y))
{
location = get_destination();
cout << display_code << get_id() << ": I'm there!" << endl;
return true;
}
else
location.x = location.x + ((get_delta()).x);
location.y = location.y + ((get_delta()).y);
cout << display_code << get_id() << ": step..." << endl;
return false;
}
int Soldier::get_attack_strength()
{
return attack_strength;
}
void Soldier::save(ofstream& file)
{
Person::save(file);
file << attack_strength << endl;
file << range << endl;
if (target == NULL)
{
file << -1 << endl;
}
else
{
file << (*target).get_id();
}
cout << "Soldier saved!" << endl;
}
void Soldier::restore(ifstream& file)
{
Person::restore(file);
int a;
double b;
file >> a;
attack_strength = a;
file >> b;
range = b;
int c;
file >> c;
if (c == -1)
{
target = NULL;
}
else
{
target = smodel.get_Person_ptr(c);
}
show_status();
}
void Soldier::take_hit(int attack_strength, Person* attacker_ptr)
{
Person::take_hit(attack_strength, attacker_ptr);
start_attack(attacker_ptr);
}
Soldier::~Soldier()
{
cout << "Soldier destructed." << endl;
}