Browse Source

unsuccessfully tried to optimize shot collisions, now limiting to 2 faster shots.

Thomas B 1 month ago
parent
commit
1a759a6c18
2 changed files with 99 additions and 59 deletions
  1. 3
    1
      src/game.h
  2. 96
    58
      src/obj.c

+ 3
- 1
src/game.h View File

25
 #define HEALTH_MAX 0x1FF
25
 #define HEALTH_MAX 0x1FF
26
 #define HEALTH_SHIFT 1
26
 #define HEALTH_SHIFT 1
27
 
27
 
28
-#define SHOT_SPEED 23
28
+// TODO ?
29
+#define SHOT_SPEED 42 //23
30
+#define MAX_TRAVEL 64 //128
29
 
31
 
30
 int32_t game(void);
32
 int32_t game(void);
31
 
33
 

+ 96
- 58
src/obj.c View File

48
  */
48
  */
49
 #define MAX_DARK 2
49
 #define MAX_DARK 2
50
 #define MAX_LIGHT 2
50
 #define MAX_LIGHT 2
51
-#define MAX_SHOT 5
51
+#define MAX_SHOT 2 //5 /* TODO !! */
52
 #define MAX_SHOT_DARK 2
52
 #define MAX_SHOT_DARK 2
53
 #define MAX_SHOT_LIGHT 2
53
 #define MAX_SHOT_LIGHT 2
54
 #define MAX_OBJ ((4 * MAX_DARK) + (4 * MAX_LIGHT) + MAX_SHOT + MAX_SHOT_DARK + MAX_SHOT_LIGHT)
54
 #define MAX_OBJ ((4 * MAX_DARK) + (4 * MAX_LIGHT) + MAX_SHOT + MAX_SHOT_DARK + MAX_SHOT_LIGHT)
55
 
55
 
56
-#define MAX_TRAVEL 128
57
-
58
 #define POS_SCALE_OBJS 5
56
 #define POS_SCALE_OBJS 5
59
 #define POS_OBJS_MAX (INT16_MAX >> (8 - POS_SCALE_OBJS))
57
 #define POS_OBJS_MAX (INT16_MAX >> (8 - POS_SCALE_OBJS))
60
 #define POS_OBJS_MIN (-(INT16_MAX >> (8 - POS_SCALE_OBJS)) - 1)
58
 #define POS_OBJS_MIN (-(INT16_MAX >> (8 - POS_SCALE_OBJS)) - 1)
80
 #define RESPAWN_DISTANCE 100 // from center
78
 #define RESPAWN_DISTANCE 100 // from center
81
 #define PLACEMENT_DISTANCE 42 // relative to each other
79
 #define PLACEMENT_DISTANCE 42 // relative to each other
82
 
80
 
81
+#define CHECK_COL_AT_SHOTS
82
+
83
 struct obj {
83
 struct obj {
84
     uint8_t active;
84
     uint8_t active;
85
     enum SPRITES sprite;
85
     enum SPRITES sprite;
151
     *y_c = y;
151
     *y_c = y;
152
 }
152
 }
153
 
153
 
154
+static void obj_respawn_type(enum SPRITES spr, int8_t center_dist) {
155
+    while (obj_cnt[spr] < obj_max[spr]) {
156
+        int8_t x, y;
157
+        generate_coords(&x, &y, center_dist);
158
+        obj_add(spr, x, y, 0, 0);
159
+    }
160
+}
161
+
154
 static void obj_respawn(int8_t center_dist) {
162
 static void obj_respawn(int8_t center_dist) {
155
     for (uint8_t spr = SPR_LIGHT; spr <= SPR_SHOT_DARK; spr++) {
163
     for (uint8_t spr = SPR_LIGHT; spr <= SPR_SHOT_DARK; spr++) {
156
         if (spr == SPR_SHOT) {
164
         if (spr == SPR_SHOT) {
157
             continue;
165
             continue;
158
         }
166
         }
159
-
160
-        while (obj_cnt[spr] < obj_max[spr]) {
161
-            int8_t x, y;
162
-            generate_coords(&x, &y, center_dist);
163
-            obj_add(spr, x, y, 0, 0);
164
-        }
167
+        obj_respawn_type(spr, center_dist);
165
     }
168
     }
166
 }
169
 }
167
 
170
 
202
     return OBJ_ADDED;
205
     return OBJ_ADDED;
203
 }
206
 }
204
 
207
 
208
+static uint8_t handle_shot_col(uint8_t shot, uint8_t orb, int32_t *score, uint8_t is_splash) {
209
+    if ((abs(objs[shot].off_x - objs[orb].off_x) <= SHOT_RANGE)
210
+            && (abs(objs[shot].off_y - objs[orb].off_y) <= SHOT_RANGE)) {
211
+        sample_play(SFX_EXPL_ORB);
212
+
213
+        objs[orb].active = 0;
214
+
215
+        obj_cnt[objs[shot].sprite]--;
216
+        obj_cnt[objs[orb].sprite]--;
217
+
218
+        objs[shot].sprite = SPR_EXPL;
219
+        objs[shot].travel = 0;
220
+        objs[shot].frame = 0;
221
+        objs[shot].frame_index = 0;
222
+        objs[shot].frame_count = 4;
223
+        objs[shot].frame_duration = 4;
224
+        obj_cnt[SPR_EXPL]++;
225
+
226
+        // move explosion to center of orb instead of shot
227
+        objs[shot].off_x = objs[orb].off_x;
228
+        objs[shot].off_y = objs[orb].off_y;
229
+
230
+        // also would look kinda cool with shot speed still applied?
231
+        objs[shot].spd_x = 0;
232
+        objs[shot].spd_y = 0;
233
+
234
+        if (!is_splash) {
235
+            obj_respawn_type(objs[orb].sprite, RESPAWN_DISTANCE);
236
+        }
237
+
238
+        if (objs[orb].sprite == SPR_LIGHT) {
239
+            (*score) += SCORE_LARGE;
240
+        } else {
241
+            (*score) -= SCORE_LARGE;
242
+        }
243
+
244
+        return 1;
245
+    }
246
+
247
+    return 0;
248
+}
249
+
205
 int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater, uint8_t is_splash) BANKED {
250
 int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater, uint8_t is_splash) BANKED {
206
     int16_t damage = 0;
251
     int16_t damage = 0;
207
 
252
 
250
 #ifdef DESPAWN_RANGE
295
 #ifdef DESPAWN_RANGE
251
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
296
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
252
                     objs[i].active = 0;
297
                     objs[i].active = 0;
253
-                    obj_cnt[objs[i].sprite]--;
254
-                    obj_respawn(RESPAWN_DISTANCE);
298
+                    obj_cnt[SPR_DARK]--;
299
+                    obj_respawn_type(SPR_DARK, RESPAWN_DISTANCE);
255
                 }
300
                 }
256
 #endif // DESPAWN_RANGE
301
 #endif // DESPAWN_RANGE
257
 
302
 
271
                 if ((abs_off_x <= DAMAGE_RANGE) && (abs_off_y <= DAMAGE_RANGE)) {
316
                 if ((abs_off_x <= DAMAGE_RANGE) && (abs_off_y <= DAMAGE_RANGE)) {
272
                     damage += DAMAGE_INC;
317
                     damage += DAMAGE_INC;
273
                 }
318
                 }
319
+
320
+#ifndef CHECK_COL_AT_SHOTS
321
+                for (uint8_t shot = 0; shot < MAX_OBJ; shot++) {
322
+                    if ((!objs[shot].active) || (objs[shot].sprite != SPR_SHOT)) {
323
+                        continue;
324
+                    }
325
+
326
+                    if (handle_shot_col(shot, i, score, is_splash)) {
327
+                        break;
328
+                    }
329
+                }
330
+#endif // ! CHECK_COL_AT_SHOTS
274
                 break;
331
                 break;
275
 
332
 
276
             case SPR_LIGHT:
333
             case SPR_LIGHT:
277
 #ifdef DESPAWN_RANGE
334
 #ifdef DESPAWN_RANGE
278
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
335
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
279
                     objs[i].active = 0;
336
                     objs[i].active = 0;
280
-                    obj_cnt[objs[i].sprite]--;
281
-                    obj_respawn(RESPAWN_DISTANCE);
337
+                    obj_cnt[SPR_LIGHT]--;
338
+                    obj_respawn_type(SPR_LIGHT, RESPAWN_DISTANCE);
282
                 }
339
                 }
283
 #endif // DESPAWN_RANGE
340
 #endif // DESPAWN_RANGE
284
 
341
 
298
                 if ((abs_off_x <= HEALTH_RANGE) && (abs_off_y <= HEALTH_RANGE)) {
355
                 if ((abs_off_x <= HEALTH_RANGE) && (abs_off_y <= HEALTH_RANGE)) {
299
                     damage -= HEALTH_INC;
356
                     damage -= HEALTH_INC;
300
                 }
357
                 }
358
+
359
+#ifndef CHECK_COL_AT_SHOTS
360
+                for (uint8_t shot = 0; shot < MAX_OBJ; shot++) {
361
+                    if ((!objs[shot].active) || (objs[shot].sprite != SPR_SHOT)) {
362
+                        continue;
363
+                    }
364
+
365
+                    if (handle_shot_col(shot, i, score, is_splash)) {
366
+                        break;
367
+                    }
368
+                }
369
+#endif // ! CHECK_COL_AT_SHOTS
301
                 break;
370
                 break;
302
 
371
 
303
             case SPR_SHOT_DARK:
372
             case SPR_SHOT_DARK:
304
 #ifdef DESPAWN_RANGE
373
 #ifdef DESPAWN_RANGE
305
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
374
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
306
                     objs[i].active = 0;
375
                     objs[i].active = 0;
307
-                    obj_cnt[objs[i].sprite]--;
308
-                    obj_respawn(RESPAWN_DISTANCE);
376
+                    obj_cnt[SPR_SHOT_DARK]--;
377
+                    obj_respawn_type(SPR_SHOT_DARK, RESPAWN_DISTANCE);
309
                 }
378
                 }
310
 #endif // DESPAWN_RANGE
379
 #endif // DESPAWN_RANGE
311
 
380
 
312
                 if ((abs_off_x <= PICKUP_SMALL_RANGE) && (abs_off_y <= PICKUP_SMALL_RANGE)) {
381
                 if ((abs_off_x <= PICKUP_SMALL_RANGE) && (abs_off_y <= PICKUP_SMALL_RANGE)) {
313
                     (*score) -= SCORE_SMALL;
382
                     (*score) -= SCORE_SMALL;
314
                     objs[i].active = 0;
383
                     objs[i].active = 0;
315
-                    obj_cnt[objs[i].sprite]--;
316
-                    obj_respawn(RESPAWN_DISTANCE);
384
+                    obj_cnt[SPR_SHOT_DARK]--;
385
+                    obj_respawn_type(SPR_SHOT_DARK, RESPAWN_DISTANCE);
317
                 }
386
                 }
318
                 break;
387
                 break;
319
 
388
 
321
 #ifdef DESPAWN_RANGE
390
 #ifdef DESPAWN_RANGE
322
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
391
                 if ((abs_off_x >= DESPAWN_RANGE) || (abs_off_y >= DESPAWN_RANGE)) {
323
                     objs[i].active = 0;
392
                     objs[i].active = 0;
324
-                    obj_cnt[objs[i].sprite]--;
325
-                    obj_respawn(RESPAWN_DISTANCE);
393
+                    obj_cnt[SPR_SHOT_LIGHT]--;
394
+                    obj_respawn_type(SPR_SHOT_LIGHT, RESPAWN_DISTANCE);
326
                 }
395
                 }
327
 #endif // DESPAWN_RANGE
396
 #endif // DESPAWN_RANGE
328
 
397
 
329
                 if ((abs_off_x <= PICKUP_SMALL_RANGE) && (abs_off_y <= PICKUP_SMALL_RANGE)) {
398
                 if ((abs_off_x <= PICKUP_SMALL_RANGE) && (abs_off_y <= PICKUP_SMALL_RANGE)) {
330
                     (*score) += SCORE_SMALL;
399
                     (*score) += SCORE_SMALL;
331
                     objs[i].active = 0;
400
                     objs[i].active = 0;
332
-                    obj_cnt[objs[i].sprite]--;
333
-                    obj_respawn(RESPAWN_DISTANCE);
401
+                    obj_cnt[SPR_SHOT_LIGHT]--;
402
+                    obj_respawn_type(SPR_SHOT_LIGHT, RESPAWN_DISTANCE);
334
                 }
403
                 }
335
                 break;
404
                 break;
336
 
405
 
406
+#ifdef CHECK_COL_AT_SHOTS
337
             case SPR_SHOT:
407
             case SPR_SHOT:
338
-                for (uint8_t j = 0; j < MAX_OBJ; j++) {
339
-                    if ((!objs[j].active) || ((objs[j].sprite != SPR_LIGHT) && (objs[j].sprite != SPR_DARK))) {
408
+                for (uint8_t orb = 0; orb < MAX_OBJ; orb++) {
409
+                    if ((!objs[orb].active)
410
+                            || ((objs[orb].sprite != SPR_LIGHT) && (objs[orb].sprite != SPR_DARK))) {
340
                         continue;
411
                         continue;
341
                     }
412
                     }
342
 
413
 
343
-                    if ((abs(objs[i].off_x - objs[j].off_x) <= SHOT_RANGE)
344
-                            && (abs(objs[i].off_y - objs[j].off_y) <= SHOT_RANGE)) {
345
-                        sample_play(SFX_EXPL_ORB);
346
-
347
-                        objs[j].active = 0;
348
-
349
-                        obj_cnt[objs[i].sprite]--;
350
-                        obj_cnt[objs[j].sprite]--;
351
-
352
-                        objs[i].sprite = SPR_EXPL;
353
-                        objs[i].travel = 0;
354
-                        objs[i].frame = 0;
355
-                        objs[i].frame_index = 0;
356
-                        objs[i].frame_count = 4;
357
-                        objs[i].frame_duration = 4;
358
-                        obj_cnt[SPR_EXPL]++;
359
-
360
-                        // move explosion to center of orb instead of shot
361
-                        objs[i].off_x = objs[j].off_x;
362
-                        objs[i].off_y = objs[j].off_y;
363
-
364
-                        // also would look kinda cool with shot speed still applied?
365
-                        objs[i].spd_x = 0;
366
-                        objs[i].spd_y = 0;
367
-
368
-                        if (!is_splash) {
369
-                            obj_respawn(RESPAWN_DISTANCE);
370
-                        }
371
-
372
-                        if (objs[j].sprite == SPR_LIGHT) {
373
-                            (*score) += SCORE_LARGE;
374
-                        } else {
375
-                            (*score) -= SCORE_LARGE;
376
-                        }
377
-
414
+                    if (handle_shot_col(i, orb, score, is_splash)) {
378
                         break;
415
                         break;
379
                     }
416
                     }
380
                 }
417
                 }
381
                 break;
418
                 break;
419
+#endif // CHECK_COL_AT_SHOTS
382
 
420
 
383
             default:
421
             default:
384
                 break;
422
                 break;

Loading…
Cancel
Save