From a20b308cfe92a8bb47c6896e7fdb24b15c2efd59 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 12 May 2023 23:36:49 +0100 Subject: Add player bullets Also a changes in the entity system: - don't preserve the BG (always erase black) - skip unused entities - fix in the reserved size (and entity is 8 bytes) - allocate new entities --- game/assets/pbullet.png | Bin 0 -> 656 bytes game/bullet.asm | 99 ++++++++++++++++++++++++ game/entities.asm | 197 +++++++++++++++++++++++------------------------- game/main.asm | 9 ++- game/player.asm | 28 +++++++ 5 files changed, 230 insertions(+), 103 deletions(-) create mode 100644 game/assets/pbullet.png create mode 100644 game/bullet.asm (limited to 'game') diff --git a/game/assets/pbullet.png b/game/assets/pbullet.png new file mode 100644 index 0000000..e1ab048 Binary files /dev/null and b/game/assets/pbullet.png differ diff --git a/game/bullet.asm b/game/bullet.asm new file mode 100644 index 0000000..c2b55cd --- /dev/null +++ b/game/bullet.asm @@ -0,0 +1,99 @@ +; +; Player bullets +; +bullet_player_new: + call entities_new + bnc + ret + + ; a : x points to the entity + + ld y, ET_BULLET + ld [a : x], y + + inc x + bo + inc a + + ; caller entity is in the stack + ld y, [sp + 4] + ld b, [sp + 5] + + inc y + bo + inc b + push y + ld y, [b : y] + ; same x + ld [a : x], y + pop y + + inc x + bo + inc a + + inc y + bo + inc b + ld y, [b : y] + ; same y + ld [a : x], y + + inc x + bo + inc a + + ; frame 0 + ld y, 0 + ld [a : x], y + + inc x + bo + inc a + + ; player bullet sprite + ld y, pbullet_sprite + ld [a : x], y + + inc x + bo + inc b + + ; update function + ld y, pbullet_update + ld [a : x], y + + ret + +pbullet_update: + ld x, [sp + 2] + ld a, [sp + 3] + + add x, 2 + bo + inc a + + ld y, [a : x] + sub y, 2 + bo + jmp pbullet_done + ld [a : x], y + ret + +pbullet_done: + ld x, [sp + 2] + ld a, [sp + 3] + + ld y, ET_NONE + ld [a : x], y + ret diff --git a/game/entities.asm b/game/entities.asm index b3e1eea..eb9c44f 100644 --- a/game/entities.asm +++ b/game/entities.asm @@ -5,15 +5,35 @@ .equ ET_NONE 0 .equ ET_PLAYER 1 .equ ET_ENEMY 2 +.equ ET_BULLET 3 .equ ET_END 0xff .equ ET_BG 0xa000 -entities_erase: - ; bg is stored in ET_BG addr - ld b, >ET_BG - push b +entities_init: + ld x, entities + + ; skip player + add x, 8 + bo + inc a + +entities_init_next: + ld y, [a : x] + cmp y, ET_END + bz + ret + + ld y, ET_NONE + ld [a : x], y + + add x, 8 + bo + inc a + jmp entities_init_next +entities_erase: ld x, entities @@ -21,8 +41,17 @@ entities_erase_next: ld y, [a : x] cmp y, ET_END bz - jmp entities_erase_done + ret + cmp y, 0 + bnz + jmp entities_erase_in_use + + add x, 8 + bo + inc a + jmp entities_erase_next +entities_erase_in_use: ; settings mode ld y, 128 ld b, 0xb0 @@ -32,9 +61,9 @@ entities_erase_next: inc b ; addr: bg data - ld y, 0 + ld y, ET_BG port b, y ; x @@ -66,17 +95,8 @@ entities_erase_next: add x, 6 bo inc a - - pop b - inc b - push b - jmp entities_erase_next -entities_erase_done: - pop a - ret - entities_update: ld x, entities @@ -86,7 +106,16 @@ entities_update_next: cmp y, ET_END bz ret + cmp y, 0 + bnz + jmp entities_update_in_use + + add x, 8 + bo + inc a + jmp entities_update_next +entities_update_in_use: push a push x @@ -124,10 +153,6 @@ entities_update_next: jmp entities_update_next entities_draw: - ; bg is stored in ET_BG addr - ld b, >ET_BG - push b - ld x, entities @@ -135,30 +160,19 @@ entities_draw_next: ld y, [a : x] cmp y, ET_END bz - jmp entities_draw_done - - ; two local variables to keep x, y coord - - inc x - bo - inc a - ld y, [a : x] - ; x coord - push y + ret + cmp y, 0 + bnz + jmp entities_draw_in_use - inc x + add x, 8 bo inc a - ld y, [a : x] - ; y coord - push y + jmp entities_draw_next - inc x - bo - inc a - ; a : x points to the frame +entities_draw_in_use: - ; save bg data + ; draw sprite ; settings mode ld y, 128 @@ -168,49 +182,19 @@ entities_draw_next: ; setup inc b - ; addr: bg data saving space - ld y, 0 - port b, y - ld y, [sp + 2] - port b, y - - ; x - ld y, [sp + 1] - port b, y - - ; y - ld y, [sp + 0] - port b, y - - ; 16x16 - ld y, 16 - port b, y - port b, y - - ; blit - dec b - ; read, no transparent - ld y, 4 - port b, y - - ; draw sprite - - ; settings mode - ld y, 128 - ld b, 0xb0 - port b, y + push a + push x - ; setup - inc b + add x, 3 + bo + inc a - ; prepare some local variables + ; frame number + ld b, [a : x] - ; frame - ld y, [a : x] inc x bo inc a - push y ; sprite addr ld y, [a : x] @@ -219,19 +203,14 @@ entities_draw_next: bo inc a ld y, [a : x] - push y - ; frame number - ld b, [sp + 2] entity_draw_frame: dec b bo jmp entity_draw_frame_done ; calculate frame - ld y, [sp + 0] inc y - ld [sp + 0], y jmp entity_draw_frame entity_draw_frame_done: @@ -239,23 +218,28 @@ entity_draw_frame_done: ; settings port ld b, 0xb1 + ld x, y + pop y + ; addr: sprite data - ld y, [sp + 1] - port b, y - ld y, [sp + 0] port b, y + port b, x - ; free local variables - pop y - pop y - pop y + pop x + pop a + + inc x + bo + inc a ; x - ld y, [sp + 1] + ld y, [a : x] port b, y - + inc x + bo + inc a ; y - ld y, [sp + 0] + ld y, [a : x] port b, y ; 16x16 @@ -269,20 +253,31 @@ entity_draw_frame_done: ld y, 3 port b, y - pop y - pop y - ; next entity - add x, 3 + add x, 6 bo inc a + jmp entities_draw_next - pop b - inc b - push b +entities_new: + ld x, entities - jmp entities_draw_next +entities_new_loop: + ld y, [a : x] + cmp y, ET_END + ccf + bz + ret + cmp y, 0 + bz + jmp entities_new_found -entities_draw_done: - pop a + add x, 8 + bo + inc a + jmp entities_new_loop + +entities_new_found: + scf ret diff --git a/game/main.asm b/game/main.asm index 8373f37..365e381 100644 --- a/game/main.asm +++ b/game/main.asm @@ -61,7 +61,7 @@ game_loop: ; Setup the game after the menu ; init_game: - halt + call entities_init ; set player initial coordinates ld x, cool_down + ld y, [a : x] + cmp y, 0 + bz + jmp player_fire + dec y + ld [a : x], y + ret + +player_fire: + add y, 10 + ld [a : x], y + + call bullet_player_new ret -- cgit v1.2.3