/
Character.h
326 lines (255 loc) · 4.51 KB
/
Character.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
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
#ifndef TANKS
#define TANKS
#include "Utility.h"
using namespace std;
using namespace sf;
class GameChar
{
public:
GameChar()
{
centerX = centerY = dX = dY = 0.0f;
}
virtual ~GameChar()
{
}
float getCenterX() const
{
return centerX;
}
float getCenterY() const
{
return centerY;
}
float getDx() const
{
return dX;
}
float getDy() const
{
return dY;
}
bool getVisible() const
{
return isVisible;
}
void setInvisible()
{
this->isVisible = false;
}
virtual void update() = 0;
virtual FloatRect getBounds() const = 0;
protected:
float centerX, centerY, dX, dY;
bool isVisible;
};
class Rocket : public GameChar
{
public:
Rocket(Sprite sp, float centerX, float centerY, float theta)
{
cout << "Rocket Created" << endl;
float rocketRelease = 76.0f;
this->centerX = centerX + rocketRelease * cosf(radianForm(theta));
this->centerY = centerY + rocketRelease * sinf(radianForm(theta));
this->isVisible = true;
this->theta = theta;
this->dX = 5 * cosf(radianForm(theta));
this->dY = 5 * sinf(radianForm(theta));
this->rocketSprite = sp;
}
~Rocket()
{
cout <<"Rocket deleted"<<endl;
}
float getTheta() const
{
return theta;
}
Sprite getSprite() const
{
return rocketSprite;
}
FloatRect getBounds() const
{
return rocketSprite.getGlobalBounds();
}
void update()
{
centerX += dX;
centerY += dY;
if (centerX < 0 || centerY < 0 ||centerX > 800 || centerY > 600 )
{
setInvisible();
}
rocketSprite.setPosition(centerX,centerY);
rocketSprite.setRotation(theta);
}
private:
float theta;
Sprite rocketSprite;
};
class Enemy : public GameChar
{
public:
Enemy(float x, float y, float radius)
{
this->centerX = x;
this->centerY = y;
cs = CircleShape(radius);
cs.setOrigin(radius , radius);
cs.setFillColor(Color::Blue);
cs.setPosition(x,y);
dX = 2.0f;
}
CircleShape getSprite() const
{
return cs;
}
FloatRect getBounds() const
{
return cs.getGlobalBounds();
}
void update()
{
if( centerX > 600 )
{
dX = -2.0f;
}
else if( centerX < 200 )
{
dX = 2.0f;
}
centerX += dX;
cs.setPosition(centerX,centerY);
}
private:
CircleShape cs;
};
class Tanks : public GameChar
{
public:
Tanks(Sprite sprite, Sprite sprite2, Texture rocketTexture, float x, float y)
{
rockets = new vector<Rocket>;
bodyTheta = weapTheta = bThetaIncr = wThetaIncr = 0.0f;
centerX = x;
centerY = y;
this->sprite = new Sprite(sprite);
this->sprite2 = new Sprite(sprite2);
this->rocketTexture = rocketTexture;
}
~Tanks()
{
delete rockets;
delete sprite;
delete sprite2;
}
float getBodyTheta() const
{
return bodyTheta;
}
float getWeapTheta() const
{
return weapTheta;
}
// can be changed
vector<Rocket>* getRockets()
{
return rockets;
}
Sprite* getSprite() const
{
return sprite;
}
Sprite* getSprite2() const
{
return sprite2;
}
FloatRect getBounds() const
{
return sprite->getGlobalBounds();
}
void weaponShoot()
{
Sprite sp;
sp.setTexture(rocketTexture);
sp.setOrigin(13,12);
rockets->push_back( Rocket(sp, centerX, centerY, weapTheta ) );
}
void moveWeapCCW()
{
wThetaIncr = -1.0f;
}
void moveWeapCW()
{
wThetaIncr = 1.0f;
}
void moveBodyCCW()
{
bThetaIncr = -0.5f;
wThetaIncr = -0.5f;
}
void moveBodyCW()
{
bThetaIncr = 0.5f;
wThetaIncr = 0.5f;
}
void stopWeap()
{
wThetaIncr = 0.0f;
}
void stopBody()
{
bThetaIncr = 0.0f;
stopWeap();
}
void forward()
{
dX = cosf(radianForm(bodyTheta));
dY = sinf(radianForm(bodyTheta));
}
void backward()
{
dX = -cosf(radianForm(bodyTheta));
dY = -sinf(radianForm(bodyTheta));
}
void stop()
{
dX = dY = 0.0f;
}
void update()
{
if( weapTheta < 360 || weapTheta > -360 )
weapTheta += wThetaIncr;
else
weapTheta = 0;
if( bodyTheta < 360 || bodyTheta > -360 )
bodyTheta += bThetaIncr;
else
bodyTheta = 0;
cout << rockets->size() << " " << endl;
centerX += dX;
centerY += dY;
sprite->setPosition(centerX,centerY);
sprite2->setPosition(centerX,centerY);
sprite->setRotation(bodyTheta);
sprite2->setRotation(weapTheta);
for(unsigned int i = 0; i < rockets->size(); i++)
{
(*rockets)[i].update();
}
if( rockets->size() >= 1 )
{
auto iter = remove_if(rockets->begin(), rockets->end(), []( const Rocket& r) { return ! r.getVisible(); } );
rockets->erase( iter, rockets->end() );
}
}
private:
float bodyTheta, weapTheta, bThetaIncr, wThetaIncr;
vector<Rocket>* rockets;
Sprite* sprite;
Sprite* sprite2;
Texture rocketTexture;
};
#endif