forked from EXL/NXEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caret.cpp
440 lines (342 loc) · 8.26 KB
/
caret.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
// handle carets; a simplified type of objects used for visual effects.
// carets have no interaction with real objects, and are always drawn
// on top of all other objects and in front even of map foreground tiles.
#include "nx.h"
#include <math.h>
#include "common/llist.h"
#include "caret.fdh"
Caret *firstcaret = NULL;
Caret *lastcaret = NULL;
static int _effecttype = EFFECT_NONE;
bool Carets::init(void)
{
firstcaret = NULL;
lastcaret = NULL;
return 0;
}
void Carets::close(void)
{
Carets::DestroyAll();
}
/*
void c------------------------------() {}
*/
Caret *CreateCaret(int x, int y, int sprite, void (*ontick)(Caret *c), \
int xinertia, int yinertia)
{
Caret *c = new Caret;
memset(c, 0, sizeof(Caret));
c->x = x;
c->y = y;
c->xinertia = xinertia;
c->yinertia = yinertia;
c->sprite = sprite;
c->OnTick = ontick;
c->effecttype = _effecttype;
LL_ADD_END(c, prev, next, firstcaret, lastcaret);
return c;
}
void Caret::Delete()
{
this->deleted = true;
}
void Caret::Destroy()
{
LL_REMOVE(this, prev, next, firstcaret, lastcaret);
delete this;
}
void Caret::MoveAtDir(int dir, int speed)
{
this->xinertia = 0;
this->yinertia = 0;
switch(dir)
{
case LEFT: this->xinertia = -speed; break;
case RIGHT: this->xinertia = speed; break;
case UP: this->yinertia = -speed; break;
case DOWN: this->yinertia = speed; break;
}
}
/*
void c------------------------------() {}
*/
void Carets::DrawAll(void)
{
Caret *c = firstcaret;
Caret *next;
int scr_x, scr_y;
while(c)
{
next = c->next;
if (c->deleted)
{
c->Destroy();
}
else
{
// do caret ai
(*c->OnTick)(c);
// move caret
c->x += c->xinertia;
c->y += c->yinertia;
// get caret's onscreen position
// since caret's are all short-lived we just assume it's still onscreen
// and let SDL's clipping handle it if not.
if (!c->invisible && !c->deleted) // must check deleted again in case handler_function set it
{
scr_x = (c->x >> CSF) - (map.displayed_xscroll >> CSF);
scr_y = (c->y >> CSF) - (map.displayed_yscroll >> CSF);
scr_x -= sprites[c->sprite].frame[c->frame].dir[0].drawpoint.x;
scr_y -= sprites[c->sprite].frame[c->frame].dir[0].drawpoint.y;
draw_sprite(scr_x, scr_y, c->sprite, c->frame, RIGHT);
}
}
c = next;
}
}
int Carets::CountByEffectType(int type)
{
int count = 0;
Caret *c = firstcaret;
while(c)
{
if (c->effecttype == type) count++;
c = c->next;
}
return count;
}
int Carets::DeleteByEffectType(int type)
{
int count = 0;
Caret *c = firstcaret;
while(c)
{
if (c->effecttype == type) c->Delete();
c = c->next;
}
return count;
}
void Carets::DestroyAll(void)
{
while(firstcaret)
firstcaret->Destroy();
}
/*
void c------------------------------() {}
*/
// generates a caret-based effect at x, y. Most sprites used for carets have the
// drawpoint at their center so the effect is generally centered at that position.
//
// an effect can be just a convenience function for creating a caret
// with a particular sprite/ai combo, or it can produce a group of carets
// which are always seen together (e.g. bonkplus or bloodspatter).
Caret *effect(int x, int y, int effectno)
{
Caret *c;
int i;
// tell CreateCaret what kind of effect we're spawning
_effecttype = effectno;
switch(effectno)
{
case EFFECT_STARSOLID: c = CreateCaret(x, y, SPR_STAR_SOLID, caret_animate3); break;
case EFFECT_STARPOOF: c = CreateCaret(x, y, SPR_STAR_POOF, caret_animate3); break;
case EFFECT_FISHY: c = CreateCaret(x, y, SPR_FISHY, caret_fishy); break;
case EFFECT_BOOMFLASH: c = CreateCaret(x, y, SPR_BOOMFLASH, caret_animate3); break;
case EFFECT_BUBBLE_BURST: c = CreateCaret(x, y, SPR_BUBBLE_BURST, caret_animate3); break;
case EFFECT_SPUR_HIT: c = CreateCaret(x, y, SPR_SPUR_HIT, caret_spur_hit); break;
case EFFECT_ZZZZ: c = CreateCaret(x, y, SPR_ZZZZ, caret_zzzz); break;
case EFFECT_LEVELUP: c = CreateCaret(x, y, SPR_LEVELUP, caret_playertext); break;
case EFFECT_LEVELDOWN: c = CreateCaret(x, y, SPR_LEVELDOWN, caret_playertext); break;
case EFFECT_BONUSFLASH: c = CreateCaret(x, y, SPR_SMOKE_CLOUD, caret_bonusflash); break;
case EFFECT_HEY: c = CreateCaret(x, y, SPR_HEY, caret_hey); break;
case EFFECT_EMPTY: c = CreateCaret(x, y, SPR_EMPTY, caret_playertext); break;
case EFFECT_SMOKETRAIL: c = CreateCaret(x, y, SPR_SMOKETRAIL, caret_animate2); break;
case EFFECT_SMOKETRAIL_SLOW:
c = CreateCaret(x, y, SPR_SMOKETRAIL, caret_animate3);
break;
case EFFECT_GUNFISH_BUBBLE:
{
c = CreateCaret(x-(3<<CSF), y-(3<<CSF), SPR_GUNFISH_BUBBLE, caret_gunfish_bubble); break;
}
break;
case EFFECT_LAVA_SPLASH:
{
c = CreateCaret(x-(3<<CSF), y-(3<<CSF), SPR_LAVA_DRIP_SPLASH, caret_gunfish_bubble); break;
}
break;
case EFFECT_GHOST_SPARKLE:
{
c = CreateCaret(x, y, SPR_GHOST_SPARKLE, caret_ghost_sparkle);
c->yinertia = random(-0x600, -0x200);
}
break;
// "blood" spatters from shot hitting enemy
case EFFECT_BLOODSPLATTER:
{
for(i=0;i<3;i++)
{
c = CreateCaret(x, y, SPR_BLOODHIT, caret_animate3);
vector_from_angle(random(0, 255), (2<<CSF), &c->xinertia, &c->yinertia);
}
}
break;
// two little blinky stars when player bonks his head on the ceiling
case EFFECT_BONKPLUS:
{
for(i=0;i<2;i++)
{
c = CreateCaret(x, y, SPR_BONKHEADPLUS, caret_bonkplus);
c->xinertia = random(-0x600, 0x600);
c->yinertia = random(-0x200, 0x200);
//uint8_t angle = random(-14, 14);
//if (random(0, 1)) angle += 128;
//vector_from_angle(angle, random(0x200, 0x384), &c->xinertia, &c->yinertia);
}
}
break;
case EFFECT_QMARK:
{
// only 1 question mark is ever shown at a time
DeleteEffectsOfType(EFFECT_QMARK);
c = CreateCaret(x, y, SPR_QMARK, caret_qmark);
}
break;
default:
staterr("effect: invalid effect type %d", effectno);
return NULL;
}
_effecttype = EFFECT_NONE;
return c;
}
/*
void c------------------------------() {}
*/
void caret_animate1(Caret *c)
{
c->animdie(0);
}
void caret_animate2(Caret *c)
{
c->animdie(1);
}
void caret_animate3(Caret *c)
{
c->animdie(2);
}
void Caret::anim(int speed)
{
Caret * const &c = this;
if (++c->animtimer > speed)
{
c->animtimer = 0;
if (++c->frame >= sprites[c->sprite].nframes)
c->frame = 0;
}
}
void Caret::animdie(int speed)
{
Caret * const &c = this;
if (++c->animtimer > speed)
{
c->animtimer = 0;
if (++c->frame >= sprites[c->sprite].nframes)
c->Delete();
}
}
/*
void c------------------------------() {}
*/
// flickers rapidly and decels at exponential speed.
// used for the "bonkplus" effect when you bonk your head
void caret_bonkplus(Caret *c)
{
c->xinertia *= 4; c->xinertia /= 5;
c->yinertia *= 4; c->yinertia /= 5;
c->invisible = (++c->timer & 2);
if (c->timer > 20)
c->Delete();
}
void caret_fishy(Caret *c)
{
c->yinertia -= 16;
c->animdie(4);
}
void caret_spur_hit(Caret *c)
{
c->timer++;
c->frame = (c->timer / 2) % 3;
if (c->timer > 24)
c->Delete();
}
// "Level Up", "Level Down", and "Empty" texts
void caret_playertext(Caret *c)
{
int spd, stop;
c->anim(1);
// "EMPTY" text goes twice as fast as "Level" text
if (c->sprite == SPR_EMPTY)
{
spd = 2;
stop = 18;
}
else
{
spd = 1;
stop = 20;
}
c->timer += spd;
if (c->timer < 80)
{
if (c->timer < stop)
{
c->y -= (spd << CSF);
}
}
else
{
c->Delete();
}
}
// ? effect when you press down with no object around to activate
void caret_qmark(Caret *c)
{
if (++c->timer < 40)
{
if (c->timer < 7)
{
c->y -= (3 << CSF);
}
}
else
{
c->Delete();
}
}
void caret_bonusflash(Caret *c)
{
if (++c->timer == 4)
c->Delete();
}
void caret_hey(Caret *c)
{
if (++c->timer > 30) c->Delete();
if (c->timer < 5) c->y -= (1<<CSF);
}
void caret_gunfish_bubble(Caret *c)
{
c->animdie(5);
c->yinertia += 0x40;
if (c->yinertia >= 0x5ff) c->yinertia = 0x5ff;
}
void caret_ghost_sparkle(Caret *c)
{
c->invisible = (++c->timer & 2);
if (c->timer > 20)
c->Delete();
}
void caret_zzzz(Caret *c)
{
c->animdie(5);
c->x += 0x80;
c->y -= 0x80;
}