-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.h
344 lines (291 loc) · 8.39 KB
/
core.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
int file_exists(char* filename)
{
struct stat statbuf;
if(stat(filename, &statbuf) < 0)
{
if(errno == ENOENT)
return 0;
else
return 1;
}
return 1;
}
// core definitions
CORE::CORE()
{
rockets = new list<rocket>(15);
bombs = new list<bomb>(20);
nukes = new list<nuke>(5);
}
CORE::~CORE()
{
delete this->ground;
delete this->turret;
delete this->gun;
delete this->sky;
delete this->crosshair;
delete this->rocketTrailPiece;
delete this->bombTrailPiece;
delete this->nukeTrailPiece;
delete this->rockets;
delete this->bombs;
delete this->nukes;
}
int CORE::load_data()
{
this->init();
// load font
fnt = new hgeFont("data/font.fnt");
fnt->SetColor(0xFFFFFFFF);
// create sky
sky = new hgeSprite(0, 0, 0, 800, 600);
sky->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND);
// create textures
txRocket = hge->Texture_Load("data/rocket.png");
txBomb = hge->Texture_Load("data/bomb.png");
txNuke = hge->Texture_Load("data/nuke.png");
// create crosshair
txCrosshair = hge->Texture_Load("data/crosshair.png");
crosshair = new hgeSprite(txCrosshair, 0, 0, 16, 16);
crosshair->SetHotSpot(8, 8);
// create ground
txGround = hge->Texture_Load("data/ground.png");
ground = new hgeSprite(txGround, 0, 0, 800, 100);
// create turret
txTurret = hge->Texture_Load("data/turret.png");
turret = new hgeSprite(txTurret, 0, 0, 128, 128);
// create gun
txGun = hge->Texture_Load("data/gun.png");
gun = new hgeSprite(txGun, 0, 0, 72, 72);
gun->SetHotSpot(36, 56);
this->turretAngle = 0;
// sign
txSign = hge->Texture_Load("data/sign.png");
sign = new hgeSprite(txSign, 0, 0, 64, 128);
sign->SetHotSpot(32, 64);
// create particle systems
txRocketTrail = hge->Texture_Load("data/rockettrail.png");
rocketTrailPiece = new hgeSprite(txRocketTrail, 0, 0, 24, 24);
rocketTrailPiece->SetHotSpot(12, 12);
rocketTrailPiece->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_ZWRITE);
txBombTrail = hge->Texture_Load("data/bombtrail.png");
bombTrailPiece = new hgeSprite(txBombTrail, 0, 0, 32, 32);
bombTrailPiece->SetHotSpot(16, 16);
bombTrailPiece->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_ZWRITE);
txNukeTrail = hge->Texture_Load("data/nuketrail.png");
nukeTrailPiece = new hgeSprite(txNukeTrail, 0, 0, 32, 32);
nukeTrailPiece->SetHotSpot(16, 16);
nukeTrailPiece->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_ZWRITE);
txExplosion = hge->Texture_Load("data/explosion.png");
explosionPiece = new hgeSprite(txExplosion, 0, 0, 48, 48);
explosionPiece->SetHotSpot(24, 24);
explosionPiece->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_ZWRITE);
// now make sound!
music = hge->Music_Load("data/bg.mod");
if(music) channel = hge->Music_Play(music, true);
hge->Channel_SetVolume(channel, 40);
if(txRocket && txBomb && txCrosshair && txGround && txTurret && txSign && txGun && txRocketTrail && txBombTrail && txExplosion && txNuke && txNukeTrail && music && channel) return 1;
return 0;
}
void CORE::init()
{
// values
hge->Random_Seed(0);
this->bombInterval = this->bombTimer = this->bombSpeedup = 4;
this->nukeTimer = 30;
score = fired = hit = 0;
textalpha = 0xFF;
alive = true;
pause = false;
// sky & stars
colors[0] = 0;
colors[1] = 16;
colors[2] = 16;
colors[3] = 0;
colors[4] = 21;
colors[5] = 80;
starcolor = 0;
for(int idx=0; idx<100; idx++)
{
stars[idx].x = hge->Random_Int(0, 800);
stars[idx].y = hge->Random_Int(0, 600);
}
// empty lists
this->rockets->empty();
this->bombs->empty();
this->nukes->empty();
}
void CORE::rotate_turret()
{
if(this->mouseY < 400)
this->turretAngle = atan((this->mouseX-398)/(420-this->mouseY));
else if(this->mouseX > 398)
this->turretAngle = 3.14/2;
else
this->turretAngle = 3.14*3/2;
}
void CORE::fire_rocket()
{
if(this->rockets->length == 15) return;
// create and set rocket up
rocket *r = new rocket(398 + sin(this->turretAngle)*50, 420 - cos(this->turretAngle)*50, this->turretAngle);
// add rocket to the list
this->rockets->add(r);
}
void CORE::drop_bomb()
{
// create and set bomb up
float x = hge->Random_Int(20, 780);
bomb *b = new bomb(x, -32, 3.14 + atan((hge->Random_Int(200, 600)-x)/(-532)));
// hard mode ?
if(hge->Timer_GetTime() > 40 && hge->Random_Int(0, 100) > 50)
{
b->curved = true;
b->curve_dir = (hge->Random_Int(0, 100) > 50 ? -1 : 1);
}
// add bomb to list
this->bombs->add(b);
}
void CORE::drop_nuke()
{
// create and set nuke up
float x = hge->Random_Int(20, 780);
nuke *n = new nuke(x, -32, 3.14 + atan((hge->Random_Int(200, 600)-x)/(-532)));
// add bomb to list
this->nukes->add(n);
}
void CORE::explode_bombs()
{
rocket *curr = this->rockets->first;
for(int idx=0; idx<this->rockets->length; idx++)
{
bomb *currb = this->bombs->first;
for(int idx2=0; idx2<this->bombs->length; idx2++)
{
if(curr->collides(currb) && curr->alive)
{
curr->explode();
currb->explode();
this->score += (100-currb->y/5);
}
if(currb->x < -10 || currb->x > 810) currb->explode();
currb = (bomb *)currb->next;
}
nuke *currn = this->nukes->first;
for(idx2=0; idx2<this->nukes->length; idx2++)
{
if(curr->collides(currn) && curr->alive)
{
curr->explode();
currn->explode();
this->score += (150-currn->y/7);
}
currn = (nuke *)currn->next;
}
curr = (rocket *)curr->next;
}
}
void CORE::repaint_sky()
{
for(int idx=0; idx<6; idx++)
{
if(colors[idx]<0) colors[idx] = 0;
if(colors[idx]>255) colors[idx] = 255;
}
long col1 = ARGB(0xFF, ((char)colors[0]), ((char)colors[1]), ((char)colors[2]));
long col2 = ARGB(0xFF, ((char)colors[3]), ((char)colors[4]), ((char)colors[5]));
sky->SetColor(col1, 0);
sky->SetColor(col1, 1);
sky->SetColor(col2, 2);
sky->SetColor(col2, 3);
}
bool CORE::collect_garbage()
{
// rockets
rocket *tmp, *curr = this->rockets->first;
for(int idx=0; idx<this->rockets->length; idx++)
{
if(curr->dead_time == 1)
{
this->fired++;
CloseHandle(curr->thread);
this->rockets->del(curr);
tmp = (rocket *)curr->next;
delete curr->body;
delete curr->trail;
delete curr;
curr = tmp;
}
}
// bombs
bomb *tmpb, *currb = this->bombs->first;
for(idx=0; idx<this->bombs->length; idx++)
{
if(currb->dead_time == 1)
{
CloseHandle(currb->thread);
if(currb->fatal)
{
alive = false;
return true;
}
this->hit++;
this->bombs->del(currb);
tmpb = (bomb *)currb->next;
delete currb->body;
delete currb->trail;
delete currb->explosion;
delete currb;
currb = tmpb;
}
}
// nukes
nuke *tmpn, *currn = this->nukes->first;
for(idx=0; idx<this->nukes->length; idx++)
{
if(currn->dead_time == 1)
{
CloseHandle(currn->thread);
if(currn->fatal)
{
alive = false;
return true;
}
this->hit++;
this->nukes->del(currn);
tmpn = (nuke *)currn->next;
delete currn->body;
delete currn->trail;
delete currn->explosion;
delete currn;
currn = tmpn;
}
}
return false;
}
void CORE::debug_render()
{
fnt->printf(5, 20, HGETEXT_LEFT, "Threads working: %i", bombs->length + rockets->length + nukes->length);
int y=32;
dynamic_spr *curr = rockets->first;
for(int idx=0; idx<rockets->length; idx++)
{
fnt->printf(5, y, HGETEXT_LEFT, "[addr %p, handle %i] rocket", curr, curr->thread);
curr = curr->next;
y+=12;
}
curr = bombs->first;
for(idx=0; idx<bombs->length; idx++)
{
fnt->printf(5, y, HGETEXT_LEFT, "[addr %p, handle %i] bomb", curr, curr->thread);
curr = curr->next;
y+=12;
}
curr = nukes->first;
for(idx=0; idx<nukes->length; idx++)
{
fnt->printf(5, y, HGETEXT_LEFT, "[addr %p, handle %i] nuke", curr, curr->thread);
curr = curr->next;
y+=12;
}
}