Skip to content

Commit

Permalink
game loop restored
Browse files Browse the repository at this point in the history
enemies can be damaged
player dies after 3 hits
  • Loading branch information
LaroldsJubilantJunkyard committed Dec 15, 2023
1 parent 8f759a1 commit 53758f0
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 181 deletions.
5 changes: 5 additions & 0 deletions galactic-armada/src/main/includes/constants.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ DEF BULLET_MOVE_SPEED EQU 20

DEF PLAYER_MOVE_SPEED EQU 15
DEF PADDLE_Y_POSITION EQU 136


DEF ENEMY_COLLISION_NOTHING EQU 0
DEF ENEMY_COLLISION_END EQU 2
DEF ENEMY_COLLISION_DAMAGED EQU 1
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ UpdateGameplayState::
call UpdateBackground

ld a, [wObjects+object_healthByte]
cp a, 250
cp a, 255
jp z, EndGameplay

ret
Expand Down
7 changes: 6 additions & 1 deletion galactic-armada/src/main/states/gameplay/objects/bullets.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ SECTION "Bullets", ROM0

; ANCHOR_END: bullets-top

; ANCHOR: bullets-update
UpdateBullet::

; The start of our object will be in bc
; Copy that to hl so we can check/adjust some bytes
ld h,b
ld l, c

; ANCHOR_END: bullets-update
; ANCHOR: bullets-update2
; Get to our y position
ld de, object_yLowByte
add hl, de
Expand All @@ -26,6 +29,8 @@ UpdateBullet::
ld a, [hl]
sbc a, 0
ld [hl], a
; ANCHOR_END: bullets-update2
; ANCHOR: bullets-update3

; If our high byte is below 10, we're not offscreen
ld a, [hl]
Expand All @@ -43,7 +48,7 @@ UpdateBullet_OutOfScreen:
ld [hl], a

ret
; ANCHOR_END: draw-bullets
; ANCHOR_END: bullets-update3
; ANCHOR: fire-bullets
FireNextBullet::
Expand Down
107 changes: 107 additions & 0 deletions galactic-armada/src/main/states/gameplay/objects/enemies-collision.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
include "src/main/includes/constants.inc"
SECTION "EnemiesCollision", ROM0

CheckCollisionForCurrentEnemy::
push hl

ld a, 16
ld [wSizeX], a
ld [wSizeY], a
ld de, wObjects
call CheckCollisionWithObjectsInHL_andDE

pop hl
jp nz, EnemyPlayerCollision
jp UpdateEnemy_CheckAllBulletCollision

UpdateEnemy_CheckAllBulletCollision:

ld b,MAX_BULLET_COUNT
ld de, wObjects+BULLETS_START

UpdateEnemy_CheckBulletCollision:

; Save the start of our enemy's bytes
; Save the current bullet counter
; Save which bullet we are looking at
push hl
push bc
push de

ld a, 16
ld [wSizeX], a
ld [wSizeY], a
call CheckCollisionWithObjectsInHL_andDE

; Retrieve the curernt bullet counter
; Return hl to the start of our enemies bytes
; Retrieve which object we were looking at
pop de
pop bc
pop hl

jp z, MoveToNextBullet
jp EnemyBulletCollision

MoveToNextBullet:

; Decrease b
; return if it reaches zero
ld a, b
dec a
ld b, a
and a
jp z, NoDamage

; Move to the next object
ld a, e
add a, PER_OBJECT_BYTES_COUNT
ld e, a

jp UpdateEnemy_CheckBulletCollision


NoDamage::

; Set a to be 0
ld a, ENEMY_COLLISION_NOTHING
ret

EnemyBulletCollision:

push hl

; Copy de to hl
ld h, d
ld l, e

; Set the bullet as inactive
ld a, 0
ld [hl], a

; Go back to the enemy bytes
pop hl

ld a, ENEMY_COLLISION_DAMAGED

ret

EnemyPlayerCollision:

push hl
push bc

call DamagePlayer
ld hl, wLives
ld de, $9C13 ; The window tilemap starts at $9C00
ld b, 1
call DrawBDigitsHL_OnDE

pop bc
pop hl

; Set a to be 1
ld a, ENEMY_COLLISION_END
ret
152 changes: 47 additions & 105 deletions galactic-armada/src/main/states/gameplay/objects/enemies.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@
include "src/main/includes/hardware.inc"
include "src/main/includes/constants.inc"

SECTION "EnemyVariables", WRAM0

wCurrentEnemyX:: db
wCurrentEnemyY:: db

wSpawnCounter: db
wNextEnemyXPosition: db
wActiveEnemyCounter::db
wUpdateEnemiesCounter:db
wUpdateEnemiesCurrentEnemyAddress::dw

; ANCHOR_END: enemies-start
SECTION "Enemies", ROM0
; ANCHOR_END: enemies-start

; ANCHOR: enemies-update-per-enemy2
; ANCHOR: enemies-update
UpdateEnemy::

; get the start of our object back in hl
Expand All @@ -41,98 +30,67 @@ UpdateEnemy::
; If our high byte is below 10, we're not offscreen
ld a, [hl]
pop hl

; restore our original hl
pop hl
cp a, 10
jp nc, DeactivateEnemy


.UpdateEnemy_CheckPlayerCollision

push hl


ld a, 16
ld [wSizeX], a
ld [wSizeY], a
ld de, wObjects
call CheckCollisionWithObjectsInHL_andDE

; Check for collision for current enemy
; if a=1, we deactivate
; if a=2, we have shot the enemy
; otherwise, do nothing
call CheckCollisionForCurrentEnemy
pop hl
jp nz, EnemyPlayerCollision

.UpdateEnemy_CheckAllBulletCollision

ld b,MAX_BULLET_COUNT
ld de, wObjects+BULLETS_START
cp a, ENEMY_COLLISION_END
jp z, DeactivateEnemy
cp a, ENEMY_COLLISION_DAMAGED
jp z, DamageEnemy
ret
; ANCHOR_END: enemies-update

UpdateEnemy_CheckBulletCollision:
; ANCHOR: enemies-damage
DamageEnemy:

; Save the start of our enemy's bytes
; Save the current bullet counter
; Save which bullet we are looking at
push hl
push bc
; Decrease the enemies health byte
push de
ld de, object_healthByte
add hl, de
ld a, [hl]
dec a
ld [hl], a

ld a, 16
ld [wSizeX], a
ld [wSizeY], a
call CheckCollisionWithObjectsInHL_andDE

; Retrieve the curernt bullet counter
; Return hl to the start of our enemies bytes
; Retrieve which object we were looking at
pop de
pop bc
pop hl

push hl

jp nz, DamageEnemy

pop hl

MoveToNextEnemy:

; Decrease b
; return if it reaches zero
ld a, b
dec a
ld b, a
and a
ret z

; Move to the next object
ld a, e
add a, PER_OBJECT_BYTES_COUNT
ld e, a

jp UpdateEnemy_CheckBulletCollision

EnemyPlayerCollision::
; if the health byte is zero, kill the enemy
and a
jp z, KillEnemy

push hl
push bc

call DamagePlayer
ld hl, wLives
ld de, $9C13 ; The window tilemap starts at $9C00
ld b, 1
call DrawBDigitsHL_OnDE
; Move to the damage byte
push de
ld de, object_healthByte
add hl, de
pop de

pop bc
pop hl
; Set as damaged for 128 frames
ld a, 128
ld [hl], a

jp DeactivateEnemy
; Move to the next
ret
; ANCHOR_END: enemies-damage

; ANCHOR: enemies-kill
KillEnemy::

; Deactivate our bullet in de
ld a,0
ld [de], a

push hl
push bc
Expand All @@ -145,35 +103,19 @@ KillEnemy::

pop bc
pop hl
DeactivateEnemy::

ld a,0
ld [hl], a

ret
; ANCHOR_END: enemies-kill
; ANCHOR: enemies-deactivate
DeactivateEnemy::

DamageEnemy:

push de
ld de, object_healthByte
add hl, de
ld a, [hl]
dec a

pop hl
pop de
cp a, 255
jp z, KillEnemy

ld a,0
ld [hl], a

push de
ld de, object_damageByte-object_healthByte
add hl, de
pop de

ld a, 128
ld [hl], a
ret

jp MoveToNextEnemy
; ANCHOR_END: enemies-deactivate
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ Damaged:

jp c, UpdateObjectPool_GoToNextObject

push hl


NotDamaged:

pop hl
Expand Down
Loading

0 comments on commit 53758f0

Please sign in to comment.