Browse Source

better gravity, simple collision and health reduction

Thomas B 2 months ago
parent
commit
4b8e24a486
4 changed files with 35 additions and 9 deletions
  1. 8
    1
      src/game.c
  2. 2
    2
      src/game.h
  3. 24
    5
      src/obj.c
  4. 1
    1
      src/obj.h

+ 8
- 1
src/game.c View File

@@ -153,7 +153,14 @@ void game(void) {
153 153
         }
154 154
 
155 155
         // TODO
156
-        //obj_act(pos_x, pos_y, &spd_x, &spd_y);
156
+        uint8_t damage = obj_act(&spd_x, &spd_y);
157
+        if (health > damage) {
158
+            health -= damage;
159
+        } else if (health < damage) {
160
+            health = 0;
161
+
162
+            // TODO
163
+        }
157 164
 
158 165
         // adjust speed down when not moving
159 166
         if (!(acc & ACC_X)) {

+ 2
- 2
src/game.h View File

@@ -23,8 +23,8 @@
23 23
 #define SPEED_INC 1
24 24
 #define SPEED_DEC 1
25 25
 
26
-#define SPEED_MAX_ACC 16
27
-#define SPEED_MAX_IDLE 12
26
+#define SPEED_MAX_ACC 23
27
+#define SPEED_MAX_IDLE 16
28 28
 
29 29
 #define POS_SCALE_OBJS 5
30 30
 #define POS_SCALE_BG 6

+ 24
- 5
src/obj.c View File

@@ -18,6 +18,7 @@
18 18
  */
19 19
 
20 20
 #include <gbdk/platform.h>
21
+#include <stdint.h>
21 22
 #include <string.h>
22 23
 #include <stdlib.h>
23 24
 
@@ -116,9 +117,11 @@ enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_
116 117
     return OBJ_ADDED;
117 118
 }
118 119
 
119
-void obj_act(int16_t pos_x, int16_t pos_y, int16_t *spd_off_x, int16_t *spd_off_y) {
120
-    pos_x += DEVICE_SPRITE_PX_OFFSET_X + (DEVICE_SCREEN_PX_WIDTH / 2) - 16;
121
-    pos_y += DEVICE_SPRITE_PX_OFFSET_Y + (DEVICE_SCREEN_PX_HEIGHT / 2);
120
+#define GRAVITY_RANGE (32 << POS_SCALE_OBJS)
121
+#define GRAVITY_SHIFT (POS_SCALE_OBJS + 4)
122
+
123
+uint8_t obj_act(int16_t *spd_off_x, int16_t *spd_off_y) {
124
+    uint8_t damage = 0;
122 125
 
123 126
     for (uint8_t i = 0; i < MAX_OBJ; i++) {
124 127
         if (!objs[i].active) {
@@ -127,14 +130,30 @@ void obj_act(int16_t pos_x, int16_t pos_y, int16_t *spd_off_x, int16_t *spd_off_
127 130
 
128 131
         switch (objs[i].sprite) {
129 132
             case SPR_DARK: {
130
-                *spd_off_x += (objs[i].off_x - pos_x) >> 8;
131
-                *spd_off_y += (objs[i].off_y - pos_y) >> 8;
133
+                if ((abs(objs[i].off_x) <= GRAVITY_RANGE) && (abs(objs[i].off_y) <= GRAVITY_RANGE)) {
134
+                    if (objs[i].off_x > 0) {
135
+                        *spd_off_x += (GRAVITY_RANGE - objs[i].off_x) >> GRAVITY_SHIFT;
136
+                    } else if (objs[i].off_x < 0) {
137
+                        *spd_off_x += (-GRAVITY_RANGE - objs[i].off_x) >> GRAVITY_SHIFT;
138
+                    }
139
+                    if (objs[i].off_y > 0) {
140
+                        *spd_off_y += (GRAVITY_RANGE - objs[i].off_y) >> GRAVITY_SHIFT;
141
+                    } else if (objs[i].off_y < 0) {
142
+                        *spd_off_y += (-GRAVITY_RANGE - objs[i].off_y) >> GRAVITY_SHIFT;
143
+                    }
144
+                }
145
+
146
+                if ((abs(objs[i].off_x) <= 32) && (abs(objs[i].off_y) <= 32)) {
147
+                    damage += 64;
148
+                }
132 149
             } break;
133 150
 
134 151
             default:
135 152
                 break;
136 153
         }
137 154
     }
155
+
156
+    return damage;
138 157
 }
139 158
 
140 159
 void obj_draw(int16_t spd_x, int16_t spd_y, uint8_t *hiwater) {

+ 1
- 1
src/obj.h View File

@@ -31,7 +31,7 @@ enum OBJ_STATE {
31 31
 
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
-void obj_act(int16_t pos_x, int16_t pos_y, int16_t *spd_off_x, int16_t *spd_off_y);
34
+uint8_t obj_act(int16_t *spd_off_x, int16_t *spd_off_y);
35 35
 void obj_draw(int16_t spd_x, int16_t spd_y, uint8_t *hiwater);
36 36
 
37 37
 #endif // __OBJ_H__

Loading…
Cancel
Save