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
|
#ifndef systems_h
#define systems_h
#include <stdint.h>
#include "components.h"
#include "config.h"
#include "fx.h"
#include "rect.h"
#include "standard.h"
#include "world.h"
#ifdef DEBUG
#include "error.h"
#include "platform.h"
#endif
typedef enum {
collision_side_left = 0,
collision_side_right,
collision_side_top,
collision_side_bottom
} Collision_Side;
static Collision_Side invert_side(Collision_Side side) {
switch (side) {
case collision_side_left: return collision_side_right;
case collision_side_right: return collision_side_left;
case collision_side_bottom: return collision_side_top;
case collision_side_top: return collision_side_bottom;
}
#if DEBUG
platform_err("Invalid collision side.\n");
platform_abort(error_gameplay_error);
#endif
return -1;
}
static void handle_bullet_vs_player(
World* world,
Entity bullet,
Entity player
) {
const CPosition* pos;
pos = &world->positions[bullet];
player_take_damage(world, &world->player, 1);
destroy_entity(world, bullet);
new_enemy_bullet_explosion(world, pos->x, pos->y);
}
static void handle_bullet_vs_enemy(
World* world,
Entity bullet,
Entity enemy
) {
CEnemy* e;
const CPosition* pos;
pos = &world->positions[bullet];
e = &world->enemies[enemy];
e->hp--;
destroy_entity(world, bullet);
new_player_bullet_explosion(world, pos->x, pos->y);
}
static void handle_bullet_vs_solid(
World* world,
Entity bullet,
Entity solid
) {
const CPosition* pos;
unsigned bbits;
bbits = world->bitmask[bullet];
pos = &world->positions[bullet];
if (bbits & ctype_enemy_bullet) {
new_enemy_bullet_explosion(world, pos->x, pos->y);
} else if (bbits & ctype_player_bullet) {
new_player_bullet_explosion(world, pos->x, pos->y);
}
destroy_entity(world, bullet);
}
static void handle_moveable_vs_solid(
World* world,
Entity moveable,
Entity solid,
Collision_Side side
) {
const CCollider* scol, * mcol;
const CPosition* spos;
CPosition* mpos;
scol = &world->colliders[solid];
mcol = &world->colliders[moveable];
spos = &world->positions[solid];
mpos = &world->positions[moveable];
switch (side) {
case collision_side_top:
mpos->y = spos->y + scol->y + scol->h - mcol->y;
return;
case collision_side_bottom:
mpos->y = (spos->y + scol->y) - (mcol->y + mcol->h);
return;
case collision_side_left:
mpos->x = spos->x + scol->x + scol->w - mcol->x;
return;
case collision_side_right:
mpos->x = (spos->x + scol->x) - (mcol->x + mcol->w);
return;
}
#if DEBUG
platform_err("Invalid collision side.\n");
platform_abort(error_gameplay_error);
#endif
}
static void handle(
World* world,
Entity a,
Entity b,
Collision_Side side
) {
unsigned ab, bb;
ab = world->bitmask[a];
bb = world->bitmask[b];
if ((ab & ctype_enemy_bullet) && (bb & ctype_player)) {
handle_bullet_vs_player(world, a, b);
return;
}
if ((ab & ctype_player) && (bb & ctype_enemy_bullet)) {
handle_bullet_vs_player(world, b, a);
return;
}
if ((ab & ctype_player_bullet) && (bb & ctype_enemy)) {
handle_bullet_vs_enemy(world, a, b);
return;
}
if ((ab & ctype_enemy) && (bb & ctype_player_bullet)) {
handle_bullet_vs_enemy(world, b, a);
return;
}
if ((ab & ctype_bullet) && (bb & ctype_solid)) {
handle_bullet_vs_solid(world, a, b);
return;
}
if ((ab & ctype_solid) && (bb & ctype_bullet)) {
handle_bullet_vs_solid(world, b, a);
return;
}
if ((ab & ctype_moveable) && (bb & ctype_solid)) {
handle_moveable_vs_solid(world, a, b, side);
return;
}
if ((ab & ctype_solid) && (bb & ctype_moveable)) {
side = invert_side(side);
handle_moveable_vs_solid(world, b, a, side);
return;
}
}
void collision_system(World* world) {
int i, j, k, s, s2, p0x, p0y, r, l, t, b;
int ax, ay, aw, ah, bx, by, bw, bh;
int overlap[4];
unsigned bits;
const CPosition* pos0, * pos1;
const CCollider* col0, * col1;
Collision_Side side;
for (i = 0; i < world->entity_count; i++) {
bits = world->bitmask[i];
if (
!(bits & ctype_collider) ||
!(bits & ctype_position)
) { continue; }
pos0 = &world->positions[i];
col0 = &world->colliders[i];
p0x = pos0->x + col0->x;
p0y = pos0->y + col0->y;
for (j = i + 1; j < world->entity_count; j++) {
bits = world->bitmask[j];
if (
!(bits & ctype_collider) ||
!(bits & ctype_position)
) { continue; }
pos1 = &world->positions[j];
col1 = &world->colliders[j];
ax = p0x;
ay = p0y;
aw = col0->w;
ah = col0->h;
bx = pos1->x + col1->x;
by = pos1->y + col1->y;
bw = col1->w;
bh = col1->h;
/* Can you tell I made this while I had a migraine? */
if (rects_overlap2(
ax,
ay,
aw,
ah,
bx,
by,
bw,
bh
)) {
r = (ax + aw) - bx;
l = (bx + bw) - ax;
t = (by + bh) - ay;
b = (ay + ah) - by;
overlap[0] = r;
overlap[1] = l;
overlap[2] = t;
overlap[3] = b;
s = INT32_MAX;
for (k = 0; k < 4; k++) {
s2 = overlap[k];
if (s2 < s) { s = s2; }
}
if (s == absolute(r)) {
side = collision_side_right;
} else if (s == absolute(l)) {
side = collision_side_left;
} else if (s == absolute(b)) {
side = collision_side_bottom;
} else if (s == absolute(t)) {
side = collision_side_top;
}
handle(world, i, j, side);
}
}
}
}
#endif
|