~nytpu/ankano unlisted

092d3d5fc0fcfd6be7761ad4571f9e4eeb7027df — nytpu 4 months ago 0c32196
add controllable paddle
4 files changed, 152 insertions(+), 14 deletions(-)

M include/defines.asm
M src/keys.asm
M src/loop.asm
A src/player.asm
M include/defines.asm => include/defines.asm +12 -0
@@ 19,6 19,18 @@ INCLUDE "hardware.inc/hardware.inc"
;INCLUDE "gb-vwf/vwf.asm"


/* broken
; struct definitions
	struct PlayerStruct
	bytes 1, YPos
	bytes 1, XPos
	bytes 1, YBox
	bytes 1, XBox
	bytes 1, Attr
	bytes 3, Tiles
	end_struct
*/

; a couple more hardware defines
NB_SPRITES equ 40


M src/keys.asm => src/keys.asm +38 -8
@@ 6,8 6,7 @@ INCLUDE "defines.asm"

SECTION "Keys Handler", ROM0
; handle keypad input
; @destroy hl, a
; TODO: convert key checks to macro
; destroy a, h, l
KeyHandler::
	ld hl, hHeldKeys
	xor a ; ld a, 0


@@ 15,7 14,7 @@ KeyHandler::
	ret z ; no input this frame

	ld a, $0F
	cp [hl]
	and [hl]
	; only d-pad is pressed, lets us skip half the bit checks most of the time
	jr z, .dPadOnly



@@ 34,9 33,11 @@ KeyHandler::
.dPadOnly
	bit PADB_RIGHT, [hl]
	call nz, wKeyHandlers.right
	ret nz ; only works if key handlers don't mess wtih flags

	bit PADB_LEFT, [hl]
	call nz, wKeyHandlers.left
	ret nz

	; not using up and down for this game
	;bit PADB_UP, [hl]


@@ 47,17 48,45 @@ KeyHandler::
	ret


SECTION "Key Handlers ROM", ROMX
SECTION FRAGMENT "Key Handlers ROM", ROMX
gameKeyHandlers::
.a
	ld a, %00011011
	ldh [hBGP], a
	ret
.aEnd

.b
	ld a, %11100100
	ldh [hBGP], a
	ret
.bEnd

.select
	ret
.selectEnd

.start
	ret
.startEnd

.right
	ld hl, Player_XPos
	inc [hl]
	ret
.rightEnd

.left
	ld hl, Player_XPos
	dec [hl]
	ret
.leftEnd
.end::


SECTION FRAGMENT "Key Handlers ROM", ROMX
introKeyHandlers::
.a
	ret
.aEnd

.b
	ret
.bEnd



@@ 78,6 107,7 @@ gameKeyHandlers::
.leftEnd
.end::


SECTION "Key Handlers", WRAM0
wKeyHandlers::
.a ds gameKeyHandlers.aEnd - gameKeyHandlers.a

M src/loop.asm => src/loop.asm +12 -6
@@ 25,7 25,7 @@ EnterMainLoop::
	ld de, GameTiles.bg
	ld hl, _VRAM9000
INCLUDE "res/game_map.2bpp.pb16.size"
	ld bc, NB_PB16_BLOCKS
	ld b, NB_PB16_BLOCKS
	PURGE NB_PB16_BLOCKS
	call pb16_unpack_block



@@ 44,6 44,7 @@ INCLUDE "res/spritemap.2bpp.pb16.size"
	; load palette
	ld a, %11100100
	ld [hBGP], a
	ld [hOBP0], a

	; load key handlers for game
	ld a, BANK(gameKeyHandlers)


@@ 54,18 55,23 @@ INCLUDE "res/spritemap.2bpp.pb16.size"
	ld c, gameKeyHandlers.end - gameKeyHandlers
	rst MemcpySmall

	call InitPlayer

	; enable lcd, enable background, disable window, use $9800 for background
	; map, use $8800 for bg tileset, enable objects
	ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9800 | LCDCF_BG8800 | LCDCF_OBJON
	ld a, LCDCF_ON | LCDCF_BGON | LCDCF_OBJON | LCDCF_BG9800 | LCDCF_BG8800
	ldh [hLCDC], a
	ldh [rLCDC], a

	; enable interrupts (primarily vblank)
	ei
	; soft reset during gameplay is fine
	ld a, 1
	ldh [hCanSoftReset], a
	; enable interrupts (primarily vblank)
	ei
MainLoop:
	call KeyHandler
	call PlayerHandler

	; if there's any more time before vblank
	rst WaitVBlank
	jr MainLoop


@@ 73,10 79,10 @@ MainLoop:

SECTION "Gameplay Tiles", ROMX
GameTiles:
.bg
INCBIN "res/game_map.2bpp.pb16"
.bgTileMap
INCBIN "res/game_map.2bpp.tilemap"
.bgTileMapEnd
.bg
INCBIN "res/game_map.2bpp.pb16"
.sprites
INCBIN "res/spritemap.2bpp.pb16"

A src/player.asm => src/player.asm +90 -0
@@ 0,0 1,90 @@
; Copyright (C) 2020 nytpu
; This file is licensed under the terms of the MIT License.
; For more license details, see LICENSE or <https://mit-license.org/>.

INCLUDE "defines.asm"

SECTION "Player Handler", ROM0
; initializes the player variables and initiates oam dma
; destroy a, b, c, h, l
InitPlayer::
	;dstruct PlayerStruct, Player

	ld a, SCRN_Y - 12
	ld [Player_YPos], a
	add 8
	ld [Player_YBox], a

	ld a, ((SCRN_X - 32) / 2) - 4
	ld [Player_XPos], a
	add 8 * 3
	ld [Player_XBox], a

	xor a
	ld [Player_Attr], a

	ld hl, Player_Tiles
	ld b, 4
REPT 3
	ld [hl], b
	inc hl
	inc b
ENDR

; deals with player updates and bounds checking, generates and copies oam
; destroy a, b, c, h, l
PlayerHandler::
	ld hl, Player_XPos
	ld a, SCRN_X - 56
	cp [hl]
	jr nc, .withinBounds_R
	ld [hl], a
.withinBounds_R
	ld a, 16
	cp [hl]
	jr c, .withinBounds
	ld [hl], a
.withinBounds

; initiates oam dma
; destroy a, b, c, h, l
; TODO: break out into separate oam generator
CreatePlayerOAM:
	ld b, 0
	ld c, 0
	ld hl, wShadowOAM
REPT 3
	ld a, [Player_YPos]
	ld [hli], a

	ld a, [Player_XPos]
	add b
	ld [hli], a

	ld a, [Player_Tiles]
	add c
	ld [hli], a

	ld a, [Player_Attr]
	ld [hli], a

	ld a, b
	add 8
	ld b, a
	inc c
ENDR

	ld a, HIGH(wShadowOAM)
	ldh [hOAMHigh], a
	ret


SECTION "Player Variables", HRAM
sizeof_Player EQU 8
Player::
Player_YPos:: dw
Player_XPos:: dw
Player_YBox:: dw
Player_XBox:: db
Player_Attr:: db
Player_Tiles:: ds 3