From e602dd3707f2b6ad3198a62497ca6ee311997485 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Fri, 6 Mar 2026 23:40:02 +0100 Subject: Convert enemy vs bullet collision into a loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fact that unrolling the loop was easier or faster was a plain lie from my lazyness. Convert it into a proper loop just so we can experiment with adding more enemies in one screen. Signed-off-by: Miquel Sabaté Solà --- src/bullets.s | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/bullets.s') diff --git a/src/bullets.s b/src/bullets.s index 8b93bac..8bbbeaa 100644 --- a/src/bullets.s +++ b/src/bullets.s @@ -287,29 +287,33 @@ inx jmp @move_loop - ;; Enemy collision for this bullet. It's actually easier/faster to just - ;; unroll the loop. + ;; Enemy collision for this bullet. @check_enemy_collision: - lda #Enemies::ENEMY_0_IDX - sta Enemies::zp_pool_index - jsr Enemies::collides - beq @enemy_1 - jsr Enemies::bite_the_dust - jmp @save_bullet_move - @enemy_1: - lda #Enemies::ENEMY_1_IDX + ldx #0 sta Enemies::zp_pool_index + + @enemy_collision_loop: + ;; Does it collide? jsr Enemies::collides - beq @enemy_2 + beq @next_enemy_collision + + ;; Yes! Kill the enemy and break the loop. jsr Enemies::bite_the_dust jmp @save_bullet_move - @enemy_2: - lda #Enemies::ENEMY_2_IDX - sta Enemies::zp_pool_index - jsr Enemies::collides + + @next_enemy_collision: + ;; Advance by the size of each pool item. + lda Enemies::zp_pool_index + clc + adc #Enemies::SIZEOF_POOL_ITEM + + ;; Are we at the end of the enemies' pool? + cmp #Enemies::ENEMIES_POOL_CAPACITY_BYTES beq @save_bullet_move - jsr Enemies::bite_the_dust - __fallthrough__ @save_bullet_move + + ;; Nope! Save the index and fetch the next enemy. + sta Enemies::zp_pool_index + jmp @enemy_collision_loop @save_bullet_move: ;; Restore back the old value from the 'x' register. -- cgit v1.2.3