Browse Source

Clean up player movement

master
Forest Belton 3 years ago
parent
commit
a3cdb70d48
3 changed files with 97 additions and 69 deletions
  1. +47
    -0
      inc/oam.inc
  2. +1
    -1
      src/oam.s
  3. +49
    -68
      src/player.s

+ 47
- 0
inc/oam.inc View File

@ -0,0 +1,47 @@
INCLUDE "hardware.inc"
; The address of an internal OAM entry
; @param \1 16-bit register
; @param \2 OAM index
; @param \3 offset
MACRO ld_OAM
ld \1, _OAM + \2 * sizeof_OAM_ATTRS + \3
ENDM
; The address of an internal OAM entry's Y coordinate
MACRO ld_OAM_y
ld_OAM \1, \2, 0
ENDM
; The address of an internal OAM entry's X coordinate
MACRO ld_OAM_x
ld_OAM \1, \2, 1
ENDM
; The address of an internal OAM entry's tile index
MACRO ld_OAM_tile
ld_OAM \1, \2, 2
ENDM
; The address of an internal OAM entry's attributes
MACRO ld_OAM_attr
ld_OAM \1, \2, 3
ENDM
; Set an entry in the internal OAM (copied on next DMA)
; @param \1 OAM index
; @param \2 X coordinate
; @param \3 Y coordinate
; @param \4 Tile index
; @param \5 Attributes
MACRO OAM_set
ld_OAM_y hl, \1
ld a, \3
ld [hli], a
ld a, \2
ld [hli], a
ld a, \4
ld [hli], a
ld a, \5
ld [hl], a
ENDM

+ 1
- 1
src/oam.s View File

@ -2,7 +2,7 @@ INCLUDE "hardware.inc"
SECTION "OAM Mirror", WRAM0, ALIGN[8] SECTION "OAM Mirror", WRAM0, ALIGN[8]
_OAM:: ds 40 * 4
_OAM:: ds OAM_COUNT * sizeof_OAM_ATTRS
_OAM_end: _OAM_end:
SECTION "OAM DMA routine", ROM0 SECTION "OAM DMA routine", ROM0

+ 49
- 68
src/player.s View File

@ -1,5 +1,6 @@
INCLUDE "hardware.inc" INCLUDE "hardware.inc"
INCLUDE "input.inc" INCLUDE "input.inc"
INCLUDE "oam.inc"
Section "Player Data", WRAM0 Section "Player Data", WRAM0
@ -11,7 +12,8 @@ Section "Player Code", ROM0
spriteData: spriteData:
INCBIN "png/sprite/player.2bpp" INCBIN "png/sprite/player.2bpp"
DEF SPRITE_IDX EQU 32
DEF SPRITE_OAM_IDX EQU 0
DEF SPRITE_IDX EQU 32 ; tile index, rename later
DEF SPRITE_WIDTH EQU 2 DEF SPRITE_WIDTH EQU 2
DEF SPRITE_HEIGHT EQU 2 DEF SPRITE_HEIGHT EQU 2
@ -25,46 +27,10 @@ Player_Init::
ld d, 16 * (SPRITE_WIDTH * SPRITE_HEIGHT) ld d, 16 * (SPRITE_WIDTH * SPRITE_HEIGHT)
call memcpy call memcpy
; top left
ld hl, _OAM
ld a, 144-8-8
ld [hli], a
ld a, 8
ld [hli], a
ld a, SPRITE_IDX
ld [hli], a
xor a
ld [hli], a
; top right
ld a, 144-8-8
ld [hli], a
ld a, 8 + 8
ld [hli], a
ld a, SPRITE_IDX + 1
ld [hli], a
xor a
ld [hli], a
; bottom left
ld a, 144-8
ld [hli], a
ld a, 8
ld [hli], a
ld a, SPRITE_IDX + 2
ld [hli], a
xor a
ld [hli], a
; bottom right
ld a, 144-8
ld [hli], a
ld a, 8 + 8
ld [hli], a
ld a, SPRITE_IDX + 3
ld [hli], a
xor a
ld [hli], a
OAM_set SPRITE_OAM_IDX + 0, 8, 144-8-8, SPRITE_IDX, 0
OAM_set SPRITE_OAM_IDX + 1, 16, 144-8-8, SPRITE_IDX + 1, 0
OAM_set SPRITE_OAM_IDX + 2, 8, 144-8, SPRITE_IDX + 2, 0
OAM_set SPRITE_OAM_IDX + 3, 16, 144-8, SPRITE_IDX + 3, 0
ld bc, _OAMRAM ld bc, _OAMRAM
ld hl, _OAM ld hl, _OAM
@ -79,7 +45,7 @@ Player_Update::
ld a, [hl] ld a, [hl]
and %1 and %1
cp %1 cp %1
jr nz, .done
ret nz
ld hl, keys ld hl, keys
ld b, [hl] ld b, [hl]
@ -90,17 +56,40 @@ Player_Update::
jr z, .left jr z, .left
; check for right boundary ; check for right boundary
ld hl, _OAM + 1
ld_OAM_x hl, SPRITE_OAM_IDX + 1
ld a, [hl] ld a, [hl]
cp 160 - 8
jr nc, .done
cp SCRN_X
jr nc, .left
; TODO: check collision
call move_right
.left:
; check for left button
ld a, b
and BTN_LEFT
ret z
; check for left boundary
ld_OAM_x hl, SPRITE_OAM_IDX
ld a, [hl]
cp 9
ret c
; TODO: check collision
call move_left
ret
move_right:
ld_OAM_x hl, SPRITE_OAM_IDX
; update top left sprite ; update top left sprite
inc [hl] inc [hl]
inc hl inc hl
ld [hl], SPRITE_IDX ld [hl], SPRITE_IDX
inc hl inc hl
res 5, [hl]
res OAMB_XFLIP, [hl]
; update top right sprite ; update top right sprite
inc hl inc hl
@ -109,7 +98,7 @@ Player_Update::
inc hl inc hl
ld [hl], SPRITE_IDX + 1 ld [hl], SPRITE_IDX + 1
inc hl inc hl
res 5, [hl]
res OAMB_XFLIP, [hl]
; update bottom left sprite ; update bottom left sprite
inc hl inc hl
@ -118,7 +107,7 @@ Player_Update::
inc hl inc hl
ld [hl], SPRITE_IDX + 2 ld [hl], SPRITE_IDX + 2
inc hl inc hl
res 5, [hl]
res OAMB_XFLIP, [hl]
; update bottom right sprite ; update bottom right sprite
inc hl inc hl
@ -127,53 +116,45 @@ Player_Update::
inc hl inc hl
ld [hl], SPRITE_IDX + 3 ld [hl], SPRITE_IDX + 3
inc hl inc hl
res 5, [hl]
res OAMB_XFLIP, [hl]
.left:
; check for left button
ld a, b
and BTN_LEFT
jr z, .done
ret
; check for left boundary
ld hl, _OAM + 1
ld a, [hl]
cp 9
jr c, .done
move_left:
ld_OAM_x hl, SPRITE_OAM_IDX
; update top left sprite
; top left
dec [hl] dec [hl]
inc hl inc hl
ld [hl], SPRITE_IDX + 1 ld [hl], SPRITE_IDX + 1
inc hl inc hl
set 5, [hl]
set OAMB_XFLIP, [hl]
; update top right sprite
; top right
inc hl inc hl
inc hl inc hl
dec [hl] dec [hl]
inc hl inc hl
ld [hl], SPRITE_IDX ld [hl], SPRITE_IDX
inc hl inc hl
set 5, [hl]
set OAMB_XFLIP, [hl]
; update bottom left sprite
; bottom left
inc hl inc hl
inc hl inc hl
dec [hl] dec [hl]
inc hl inc hl
ld [hl], SPRITE_IDX + 3 ld [hl], SPRITE_IDX + 3
inc hl inc hl
set 5, [hl]
set OAMB_XFLIP, [hl]
; update bottom right sprite
; bottom right
inc hl inc hl
inc hl inc hl
dec [hl] dec [hl]
inc hl inc hl
ld [hl], SPRITE_IDX + 2 ld [hl], SPRITE_IDX + 2
inc hl inc hl
set 5, [hl]
set OAMB_XFLIP, [hl]
.done:
ret ret

Loading…
Cancel
Save