forked from ComputerGeek01/Random
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFighter.cpp
258 lines (207 loc) · 4.78 KB
/
Fighter.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
#include "Fighter.hpp"
int FIGHTER::Fighters = 0;
int FIGHTER::Attack(UNIT* Unit)
{
if(!Alive) //If You're Dead You Don't Get To Attack
{
return 0;
}
if(Unit->LID == LID) //Don't Attack Yourself
{
return 0;
}
if(Ammo <= 0) //If You're Out Of Ammo Run Away
{
Mode = RUN;
MoveAway(Unit);
return 0;
}
if(Unit->Type == HUMAN) //If UNIT Is A Human Don't Shoot It
{
MoveAway(Unit);
return 0;
}
if(!Unit->Alive) //If What You're Evaluating Is Not Alive Don't Shoot It
{
return 0;
}
if(Target == -1 && Unit->Alive && Unit->Type == ZOMBIE) //If You Have No Target, Choose A New One
{
//std::cout << "Targeting " << Unit->PID << std::endl;
Target = Unit->LID;
TargetLoc = Unit->Pos;
}
float NewRange = fabs(sqrt(pow(Unit->Pos.y, 2) + pow(Unit->Pos.x, 2)) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2)));
TRange = fabs(sqrt((pow(TargetLoc.y, 2) + pow(TargetLoc.x, 2))) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2)));
if(NewRange < TRange)
{
Target = Unit->LID;
TargetLoc = Unit->Pos;
TRange = NewRange;
}
if(!Loaded) //If Your Weapon Not Loaded Run Away
{
Mode = RUN;
MoveAway(Unit);
return 0;
}
if(Unit->LID == Target) //If UNIT Is Your Current Target
{
if(!Unit->Alive)
{
Target = -1;
return 0;
}
if(TRange >= Range) //If UNIT Is Out Of 'Range' Left Or Right Approach Target
{
Dest = Unit->Pos;
TargetLoc = Unit->Pos;
return 0;
}
int Hit = rand() % 4; //If All Criteria Passed Then Fire
std::cout << LID << " Is Shooting At A " << Unit->TYPE() << " At " << TRange << std::endl;
Ammo--;
Time.Reset();
Loaded = false;
Firing = true;
switch(Hit)
{
case 1:
Unit->Hit();
//Target = -1; //One Hit Used To Kill The Opponent
return 1;
default:
return 1;
}
}
return 1;
}
std::string FIGHTER::GetData()
{
if(!Alive)
{
return "DEAD";
}
std::ostringstream String;
String << "LID: " << LID << "\nHit Points: " << HP << "\nTarget: " << Target << "\nAmmo " << Ammo;
return String.str();
}
void FIGHTER::Update()
{
switch(Mode) //This Will Probably Be Eliminated
{
case WAIT: //If Unit Is Waiting Then Return To Normal Mode
Mode = NORMAL;
return;
case CHASE: //If Unit Is In CHASE Mode They Will Not Choose A New Destination
Move();
break;
case NORMAL:
Move();
break;
case RUN:
Move();
break;
default:
break;
}
if((Pos.x == Dest.x) && (Pos.y == Dest.y))
{
NewDest();
}
if(Time.GetElapsedTime() >= 1) //Reload Timer NOTE: This Should NOT Be A Constant, SFML 2.0 Is Moving To Miliseconds
{
if(Ammo > 0)
{
Mode = NORMAL;
Loaded = true;
Time.Reset();
}
}
}
sf::Sprite* FIGHTER::draw()
{
if(!Selected && Loaded)
{
Sprite.SetPosition(Pos);
return &Sprite;
}
if(!Selected && Firing)
{
Firing = false;
Sprite2.SetPosition(Pos);
return &Sprite2;
}
if(Selected && !Firing)
{
Sprite3.SetPosition(Pos);
return &Sprite3;
}
if(Selected && Firing)
{
Firing = false;
Sprite4.SetPosition(Pos);
return &Sprite4;
}
if(!Selected && !Loaded)
{
Sprite5.SetPosition(Pos);
return &Sprite5;
}
if(Selected && !Loaded)
{
Sprite6.SetPosition(Pos);
return &Sprite6;
}
}
inline bool FIGHTER::RightClick(float X, float Y)
{
if(Selected)
{
Dest.x = X;
Dest.y = Y;
return true;
}
return false;
}
void FIGHTER::Zombify(sf::Image* zimg) //Broken, This Function Not In Use
{
std::cout << LID << " Zombified\n";
Type = ZOMBIE;
Img = zimg;
Sprite.SetImage(*Img);
}
void FIGHTER::Hit()
{
HP--;
if(HP <= 0)
{
Die();
}
}
void FIGHTER::Die()
{
Alive = false;
Selected = false;
Id--;
Fighters--;
}
/* void FIGHTER::MoveAway(sf::Vector2f Obj)
{
if(Obj.x > Pos.x)
{
Dest.x -= 1;
}
if(Obj.x < Pos.x)
{
Dest.x += 1;
}
if(Obj.y > Pos.y)
{
Dest.y -= 1;
}
if(Obj.y < Pos.y)
{
Dest.y += 1;
}
} */