Aviator on the BBC Micro

The Theme: CheckIfAlienIsHit (Part 2 of 2)

```       Name: CheckIfAlienIsHit (Part 2 of 2)                         [Show more]
Type: Subroutine
Category: The Theme
Summary: Check to see whether the alien has been hit, and if so, initiate
the explosion
Deep dive: Detecting alien hits
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

LDA #0                 \ Set hitTimer = 0 so the default result is a miss
STA hitTimer

LDY objectId           \ Set Y to the object ID of the alien

JSR GetAlienWeakSpot   \ Calculate the coordinate of the alien's weak spot as
\ follows:
\
\   (I+2 W+2) = xObject + (5 Q)
\
\   (I+1 W+1) = yObject
\
\   (I W)     = zObject + (5 Q)
\
\ So for the alien object, the weak spot is at a point
\ coordinate of ((5 Q), 0, (5 Q)) within the object

LDX #228               \ Set VV to iterate through the following in the outer
STX VV                 \ loop below (i.e. from ahit6 to the end):
\
\   * 228 = the trailing end of the left bullet trail
\
\   * 230 = the trailing end of the right bullet trail

.ahit6

LDX VV                 \ Set X to denote the trailing end of the bullet trail
\ that we are analysing (228 or 230)

LDA #31                \ Set WW to act as a loop counter for when we check
STA WW                 \ along the length of the bullet trail for a hit, moving
\ 1/32 of the way for each of the 32 iterations

LDY #0                 \ Set Y to loop through the three axes

.ahit7

STY Q                  \ Store the axis counter in Q so we can retrieve it
\ below

JSR GetTrailVectorStep \ Set (A V R) to 1/32 of the vector for the specified
\ bullet trail

LDY Q                  \ Restore the axis counter into Y

STA xTemp2Hi,Y         \ Set (xTemp2Hi xTemp2Lo xTemp1Lo) = (A V R)
LDA V
STA xTemp2Lo,Y
LDA R
STA xTemp1Lo,Y

INY                    \ Increment the loop counter to move on to the next axis

CPY #3                 \ Loop back until we have calculated the bullet trail
BNE ahit7              \ vector

LDX #LO(xTemp2Lo)      \ Set X so the call to CopyWorkToPoint copies the
\ coordinates from (xTemp2, yTemp2, zTemp2)

LDY #0                 \ Set Y so the call to CopyWorkToPoint copies the
\ coordinates to point 0

JSR CopyWorkToPoint    \ Copy the coordinates from (xTemp2, yTemp2, zTemp2)
\ to point 0

\ Point 0 now contains the vector of the bullet trail
\ for the bullet specified in VV, with the lowest byte
\ dropped, so each axis contains (A V) from the above

LDY VV                 \ Set Y to the bullet that we are analysing (228 or 230)

JSR CheckAlienWeakSpot \ Check whether this end of the bullet trail has hit the
\ alien's weak spot, and if it has, set up the explosion
\ and return from the subroutine

LDY VV                 \ Set Y to the bullet that we are analysing (228 or 230)

LDX #216               \ Set X to iterate through the coordinate offsets for
\ object 0

.ahit8

TYA                    \ Point Y to the next axis (xObject, yObject, zObject)
CLC                    \
ADC #nextAxis          \ The first iteration of this loop has Y = 228 or 230,
TAY                    \ so this bumps Y onto 268 or 260, which gets truncated
\ in the 8-bit register to 12 or 14, so this moves Y
\ through the xObject, yObject and zObject values for
\ object 12 (when the initial value of Y = 228) or
\ object 14 (when the initial value of Y = 230)

TXA                    \ Point X to the next axis (xObject, yObject, zObject)
CLC                    \
ADC #nextAxis          \ The first iteration of this loop has X = 216, so this
TAX                    \ bumps X onto 256, which gets truncated in the 8-bit
\ register to 0, so this moves X through the xObject,
\ yObject and zObject values for object 0

LDA xObjectLo,Y        \ Copy the coordinate from the bullet object to object 0
STA xObjectLo,X

CPX #200               \ Loop back until we have copied all six coordinates
BNE ahit8

\ By this point, object 0 is at the same coordinate as
\ the bullet we are checking, and we now work our way
\ along the trail vector, adding the 1/32 vector that
\ we calculated above for each of the 32 iterations of
\ the following loop

.ahit9

LDY #2                 \ Set a counter in Y to iterate through the three axes
\ (the comments below are for the x-axis calculation)

LDX #nextAxis*2        \ Set the index in X to point to the z-coordinate for
\ object 0, which we decrement by nextAxis over each
\ iteration to work through the three axes

.ahit10

\ Above we set (xTemp2Hi xTemp2Lo xTemp1Lo) to the 1/32
\ vector, but we only copied (xTemp2Hi xTemp2Lo) into
\ point 0, and ignored the fractional part, so we add
\ that part to the object 0 coordinate first

LDA xTemp1Lo,Y         \ Set xTemp2Hi = xTemp2Hi + xTemp1Lo
CLC                    \
ADC xTemp2Hi,Y         \ to work out when the fractional part cumulatively
STA xTemp2Hi,Y         \ adds up to an integer (in other words, we store the
\ cumulative sum of the fractional part from xTemp1Lo in
\ xTemp2Hi)

\ skip incrementing the object coordinate

\ Otherwise the cumulative sum of the fractional part
\ just reached an integer, so we need to add an extra
\ integer (1) to the object 0 coordinate

INC xObjectLo,X        \ Increment the object coordinate for object 0 in
BNE ahit11             \ (xObjectHi xObjectLo), starting with the low byte
INC xObjectHi,X        \ and incrementing the high byte if the low byte
\ overflows

.ahit11

TXA                    \ Point X to the next axis (zObject, yObject, xObject)
SEC
SBC #nextAxis
TAX

DEY                    \ Decrement the axis counter

BPL ahit10             \ Loop back until we have processed all three axes

\ We have now moved object 0 along the trail by adding
\ the fractional part of the 1/32 vector xTemp1Lo, so
\ now we can add the rest of the vector which we stored
\ in point 0 above, to move along the trail by exactly
\ 1/32 of the trail vector

LDX #0                 \ Move object 0 by the vector in point 0, which we set
LDY #0                 \ to the vector of the bullet trail above

\ We now check whether this point along the bullet trail
\ is in the alien's weak spot

LDY #216               \ Check whether object 0 has hit the alien's weak
JSR CheckAlienWeakSpot \ spot, and if it has, set up the explosion and return
\ from the subroutine

DEC WW                 \ Decrement the counter we set in WW so we repeat this
\ process 32 times

BPL ahit9              \ Loop back until we have stepped all the way along the
\ trail vector, from the back end of the trail all the
\ way to the bullet at the front, in steps of 1/32,
\ checking at each point whether it is in the weak spot

\ If we get here then we have now checked the entire
\ trail of the left bullet for a hit, so we repeat the
\ whole thing for the right bullet trail

LDA VV                 \ Set VV = VV + 2
CLC                    \
ADC #2                 \ so VV now points to the next bullet trail (i.e. 230)
STA VV

CMP #232               \ If VV = 232 then we have now processed both bullet
BEQ ahit12             \ trails, so jump to ahit12 to return from the
\ subroutine

JMP ahit6              \ Otherwise loop back to ahit6 to process the next
\ bullet trail

.ahit12

RTS                    \ Return from the subroutine
```