aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-03-20 21:50:49 +0100
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-03-20 21:50:49 +0100
commit1d1922c3027a0f9b6a31f532c455f93154b5dd38 (patch)
tree9f8e0df37aeb71a5b774163939bb6e551fef98fe
parente0bdeaed3b2face03bb388df565344e688759bb3 (diff)
downloadjetpac.nes-1d1922c3027a0f9b6a31f532c455f93154b5dd38.tar.gz
jetpac.nes-1d1922c3027a0f9b6a31f532c455f93154b5dd38.zip
player: Inline the position to screen translation
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--src/player.s84
1 files changed, 35 insertions, 49 deletions
diff --git a/src/player.s b/src/player.s
index dc87cb0..210a7c1 100644
--- a/src/player.s
+++ b/src/player.s
@@ -1,5 +1,28 @@
.segment "CODE"
+;; Translate the given fixed-point 16-bit position into screen coordinates and
+;; leave it into the `a` register.
+;;
+;; NOTE: see the documentation on player's movement for more information.
+.macro FIXED_POINT_POSITION_TO_SCREEN POS_ADDR
+ ;; We save the high byte into a temporary value, and we load the low byte
+ ;; into the accumulator.
+ lda POS_ADDR + 1
+ sta Globals::zp_tmp0
+ lda POS_ADDR
+
+ ;; And now it's a matter of rotating the high byte into the low one to
+ ;; match a full byte.
+ lsr Globals::zp_tmp0
+ ror
+ lsr Globals::zp_tmp0
+ ror
+ lsr Globals::zp_tmp0
+ ror
+ lsr Globals::zp_tmp0
+ ror
+.endmacro
+
;; Functions and variables that keep up with the player's sprite. That is,
;; movement, heading, animation, collision with the environment, etc.
.scope Player
@@ -158,11 +181,19 @@
sta zp_walk_counter
@to_screen:
- ;; At this point all positions are clear, transform them into screen
- ;; coordinates, eject out from boundaries and platforms, and update the
- ;; sprite with the new state.
- jsr position_to_screen
+ ;; Translate fixed-point positions to screen coordinates.
+ FIXED_POINT_POSITION_TO_SCREEN zp_position_y
+ sta zp_screen_y
+ FIXED_POINT_POSITION_TO_SCREEN zp_position_x
+ sta zp_screen_x
+
+ ;; We have the newly proposed screen coordinates. Now let's check if
+ ;; that collides with some background element. If that's the case,
+ ;; handle ejection logic now.
jsr background_check
+
+ ;; And with that, update all the sprites with the information we have
+ ;; collected (i.e. heading, throttle, coordinates).
JAL update_sprites
.endproc
@@ -342,51 +373,6 @@
rts
.endproc
- ;; Convert the positions with subpixel precision into mere screen
- ;; coordinates. That is, update the values on `zp_screen_{x,y}` given the
- ;; current values of `zp_position_{x,y}`.
- .proc position_to_screen
- ;; We save the high byte into a temporary value, and we load the low
- ;; byte into the accumulator.
- lda zp_position_y + 1
- sta Globals::zp_tmp0
- lda zp_position_y
-
- ;; And now it's a matter of rotating the high byte into the low one to
- ;; match a full byte.
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
-
- ;; Ecce Y coordinates.
- sta zp_screen_y
-
- ;; And the same for the X coordinates.
- lda zp_position_x + 1
- sta Globals::zp_tmp0
- lda zp_position_x
-
- ;; And rolling...
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
- lsr Globals::zp_tmp0
- ror
-
- ;; Ecce X coordinates.
- sta zp_screen_x
-
- rts
- .endproc
-
;; Check on whether the player is out of bounds in any way and provide an
;; ejection logic for each situation.
.proc background_check