The current system basically gets CollisionEvents for all applicable events and puts them in a dynamic array. We then iterate over this array and perform doCollisionEvent or doMissEvent on the event particulars (the loop code that controls this can be seen here). Those funcs are below:
pub fn doCollisionEvent(numCollisions: *usize, enemyList: *std.ArrayList(raylib.Rectangle), projectiles: *std.ArrayList(Projectile), collision: CollisionEvent) void {
numCollisions.* += 1;
_ = projectiles.orderedRemove(collision.proj_idx);
_ = enemyList.orderedRemove(collision.rec_idx);
}
// TODO: only spawn enemies if the shot missed and hit the edge of the screen (or a certain distance away from the player)
pub fn doMissEvent(activeEvent: CollisionEvent, enemyList: *std.ArrayList(raylib.Rectangle), projectileList: *std.ArrayList(Projectile)) !void {
try spawnEnemy(activeEvent.point, enemyList);
_ = projectileList.orderedRemove(activeEvent.proj_idx);
}
However, any time the player hits or misses it causes the game to crash. These errors are below:
on miss:
Collision event: gameAPI.CollisionEvent{ .bool = false, .point = raylib.Vector2{ .x = 0e0, .y = 0e0 }, .rec_idx = 4, .proj_idx = 0 }
thread 126201 panic: index out of bounds: index 0, len 0
/usr/local/Cellar/zig/0.13.0/lib/zig/std/array_list.zig:282:40: 0x10a3ef650 in orderedRemove (DiaB-concept)
const old_item = self.items[i];
^
/Users/jordans/projects/mach-mess/src/gameAPI.zig:240:37: 0x10a3ef56f in doMissEvent (DiaB-concept)
_ = projectileList.orderedRemove(activeEvent.proj_idx);
^
/Users/jordans/projects/mach-mess/src/main.zig:160:43: 0x10a3ecc61 in main (DiaB-concept)
} else try doMissEvent(collision, &enemyList, &projectileList);
^
/usr/local/Cellar/zig/0.13.0/lib/zig/std/start.zig:524:37: 0x10a3ebf8b in main (DiaB-concept)
const result = root.main() catch |err| {
^
???:?:?: 0x7ff8060353a5 in ??? (???)
Unwind information for `???:0x7ff8060353a5` was not available, trace may be incomplete
???:?:?: 0x0 in ??? (???)
run
└─ run DiaB-concept failure
on hit:
Collision event: gameAPI.CollisionEvent{ .bool = true, .point = raylib.Vector2{ .x = 1.0689075e2, .y = 1.12681305e2 }, .rec_idx = 1, .proj_idx = 0 }
thread 129194 panic: index out of bounds: index 1, len 1
/usr/local/Cellar/zig/0.13.0/lib/zig/std/array_list.zig:282:40: 0x109580a50 in orderedRemove (DiaB-concept)
const old_item = self.items[i];
^
/Users/jordans/projects/mach-mess/src/gameAPI.zig:232:32: 0x109554ce6 in doCollisionEvent (DiaB-concept)
_ = enemyList.orderedRemove(collision.rec_idx);
^
/Users/jordans/projects/mach-mess/src/main.zig:159:41: 0x109550c41 in main (DiaB-concept)
doCollisionEvent(&numCollisions, &enemyList, &projectileList, collision);
^
/usr/local/Cellar/zig/0.13.0/lib/zig/std/start.zig:524:37: 0x10954ff8b in main (DiaB-concept)
const result = root.main() catch |err| {
^
???:?:?: 0x7ff8060353a5 in ??? (???)
Unwind information for `???:0x7ff8060353a5` was not available, trace may be incomplete
???:?:?: 0x0 in ??? (???)
run
└─ run DiaB-concept failure
The correct behavior would be:
On hit the enemy would be removed from the enemyList and the projectile that hit it would be removed from the projectileList
On miss the projectile would reach the edge of the screen and spawn 1..5 enemies at the spot where it passed into the out-of-bounds area. This would also include removing the projectile from the projectileList and adding the enemies to the enemyList
The current system basically gets
CollisionEvents
for all applicable events and puts them in a dynamic array. We then iterate over this array and performdoCollisionEvent
ordoMissEvent
on the event particulars (the loop code that controls this can be seen here). Those funcs are below:However, any time the player hits or misses it causes the game to crash. These errors are below:
on miss:
on hit:
The correct behavior would be:
enemyList
and the projectile that hit it would be removed from theprojectileList
projectileList
and adding the enemies to theenemyList