aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-05-12 23:36:49 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-05-12 23:36:49 +0100
commita20b308cfe92a8bb47c6896e7fdb24b15c2efd59 (patch)
tree22a38850ea38dc91f02095101192061aaafbc30d
parent8eec2f91339b8f06e1b80c49c976a9d9ac6e01f7 (diff)
downloadtr8vm-a20b308cfe92a8bb47c6896e7fdb24b15c2efd59.tar.gz
tr8vm-a20b308cfe92a8bb47c6896e7fdb24b15c2efd59.zip
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
-rw-r--r--game/assets/pbullet.pngbin0 -> 656 bytes
-rw-r--r--game/bullet.asm99
-rw-r--r--game/entities.asm197
-rw-r--r--game/main.asm9
-rw-r--r--game/player.asm28
5 files changed, 230 insertions, 103 deletions
diff --git a/game/assets/pbullet.png b/game/assets/pbullet.png
new file mode 100644
index 0000000..e1ab048
--- /dev/null
+++ b/game/assets/pbullet.png
Binary files 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 a
+ ld y, >pbullet_sprite
+ ld [a : x], y
+
+ inc x
+ bo
+ inc b
+
+ ; update function
+ ld y, <pbullet_update
+ ld [a : x], y
+ inc x
+ bo
+ inc a
+ 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
+ ld a, >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
ld a, >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
- ld y, [sp + 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
ld a, >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
ld a, >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
+ ld a, >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, <player_coord
@@ -75,6 +75,8 @@ init_game:
ld y, 104
ld [a : x], y
+ halt
+
call clear_screen
call entities_draw
@@ -252,6 +254,7 @@ int_handler:
.include "starfield.asm"
.include "entities.asm"
.include "player.asm"
+.include "bullet.asm"
;
; entity data
@@ -268,7 +271,7 @@ player_coord:
.dw player_update
; 16 entities
- .ds 112, 0
+ .ds 128, 0
; end of list
.db ET_END
@@ -276,6 +279,8 @@ player_coord:
; sprite data
player_sprite:
.incpng "assets/player.png"
+pbullet_sprite:
+ .incpng "assets/pbullet.png"
; starfield data
stars:
diff --git a/game/player.asm b/game/player.asm
index 0e44411..ef63839 100644
--- a/game/player.asm
+++ b/game/player.asm
@@ -2,6 +2,9 @@
; player update code
;
+cool_down:
+ .db 0
+
player_update:
ld x, [sp + 2]
ld a, [sp + 3]
@@ -52,6 +55,8 @@ player_update:
; base frame
ld y, 0
+ push b
+
; both left and right
and b, 48
cmp b, 48
@@ -69,4 +74,27 @@ player_update:
player_store_frame:
ld [a : x], y
+
+ pop b
+
+ ; fire
+ bit b, 0
+ bnz
+ ret
+
+ ld x, <cool_down
+ ld a, >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