Browse Source

move obj movement into obj_act(), mask wrap of coordinates so items can be picked up after wrapping screens

Thomas B 1 month ago
parent
commit
c31a6d8f38
5 changed files with 27 additions and 20 deletions
  1. 3
    3
      src/game.c
  2. 3
    0
      src/game.h
  3. 1
    1
      src/main.c
  4. 19
    15
      src/obj.c
  5. 1
    1
      src/obj.h

+ 3
- 3
src/game.c View File

@@ -320,8 +320,8 @@ int32_t game(void) NONBANKED {
320 320
             redraw = 1;
321 321
         }
322 322
 
323
-        pos_x += spd_x;
324
-        pos_y += spd_y;
323
+        pos_x = (pos_x + spd_x) & POS_MASK_BG;
324
+        pos_y = (pos_y + spd_y) & POS_MASK_BG;
325 325
         move_bkg(pos_x >> POS_SCALE_BG, pos_y >> POS_SCALE_BG);
326 326
 
327 327
         uint8_t hiwater = SPR_NUM_START;
@@ -333,7 +333,7 @@ int32_t game(void) NONBANKED {
333 333
             hiwater = ship_hiwater;
334 334
         }
335 335
 
336
-        obj_draw(spd_x, spd_y, &hiwater);
336
+        obj_draw(&hiwater);
337 337
         status(health >> HEALTH_SHIFT, power >> POWER_SHIFT, &hiwater);
338 338
 
339 339
         hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);

+ 3
- 0
src/game.h View File

@@ -29,7 +29,10 @@
29 29
 #define SPEED_MAX_IDLE 16
30 30
 
31 31
 #define POS_SCALE_OBJS 5
32
+#define POS_MASK_OBJS 0x1FFF
33
+
32 34
 #define POS_SCALE_BG 6
35
+#define POS_MASK_BG 0x3FFF
33 36
 
34 37
 #define POWER_MAX 0x1FF
35 38
 #define POWER_SHIFT 1

+ 1
- 1
src/main.c View File

@@ -135,7 +135,7 @@ static void splash(void) NONBANKED {
135 135
         }
136 136
 
137 137
         uint8_t hiwater = SPR_NUM_START;
138
-        obj_draw(0, 0, &hiwater);
138
+        obj_draw(&hiwater);
139 139
         hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);
140 140
 
141 141
         vsync();

+ 19
- 15
src/obj.c View File

@@ -131,12 +131,30 @@ enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_
131 131
 
132 132
 int16_t obj_act(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score) NONBANKED {
133 133
     int16_t damage = 0;
134
+    int16_t spd_x = *spd_off_x;
135
+    int16_t spd_y = *spd_off_y;
134 136
 
135 137
     for (uint8_t i = 0; i < MAX_OBJ; i++) {
136 138
         if (!objs[i].active) {
137 139
             continue;
138 140
         }
139 141
 
142
+        // move objects by their speed and compensate for movement of the background / ship
143
+        objs[i].off_x = (objs[i].off_x + objs[i].spd_x - spd_x) & POS_MASK_OBJS;
144
+        objs[i].off_y = (objs[i].off_y + objs[i].spd_y - spd_y) & POS_MASK_OBJS;
145
+
146
+        // only update travel time if we're actually moving
147
+        if ((objs[i].spd_x != 0) || (objs[i].spd_y != 0)) {
148
+            objs[i].travel += 1;
149
+        }
150
+
151
+        // remove objects that have traveled for too long
152
+        if (objs[i].travel >= MAX_TRAVEL) {
153
+            objs[i].active = 0;
154
+            continue;
155
+        }
156
+
157
+        // handle collission
140 158
         switch (objs[i].sprite) {
141 159
             case SPR_DARK:
142 160
                 if ((abs(objs[i].off_x) <= GRAVITY_RANGE) && (abs(objs[i].off_y) <= GRAVITY_RANGE)) {
@@ -220,26 +238,12 @@ int16_t obj_act(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score) NONBANKE
220 238
     return damage;
221 239
 }
222 240
 
223
-void obj_draw(int16_t spd_x, int16_t spd_y, uint8_t *hiwater) NONBANKED {
241
+void obj_draw(uint8_t *hiwater) NONBANKED {
224 242
     for (uint8_t i = 0; i < MAX_OBJ; i++) {
225 243
         if (!objs[i].active) {
226 244
             continue;
227 245
         }
228 246
 
229
-        // move objects by their speed and compensate for movement of the background / ship
230
-        objs[i].off_x += objs[i].spd_x - spd_x;
231
-        objs[i].off_y += objs[i].spd_y - spd_y;
232
-
233
-        // only update travel time if we're actually moving
234
-        if ((objs[i].spd_x != 0) || (objs[i].spd_y != 0)) {
235
-            objs[i].travel += 1;
236
-        }
237
-
238
-        // remove objects that have traveled for too long
239
-        if (objs[i].travel >= MAX_TRAVEL) {
240
-            objs[i].active = 0;
241
-        }
242
-
243 247
         spr_draw(objs[i].sprite, FLIP_NONE, objs[i].off_x >> POS_SCALE_OBJS, objs[i].off_y >> POS_SCALE_OBJS, 0, hiwater);
244 248
     }
245 249
 }

+ 1
- 1
src/obj.h View File

@@ -32,6 +32,6 @@ enum OBJ_STATE {
32 32
 void obj_init(void);
33 33
 enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y);
34 34
 int16_t obj_act(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score);
35
-void obj_draw(int16_t spd_x, int16_t spd_y, uint8_t *hiwater);
35
+void obj_draw(uint8_t *hiwater);
36 36
 
37 37
 #endif // __OBJ_H__

Loading…
Cancel
Save