diff --git a/CHANGELOG.md b/CHANGELOG.md index ae93e98..017ea43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [0.0.3] - 2018-09-17 + +### Changed + +- Use a lookup table for y position rather than a random function in asm + ## [0.0.2] - 2018-07-03 ### Added diff --git a/src/droplet.asm b/src/droplet.asm index ed24a86..ada3f32 100644 --- a/src/droplet.asm +++ b/src/droplet.asm @@ -1,6 +1,7 @@ INCLUDE "gbhw.inc" -MAX_DROPLETS EQU 10 +MAX_DROPLETS EQU 20 +INITIAL_DROPLETS EQU 20 INITIAL_SPAWN_SPRITE_Y EQU 8 IDLE_SPRITE_Y EQU 0 @@ -25,29 +26,53 @@ init_droplets:: call mem_Set xor a - ld [total_droplets],a + ld [total_droplets], a ld [spawn_delay], a - ret + ; spawn max + jr .skip +.loop + call spawn_droplet +.skip + ld a, [total_droplets] + cp MAX_DROPLETS + jr nz, .loop -spawn_droplet:: - ; has the spawn delay reached zero? - ld a, [spawn_delay] - cp 0 - jr z, .skip_return - dec a - ld [spawn_delay], a + call init_droplets_y ret -.skip_return - ; reset the spawn delay with a random range + + +init_droplets_y: + ld hl, droplets + + ld a, [total_droplets] + ld c, a + inc c + jp .skip + +.loop + push hl push bc - call fast_random + call get_random_sprite_y pop bc + pop hl + + ld [hl+], a + inc hl + inc hl + inc hl +.skip + dec c + jp nz, .loop + ret - and %00000011 - add 16 - ld [spawn_delay], a +spawn_droplet:: + ld a, [total_droplets] + cp MAX_DROPLETS + jr nz, .skip_return + ret +.skip_return ; advance to the next idle droplet ld hl, droplets @@ -61,12 +86,10 @@ spawn_droplet:: cp IDLE_SPRITE_Y jr nz, .next - ; found an idle droplet ; set y ld [hl], INITIAL_SPAWN_SPRITE_Y - ; set x to random 0-19 multiplied by 8 to align with tiles inc hl push hl @@ -107,7 +130,7 @@ spawn_droplet:: ret -; Move each active droplet down and rotate the tile +; Move each active droplet down and cycle the tile ; Speed is based on memory position for variety move_droplets:: ld hl, droplets @@ -181,13 +204,13 @@ move_droplets:: .inc_y ld a, [droplet_sprite_y] add e - ; if we are now offscreen, set the droplet to idle + ; if we are now offscreen, reset the droplet cp a, SCRN_Y + 16 ; carry flag set if 160 > y jp c, .inc_y_set_idle_skip ld a, [total_droplets] dec a ld [total_droplets], a - ld a, IDLE_SPRITE_Y + ld a, INITIAL_SPAWN_SPRITE_Y .inc_y_set_idle_skip ld [droplet_sprite_y], a .dont_move: @@ -197,9 +220,6 @@ move_droplets:: cp %00000011 jp nz, .dont_cycle_character - ld a, [droplet_sprite_y] - cp IDLE_SPRITE_Y ; is the droplet idle? - jp nc, .skip_tile_reset push bc call fast_random pop bc @@ -222,7 +242,6 @@ move_droplets:: inc hl .skip - dec c jp nz, .loop ret diff --git a/src/main.asm b/src/main.asm index 271f14e..be855ed 100644 --- a/src/main.asm +++ b/src/main.asm @@ -34,7 +34,6 @@ start:: xor a ld [tile_command_list_length], a call move_droplets - call spawn_droplet call set_droplets_to_bg call update_tile_fade call update_tile_fade diff --git a/src/random.asm b/src/random.asm index 2c827f8..bd0eba1 100644 --- a/src/random.asm +++ b/src/random.asm @@ -1,16 +1,19 @@ RANDOM_SEED EQU 10 -RANDOM_LENGTH EQU 40 +RANDOM_X_LENGTH EQU 40 +RANDOM_Y_LENGTH EQU 36 SECTION "random vars", WRAM0 seed:: DS 1 -next_random_offset:: DS 1 +next_random_x_offset:: DS 1 +next_random_y_offset:: DS 1 SECTION "random", ROM0 init_random:: xor a - ld [next_random_offset], a + ld [next_random_x_offset], a + ld [next_random_y_offset], a ld a, RANDOM_SEED ld [seed],a ret @@ -22,7 +25,7 @@ init_random:: get_random_sprite_x:: ld hl, random_sprite_x ; get start of random LUT ld b, 0 - ld a, [next_random_offset] ; current offset in LUT + ld a, [next_random_x_offset] ; current offset in LUT ld c, a add hl, bc ld b, [hl] @@ -31,11 +34,37 @@ get_random_sprite_x:: ld a, c inc a - cp a, RANDOM_LENGTH + cp a, RANDOM_X_LENGTH jp nz, .no_reset ld a, 0 .no_reset - ld [next_random_offset], a + ld [next_random_x_offset], a + ld a, b + ret + + + +; Output: +; A - random tile-aligned x coord +; Destroys: +; BC, HL +get_random_sprite_y:: + ld hl, random_sprite_y ; get start of random LUT + ld b, 0 + ld a, [next_random_y_offset] ; current offset in LUT + ld c, a + add hl, bc + ld b, [hl] + + ; inc offset + ld a, c + inc a + + cp a, RANDOM_X_LENGTH + jp nz, .no_reset + ld a, 0 +.no_reset + ld [next_random_y_offset], a ld a, b ret @@ -74,6 +103,13 @@ fast_random:: ; Table of tile-aligned sprite x values. Two sequences are used to improve ; randomness. +; PowerShell: (0..19 | get-random -count 20 | %{'{0:X}' -f (8 + ($_ * 8)) }) -join ',$' random_sprite_x: DB $78,$40,$50,$58,$90,$70,$20,$48,$A0,$18,$88,$68,$8,$60,$80,$98,$28,$38,$30,$10 DB $60,$78,$48,$18,$88,$50,$68,$90,$8,$58,$38,$20,$70,$40,$80,$30,$28,$10,$98,$A0 + +; Table of tile-aligned sprite y values. Two sequences are used to improve +; randomness. +random_sprite_y: +DB $50,$88,$60,$20,$10,$58,$70,$78,$80,$38,$68,$48,$40,$90,$30,$28,$98,$18 +DB $20,$50,$48,$30,$68,$88,$10,$38,$70,$60,$98,$80,$18,$78,$58,$40,$28,$90 \ No newline at end of file