collisions

This commit is contained in:
Tyrel Souza 2023-01-24 00:39:34 -05:00
parent 3bdf1a28c1
commit 0b4f05e1c2
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
3 changed files with 125 additions and 0 deletions

View File

@ -8,6 +8,8 @@
;; Variables declared in RAM zero-page ;; Variables declared in RAM zero-page
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.segment "ZEROPAGE" .segment "ZEROPAGE"
Collision: .res 1 ; 0 or 1, if 0 no collision, if 1, collisison
Buttons: .res 1 ; Pressed buttons (A|B|Sel|Start|Up|Dwn|Lft|Rgt) Buttons: .res 1 ; Pressed buttons (A|B|Sel|Start|Up|Dwn|Lft|Rgt)
PrevButtons: .res 1 ; Stores the previous buttons from the last frame PrevButtons: .res 1 ; Stores the previous buttons from the last frame
@ -39,6 +41,11 @@ ParamYPos: .res 1 ; Used as parameter to subroutine
ParamTileNum: .res 1 ; Used as parameter to subroutine ParamTileNum: .res 1 ; Used as parameter to subroutine
ParamNumTiles: .res 1 ; Used as parameter to subroutine ParamNumTiles: .res 1 ; Used as parameter to subroutine
ParamAttribs: .res 1 ; Used as parameter to subroutine ParamAttribs: .res 1 ; Used as parameter to subroutine
ParamRectX1: .res 1 ; Used as parameter to subroutine
ParamRectY1: .res 1 ; Used as parameter to subroutine
ParamRectX2: .res 1 ; Used as parameter to subroutine
ParamRectY2: .res 1 ; Used as parameter to subroutine
PrevOAMCount: .res 1 ; Store the previous number of bytes that were sent to the OAM PrevOAMCount: .res 1 ; Store the previous number of bytes that were sent to the OAM
@ -327,6 +334,103 @@ EndRoutine:
rts rts
.endproc .endproc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Subroutine to check collisions
;; PARAMS: ParamXPos, ParamYPos (x and y of missile)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.proc CheckEnemyCollision
txa
pha ; Push and save the X register in the stack
ldx #0
stx Collision ; Collision = 0
EnemiesCollisionLoop:
cpx #MAX_ACTORS * .sizeof(Actor)
beq FinishCollisionCheck
lda ActorsArray+Actor::Type,x
cmp #ActorType::AIRPLANE
bne NextEnemy ; If not an airplane - skip to next enemy type
;; LOAD BOUNDING BOX X1,Y1,X2,Y2
lda ActorsArray+Actor::XPos,x ; Bounding Box X1Y1
sta ParamRectX1
lda ActorsArray+Actor::YPos,y
sta ParamRectY1
lda ActorsArray+Actor::XPos,x ; Bounding Box X2Y2
clc
adc #22 ; Get right value of the airplane bounding box by adding 22
sta ParamRectX2
lda ActorsArray+Actor::YPos,y ; Bottom of box by adding 8, one sprite tall
clc
adc #8
sta ParamRectY2
jsr IsPointInsideBoundingBox
lda Collision
beq NextEnemy
lda #ActorType::NULL
sta ActorsArray+Actor::Type,x
jmp FinishCollisionCheck
NextEnemy:
txa
clc
adc #.sizeof(Actor)
tax
jmp EnemiesCollisionLoop
FinishCollisionCheck:
pla
tax
rts
.endproc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Subroutine to check if a point is inside a bounding box.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Params:
;; (ParamXPos, ParamYPos) - coords to be tested
;; (ParamRectX1,ParamRectY1,ParamRectX2,ParamRectY2) Are rectangle coords)
;; Output:
;; Collision flag is either 1 or 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.proc IsPointInsideBoundingBox
lda ParamXPos
cmp ParamRectX1
bcc PointIsOutside
lda ParamYPos
cmp ParamRectY1
bcc PointIsOutside
lda ParamXPos
cmp ParamRectX2
bcs PointIsOutside
lda ParamYPos
cmp ParamRectY2
bcs PointIsOutside
PointIsInside:
lda #1
sta Collision
jmp EndCollisionCheck
PointIsOutside:
lda #0
sta Collision
EndCollisionCheck:
rts
.endproc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Subroutine to loop all actors and update them (position, velocity, etc.) ;; Subroutine to loop all actors and update them (position, velocity, etc.)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -345,6 +449,27 @@ EndRoutine:
lda #ActorType::NULL lda #ActorType::NULL
sta ActorsArray+Actor::Type,x ; Remove the missile from the array sta ActorsArray+Actor::Type,x ; Remove the missile from the array
SkipMissile: SkipMissile:
CheckCollision:
lda ActorsArray+Actor::XPos,x
clc
adc #3
sta ParamXPos ; Missile position to be checked X += 3
lda ActorsArray+Actor::YPos,y
clc
adc #1
sta ParamYPos ; Missile position to be checked Y += 1
jsr CheckEnemyCollision ; Perform collision check between the missile and other enemy actor
lda Collision
beq NoCollisionFound
lda #ActorType::NULL
sta ActorsArray+Actor::Type,x
NoCollisionFound:
jmp NextActor jmp NextActor
: :

Binary file not shown.

Binary file not shown.