-
Notifications
You must be signed in to change notification settings - Fork 1
/
karir.cpp
450 lines (362 loc) · 9.46 KB
/
karir.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <iostream>
#include <cstdlib>
#include <vector>
#include <list>
#include <math.h>
#include <SDL.h>
#include <SDL_rotozoom.h>
#include <SDL_framerate.h>
#include <SDL_gfxPrimitives.h>
#include "CSurface.h"
#include "CEvent.h"
#include "generic.hpp"
#include "Ship.hpp"
#include "Vector.hpp"
using namespace std;
class DirectionDrawer : public CEvent {
private:
SDL_Surface* Surf_Display;
SDL_Surface* Surf_BG;
SDL_Surface* Surf_Explosion;
int Running;
int Pause;
private:
vector<Ship> ships;
double explosion_frames;
vector<Explosion> explosions;
public:
DirectionDrawer();
bool PrepSDL();
void Init();
void MainLoop();
void Render();
void OnEvent(SDL_Event*);
void OnKeyDown(SDLKey, SDLMod, Uint16);
void OnKeyUp(SDLKey, SDLMod, Uint16);
void DrawVec(Vector, Cords,Uint8);
void FindCollisions(void);
};
DirectionDrawer::DirectionDrawer() {
Surf_Display = NULL;
Surf_BG = NULL;
Surf_Explosion = NULL;
Running = 1;
Pause = -1;
}
void DirectionDrawer::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
switch(sym) {
case SDLK_ESCAPE:
Running = 0;
break;
case SDLK_p:
Pause *= -1;
break;
case SDLK_z:
Init();
break;
// player 1
case SDLK_SPACE:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
if (a->CanFire())
ships.push_back(a->Fire());
break;
}
break;
case SDLK_RIGHT:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Rotate((-1 * a->rotate_speed));
break;
}
break;
case SDLK_LEFT:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Rotate(a->rotate_speed);
break;
}
break;
case SDLK_DOWN:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Accelerating(-1);
break;
}
break;
case SDLK_UP:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Accelerating(1);
break;
}
break;
// player 2
case SDLK_LCTRL:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
if (a->CanFire())
ships.push_back(a->Fire());
break;
}
break;
case SDLK_d:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Rotate((-1 * a->rotate_speed));
break;
}
break;
case SDLK_a:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Rotate(a->rotate_speed);
break;
}
break;
case SDLK_s:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Accelerating(-1);
break;
}
break;
case SDLK_w:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Accelerating(1);
break;
}
break;
}
}
void DirectionDrawer::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
switch(sym) {
// player 1
case SDLK_RIGHT:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Rotate((a->rotate_speed));
break;
}
break;
case SDLK_LEFT:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Rotate(-1 * a->rotate_speed);
break;
}
break;
case SDLK_DOWN:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Accelerating(1);
break;
}
break;
case SDLK_UP:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 1) {
a->Accelerating(-1);
break;
}
break;
// player 2
case SDLK_d:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Rotate((a->rotate_speed));
break;
}
break;
case SDLK_a:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Rotate(-1 * a->rotate_speed);
break;
}
break;
case SDLK_s:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Accelerating(1);
break;
}
break;
case SDLK_w:
for (ship_iter a = ships.begin(); a != ships.end(); a++)
if (a->ship_id == 2) {
a->Accelerating(-1);
break;
}
break;
}
}
void DirectionDrawer::Init() {
vector<Ship> empty_ship_vec;
ships = empty_ship_vec;
explosion_frames = 18;
Ship ship;
ship.ship_id = 1;
ship.ShipCords.x = 500;
ship.ShipCords.y = 500;
ship.ShipCords.degree = 0;
ship.acc_speed = 2;
ship.max_speed = 10;
ship.rotate_speed = 10;
ship.fire_cost = 10;
ship.hit_points = 100;
ship.LoadSurface("./gfx/Ship1.png",ship.ship_surf);
ship.LoadSurface("./gfx/fire.png",ship.missile_surf);
ships.push_back(ship);
// seconds ship
Ship ship2;
ship2.ship_id = 2;
ship2.ShipCords.x = 500;
ship2.ShipCords.y = 300;
ship2.ShipCords.degree = 0;
ship2.acc_speed = 2;
ship2.max_speed = 10;
ship2.rotate_speed = 10;
ship2.fire_cost = 10;
ship2.hit_points = 100;
ship2.LoadSurface("./gfx/Ship2.png",ship2.ship_surf);
ship2.LoadSurface("./gfx/fire.png",ship2.missile_surf);
ships.push_back(ship2);
}
bool DirectionDrawer::PrepSDL() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
return false;
if ((Surf_Display = SDL_SetVideoMode(BOARD_X,BOARD_Y,32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
return false;
Surf_BG = CSurface::OnLoad("./gfx/bg.png");
Surf_Explosion = CSurface::OnLoad("./gfx/explosion.png");
return true;
}
void DirectionDrawer::OnEvent(SDL_Event* Event) {
CEvent::OnEvent(Event);
}
bool Collides(Cords a_cords,SDL_Surface* a_surf, Cords b_cords, SDL_Surface* b_surf) {
double ax1 = a_cords.x;
double ay1 = a_cords.y;
double ax2 = ax1 + a_surf->w;
double ay2 = ay1 + a_surf->h;
double bx1 = b_cords.x;
double by1 = b_cords.y;
double bx2 = bx1 + b_surf->w;
double by2 = by1 + b_surf->h;
if (ax1 >= bx1 && ax1 <= bx2 && ay1 >= by1 && ay1 <= by2)
return true;
if (ax2 >= bx1 && ax2 <= bx2 && ay2 >= by1 && ay2 <= by2)
return true;
return false;
}
void DirectionDrawer::FindCollisions() {
// i guess this is a lame collision detection algorithm
// but i just want to get it working... :)
// go over all objects
for (ship_iter a = ships.begin(); a != ships.end(); a++) {
// and go over all objects again
for (ship_iter b = ships.begin(); b != ships.end(); b++) {
// if it isn't the same object, check for collision
if (a != b) {
if (Collides(
a->ShipCords,
a->GetSurface((vsurf_sz)a->ShipCords.degree),
b->ShipCords,
b->GetSurface((vsurf_sz)a->ShipCords.degree)))
{
// a is a missile hitting enemy ship b
if (a->fire_damage > 0 && b->hit_points > 0 and a->missile_id != b->ship_id) {
// create explosion
Explosion explosion;
explosion.Explosion_Cords = b->ShipCords;
explosion.frames = explosion_frames;
explosions.push_back(explosion);
// lower hitpoints
b->hit_points -= a->fire_damage;
a->hit_points--;
}
}
}
}
}
}
void DirectionDrawer::MainLoop() {
int i = 0;
while (Running == 1) {
SDL_Event event;
while (SDL_PollEvent(&event))
OnEvent(&event);
if (Pause < 0) {
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++)
sp->NextShip();
FindCollisions();
Render();
SDL_Delay(1000 / FRAME_RATE);
}
// reseting vectors
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++) {
sp->ClearVectors();
// deleting expired missiles
if (sp->max_distance != 0 && sp->distance > sp->max_distance) {
ships.erase(sp);
sp--;
}
}
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++)
if (sp->hit_points <= 0)
ships.erase(sp--);
}
}
void DirectionDrawer::DrawVec(Vector v, Cords c,Uint8 color) {
double x = c.x + ( 100 * v.force * v.X());
double y = c.y - ( 100 * v.force * v.Y());
cout << "cx: " << c.x << " cy: " << c.y << " x: " << x << " y: " << y << endl;
lineRGBA(Surf_Display,c.x,c.y,x,y,0,color / 2,color ,255);
}
void DirectionDrawer::Render() {
//background
CSurface::OnDraw(Surf_Display,Surf_BG, 0, 0);
if (DRAW_VECTORS == 1) {
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++)
DrawVec(sp->ship_vec,sp->ShipCords,100);
// draw ship_vec
Uint8 color = 200;
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++) {
for (vec_iter i = sp->ship_vectors.begin(); i != sp->ship_vectors.end(); i++) {
DrawVec(*i,sp->ShipCords,color);
color += 30;
}
}
}
if (DRAW_TRACE == 1) {
// movement traces
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++)
for (list<Cords>::const_iterator p = sp->Last_ShipCords.begin(); p != sp->Last_ShipCords.end(); p++) {
filledCircleRGBA(Surf_Display,p->x,p->y,4,255,255,0,255);
}
}
// ship itself
for (ship_iter sp = ships.begin(); sp != ships.end(); sp++) {
SDL_Surface* tmp_surf = sp->GetSurface((vsurf_sz)sp->ShipCords.degree);
CSurface::OnDraw(Surf_Display,tmp_surf, (sp->ShipCords.x - (tmp_surf->w / 2)), (sp->ShipCords.y - (tmp_surf->h /2 )));
}
// draw explosions
for (exp_iter ex = explosions.begin(); ex != explosions.end(); ex++) {
CSurface::OnDraw(Surf_Display,Surf_Explosion, ( ex->Explosion_Cords.x - 32 / 2), (ex->Explosion_Cords.y - 42 / 2),(ex->frame * 34),((int)ex->frame / 6) * 42,34,42);
if (ex->frame++ > ex->frames) {
explosions.erase(ex);
ex--;
}
}
SDL_Flip(Surf_Display);
}
int main(int argc, char* argv[]) {
DirectionDrawer Board;
if (!Board.PrepSDL())
return 1;
Board.Init();
Board.MainLoop();
return 0;
}