diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-03-06 23:40:02 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-03-06 23:40:02 +0100 |
| commit | e602dd3707f2b6ad3198a62497ca6ee311997485 (patch) | |
| tree | 5c4e07b8e878b77f85b29a1de90b8e8827ffd41a | |
| parent | 53b6d7eb0461d63f580c5a2612b71a735a5ecba1 (diff) | |
| download | jetpac.nes-e602dd3707f2b6ad3198a62497ca6ee311997485.tar.gz jetpac.nes-e602dd3707f2b6ad3198a62497ca6ee311997485.zip | |
Convert enemy vs bullet collision into a loop
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à <mssola@mssola.com>
| -rw-r--r-- | .nasm/segments.txt | 2 | ||||
| -rw-r--r-- | src/bullets.s | 38 | ||||
| -rw-r--r-- | src/enemies.s | 10 |
3 files changed, 26 insertions, 24 deletions
diff --git a/.nasm/segments.txt b/.nasm/segments.txt index 5bd1c28..f09170c 100644 --- a/.nasm/segments.txt +++ b/.nasm/segments.txt @@ -1,4 +1,4 @@ - HEADER: 16/16 (100%) -- ROM0: 6551/32762 (20.00%) +- ROM0: 6538/32762 (19.96%) - ROMV: 6/6 (100%) - ROM2: 8192/8192 (100%) 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. diff --git a/src/enemies.s b/src/enemies.s index b6005fc..33b4c33 100644 --- a/src/enemies.s +++ b/src/enemies.s @@ -17,13 +17,11 @@ ;; change it there. ENEMIES_POOL_CAPACITY = 3 - ;; The capacity of the enemies pool in bytes. - ENEMIES_POOL_CAPACITY_BYTES = ENEMIES_POOL_CAPACITY * 4 + ;; The amount of bytes each pool item takes. + SIZEOF_POOL_ITEM = 4 - ;; Indeces where each enemy definition starts on the pool. - ENEMY_0_IDX = 0 - ENEMY_1_IDX = 4 - ENEMY_2_IDX = 8 + ;; The capacity of the enemies pool in bytes. + ENEMIES_POOL_CAPACITY_BYTES = ENEMIES_POOL_CAPACITY * SIZEOF_POOL_ITEM ;; Initial X coordinates for enemies depending on if they appear on the ;; left/right edge of the screen. |
