-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffects.c
388 lines (343 loc) · 15.4 KB
/
effects.c
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
/*******************************************************************************************
** Author: Chris Spravka
** Date: 09 OCT 2018
** Description: effects implementation file (on screen, not-physics-based objects)
NOTES
1. figure out how you want to attach these to sprites (which seems logical)
2. you could instantiate new effect for every key punch/action/result, which would look cooler (appropriately persistent effects) but be
an unholy mess of spawning/malloc'ing things and then killing them
3. do better with # of disps/cps and the timing between them
*******************************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#include <string.h>
#include <math.h>
#include "effects.h"
void initEffect(struct effect *inputEffect, int ID, float ttl, float xLoc, float yLoc) {
inputEffect->ID = ID;
inputEffect->ttl = ttl;
inputEffect->xLoc = xLoc;
inputEffect->yLoc = yLoc;
inputEffect->start =-1;
inputEffect->numDisps=0;
inputEffect->xSize=0;
inputEffect->ySize=0;
}
void modEffect(int effectIndex, float start, float xLoc, float yLoc, struct gameState * state) {
if (effectIndex == -1) {
effectIndex = state->allEffects->numEffects -1;
}
struct effect *e1 = state->allEffects->effectArr[effectIndex];
if (e1->xLoc > -1000) {
e1->xLoc = xLoc - e1->xSize/2;
}
if (e1->yLoc > -1000) {
e1->yLoc = yLoc - e1->ySize/2;
}
e1->start = start;
}
void initDispPair(struct effect *inputEffect, int cpIn, int attrIn, const char * dispIn) {
// error/length checking?
inputEffect->dispArr[inputEffect->numDisps] = malloc(sizeof(struct dispPair));
inputEffect->dispArr[inputEffect->numDisps]->colorPair = cpIn;
inputEffect->dispArr[inputEffect->numDisps]->attr = attrIn;
strcpy(inputEffect->dispArr[inputEffect->numDisps]->disp, dispIn);
inputEffect->numDisps++;
}
void setEffectSize(struct effect * effectIn, int x, int y) {
effectIn->xSize = x;
effectIn->ySize = y;
}
// instead of this, i think i would like to read these from a file initially or something
void initEffectLibrary(struct effectList *localList) {
localList->numEffects =0;
// right thrust
struct effect * rtThrust = malloc(sizeof(struct effect));
initEffect(rtThrust, 0, 0.5, -3, 1);
rtThrust->parentID = 0;
initDispPair(rtThrust, 2, 0, " o");
initDispPair(rtThrust, 3, 0, " O ");
initDispPair(rtThrust, 1, 0, ") ");
initDispPair(rtThrust, 1, A_DIM, "| ");
localList->effectArr[localList->numEffects] = rtThrust;
localList->numEffects++;
// left thrust 1
struct effect * ltThrust1 = malloc(sizeof(struct effect));
initEffect(ltThrust1, 0, 0.5, 4, 2);
ltThrust1->parentID = 0;
initDispPair(ltThrust1, 2, 0, "o ");
initDispPair(ltThrust1, 3, 0, " O ");
initDispPair(ltThrust1, 1, 0, " (");
localList->effectArr[localList->numEffects] = ltThrust1;
localList->numEffects++;
// up thrust 1
struct effect * upThrust1 = malloc(sizeof(struct effect));
//initEffect(upThrust1, 0, 0.5, 0,1,"o","\nO","\n\n_",2, 3, 1);
initEffect(upThrust1, 0, 0.5, 2, 3);
upThrust1->parentID = 0;
initDispPair(upThrust1, 2, 0, "o");
initDispPair(upThrust1, 3, 0, "\nO ");
initDispPair(upThrust1, 1, 0, "\n\n_");
localList->effectArr[localList->numEffects] = upThrust1;
localList->numEffects++;
// left thrust 2
struct effect * ltThrust2 = malloc(sizeof(struct effect));
initEffect(ltThrust2, 0, 0.5, 4, 0);
ltThrust2->parentID = 0;
initDispPair(ltThrust2, 2, 0, "o ");
initDispPair(ltThrust2, 3, 0, " O ");
initDispPair(ltThrust2, 1, 0, " (");
localList->effectArr[localList->numEffects] = ltThrust2;
localList->numEffects++;
//down thrust 1
struct effect * dwThrust1 = malloc(sizeof(struct effect));
//initEffect(dwThrust1, 0, 0.5, 0,-3,"\n\no","\nO","-\n\n",2, 3, 1);
initEffect(dwThrust1, 0, 0.5, 2, -3);
dwThrust1->parentID = 0;
initDispPair(dwThrust1, 2, 0, "\n\no");
initDispPair(dwThrust1, 3, 0, "\nO ");
initDispPair(dwThrust1, 1, 0, "-\n\n");
localList->effectArr[localList->numEffects] = dwThrust1;
localList->numEffects++;
// down thrust 2 (unused)
struct effect * dwThrust2 = malloc(sizeof(struct effect));
//initEffect(dwThrust2, 0, 0.5, 23,-3,"\n\no","\nO","-\n\n",2, 3, 1);
initEffect(dwThrust2, 0, 0.5, 23,-3);
dwThrust2->parentID = 0;
initDispPair(dwThrust2, 2, 0, "\n\no");
initDispPair(dwThrust2, 3, 0, "\nO ");
initDispPair(dwThrust2, 1, 0, "-\n\n");
localList->effectArr[localList->numEffects] = dwThrust2;
localList->numEffects++;
//ship explosion 1
struct effect * shipEx1 = malloc(sizeof(struct effect));
shipEx1->parentID = 0;
initEffect(shipEx1, 0, 1, -1,-1); // effect, ID (not used), ttl, x, y
initDispPair(shipEx1, 2, 0, "\n\n\n o \n\n\n"); // effect, colorPair, attr, char *
initDispPair(shipEx1, 3, 0, "\n\n \\*O*/ \n -O*O- \n /*O*\\ \n\n");
initDispPair(shipEx1, 1, 0, "\no-+|.o-\n* '\n. o\n*^+-|\\o\n");
initDispPair(shipEx1, 1, A_DIM, ".......\n. .\n. .\n. .\n.......");
setEffectSize(shipEx1, 7, 8);
localList->effectArr[localList->numEffects] = shipEx1;
localList->numEffects++;
/*
//ship explosion 2
struct effect * shipEx2 = malloc(sizeof(struct effect));
shipEx1->parentID = 0;
initEffect(shipEx2, 0, 3, 0,0); // effect, ID (not used), ttl, x, y
initDispPair(shipEx2, 4, 0, " --\n /\n --/\\\n ---/"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 1, A_DIM, " **\n *\n ****\n ****"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 1, A_DIM, " **\n *\n ****\n ****"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 14, 0, "\n\n O"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 3, A_BOLD, "\n \\|/\n = =\n /|\\"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 3, A_BOLD, " \\ || /\n \n -- ** --\n \n / || \\"); // effect, colorPair, attr, char *
//setEffectSize(shipEx2, 7, 8);
localList->effectArr[localList->numEffects] = shipEx2;
localList->numEffects++;
*/
//ship explosion 2
struct effect * shipEx2 = malloc(sizeof(struct effect));
shipEx1->parentID = 0;
// correct length is 3!!
initEffect(shipEx2, 0, 3, 0,0); // effect, ID (not used), ttl, x, y
initDispPair(shipEx2, 4, 0, "\n [\n =->\n ["); // effect, colorPair, attr, char *
initDispPair(shipEx2, 1, A_DIM, "\n *\n ***\n *"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 1, A_DIM, "\n *\n ***\n *"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 14, 0, "\n\n O"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 3, A_BOLD, "\n \\|/\n = =\n /|\\"); // effect, colorPair, attr, char *
initDispPair(shipEx2, 3, A_BOLD, " \\ || /\n \n -- ** --\n \n / || \\"); // effect, colorPair, attr, char *
// initDispPairSprite(ship, 1, 0, " |-[\n}===->\n |-[");
//setEffectSize(shipEx2, 7, 8);
localList->effectArr[localList->numEffects] = shipEx2;
localList->numEffects++;
//ship explosion 3
struct effect * shipEx3 = malloc(sizeof(struct effect));
shipEx1->parentID = 0;
initEffect(shipEx3, 0, 3.1, 0,0); // effect, ID (not used), ttl, x, y
initDispPair(shipEx3, 2, 0, "\n\n -"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n \\"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n |"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n /"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n -"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n \\"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n |"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n /"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n -"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n \\"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n |"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n /"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n -"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n \\"); // effect, colorPair, attr, char *
initDispPair(shipEx3, 2, 0, "\n\n |"); // effect, colorPair, attr, char *
//setEffectSize(shipEx2, 7, 8);
localList->effectArr[localList->numEffects] = shipEx3;
localList->numEffects++;
//laser effect
struct effect * lasEff = malloc(sizeof(struct effect));
lasEff->parentID = 0;
initEffect(lasEff, 0, 1, 5,5); // effect, ID (not used), ttl, x, y
char temp[MAX_DISP_SUBSIZE];
strcpy(temp, "-");
int i;;
for (i=0; i< MAX_DISP_SUBSIZE-2; i++) {
strcat(temp, "-");
}
initDispPair(lasEff, 8, 0, temp); // effect, colorPair, attr, char *
initDispPair(lasEff, 8, A_DIM, temp);
initDispPair(lasEff, 1, A_DIM, temp);
//setEffectSize(lasEff, 7, 8);
localList->effectArr[localList->numEffects] = lasEff;
localList->numEffects++;
// ammo effect
struct effect * ammo1 = malloc(sizeof(struct effect));
shipEx1->parentID = 0;
initEffect(ammo1, 0, 0.1, 0,0); // effect, ID (not used), ttl, x, y
initDispPair(ammo1, 1, 0, " "); // effect, colorPair, attr, char *
localList->effectArr[localList->numEffects] = ammo1;
localList->numEffects++;
// generate more effects!
// .. //
}
void addEffect(int ID, int parentID, struct gameState * state, struct library * lib) {
// make a copy so different instantiations can behave differently
struct effect * temp = malloc(sizeof(struct effect));
memcpy(temp,lib->allEffects->effectArr[ID],sizeof(struct effect));
// also make sure you are copying the disps, even though you have no intention of making them behave differently
int i;
for (i=0; i < temp->numDisps; i++) {
struct dispPair * dpTemp = malloc(sizeof(struct dispPair));
memcpy(dpTemp, lib->allEffects->effectArr[ID]->dispArr[i], sizeof(struct dispPair));
temp->dispArr[i] = dpTemp;
}
temp->parentID = parentID; // this doesn't stay pertinent for long
state->allEffects->effectArr[state->allEffects->numEffects] = temp;
// goes both ways - > effect->sprite and sprite-> effect, this is needed because how how frequently sprites are
// created and destroyed and index values change
state->allSprites->spriteArr[parentID]->effectIDs[state->allSprites->spriteArr[parentID]->numEffects] = state->allEffects->numEffects;
state->allSprites->spriteArr[parentID]->numEffects++;
state->allEffects->numEffects++;
}
// rewrote to cycle through sprites first (more robust for timing!)
void printEffect(WINDOW* window, struct gameState * state) {
int effectCount = 0, spriteCount=0;
struct spriteList * localSpriteList = state->allSprites;
if (state->skyReady && state->skyReady <3) {
wbkgd(window, COLOR_PAIR(15));
state->skyReady ++;
}
for (spriteCount=0; spriteCount < localSpriteList->numSprites; spriteCount++) {
for (effectCount=0; effectCount < localSpriteList->spriteArr[spriteCount]->numEffects; effectCount++) {
struct effect * effectIn = state->allEffects->effectArr[localSpriteList->spriteArr[spriteCount]->effectIDs[effectCount]];
char tdisp[MAX_DISP_SUBSIZE];
// results based on time
int dispIndex = 0;
if (effectIn->start > 0) {
if ((state->time-effectIn->start) < effectIn->ttl) {
dispIndex = (int)effectIn->numDisps*((state->time-effectIn->start)/effectIn->ttl);
//mvwprintw(window,20,20,"disp:%i, dt:%f, ttl:%f", dispIndex, time-effectIn->start, effectIn->ttl);
strcpy(tdisp, effectIn->dispArr[dispIndex]->disp);
wcolor_set(window, effectIn->dispArr[dispIndex]->colorPair, NULL);
wattron(window, effectIn->dispArr[dispIndex]->attr);
}
// turn it off and reset it
else {
strcpy(tdisp, "");
effectIn->start = -1;
wcolor_set(window, 1, NULL);
wattroff(window, effectIn->dispArr[dispIndex]->attr);
// gross
if (localSpriteList->spriteArr[spriteCount]->markedForDeath > 0) {
localSpriteList->spriteArr[spriteCount]->markedForDeath=-1;
}
}
// decompose the display string to print through the vertical axis
// you can't just you /n in curses because it will start all the back at the front of the line, not
// where you started printing
int i=0, j=0, n=0;
//int dum=0;
char temp[256];
for (i=0; i<= strlen(tdisp); i++) {
// render each line individually, but keep track of where you left off in the sprite->disp with 'n'
if (tdisp[i] == '\n' || tdisp[i] == '\0') {
memcpy(temp, &(tdisp[n]), (i-n)*sizeof(char));
// manually terminate your new string
temp[i-n] = '\0';
// print to buffer but don't refresh here
int dx=0;
// this little ditty will prevent it from overprinting the spaces, so you don't get
// a big dumb background block around your sprite
while (dx < strlen(temp)) {
if (temp[dx] != ' ') {
mvwprintw(window, localSpriteList->spriteArr[spriteCount]->yLoc + effectIn->yLoc+j, localSpriteList->spriteArr[spriteCount]->xLoc + effectIn->xLoc+(float)dx, "%c", temp[dx]);
}
dx++;
}
// this is i+1 to skip the newline character in the next substring
n=i+1;
j++;
}
}
wattroff(window, effectIn->dispArr[dispIndex]->attr);
}
}
}
}
void printEffectServer(struct gameState * state) {
int effectCount = 0, spriteCount=0;
struct spriteList * localSpriteList = state->allSprites;
if (state->skyReady && state->skyReady <3) {
state->skyReady ++;
}
for (spriteCount=0; spriteCount < localSpriteList->numSprites; spriteCount++) {
for (effectCount=0; effectCount < localSpriteList->spriteArr[spriteCount]->numEffects; effectCount++) {
struct effect * effectIn = state->allEffects->effectArr[localSpriteList->spriteArr[spriteCount]->effectIDs[effectCount]];
char tdisp[MAX_DISP_SUBSIZE];
// results based on time
int dispIndex = 0;
if (effectIn->start > 0) {
if ( !((state->time-effectIn->start) > effectIn->ttl) ) {
dispIndex = (int)effectIn->numDisps*((state->time-effectIn->start)/effectIn->ttl);
strcpy(tdisp, effectIn->dispArr[dispIndex]->disp);
}
// turn it off and reset it
else {
strcpy(tdisp, "");
effectIn->start = -1;
// gross
if (localSpriteList->spriteArr[spriteCount]->markedForDeath > 0) {
localSpriteList->spriteArr[spriteCount]->markedForDeath=-1;
}
}
}
}
}
}
// fix me - you need to regularly call this function and look for defunct effects
// in order to stay under the limit (and manage your memory better)
// i think you really want a linked-list here, so you don't lose track of folks
void delEffect(struct gameState * state, int parentIndex) {
// fix and clean this up
// find the effect
struct sprite * spriteIn = state->allSprites->spriteArr[parentIndex];
int i,j;
for (i=0; i<spriteIn->numEffects; i++) {
for (j=0; j<state->allEffects->effectArr[spriteIn->effectIDs[i]]->numDisps; j++) {
free(state->allEffects->effectArr[spriteIn->effectIDs[i]]->dispArr[j]);
}
free(state->allEffects->effectArr[spriteIn->effectIDs[i]]);
}
// this only works because you are currently killing the last thing you
// gave an effect to
state->allEffects->numEffects--;
}
void freeEffectList(struct effectList *localList) {
int i=0, j=0;
for (i=0; i< localList->numEffects; i++) {
for (j=0; j<localList->effectArr[i]->numDisps; j++) {
free(localList->effectArr[i]->dispArr[j]);
}
free(localList->effectArr[i]);
localList->effectArr[i] = NULL;
}
}