Aviator on the BBC Micro

# The Theme: FireGuns

```       Name: FireGuns                                                [Show more]
Type: Subroutine
Category: The Theme
Summary: Create the bullet objects and send them on their way
Deep dive: Adding bullets to the world
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* UpdateFlightModel (Part 3 of 4) calls FireGuns

Other entry points:

FireGuns-1           Contains an RTS

.FireGuns

LDA firingStatus       \ If either firingStatus or hitTimer are non-zero, then
ORA hitTimer           \ there are either bullets still in the air, or we only
BNE FireGuns-1         \ just hit an alien. In either case we can't fire the
\ guns, so return from the subroutine (as FireGuns-1
\ contains an RTS)

LDX #228               \ Set point 228 to (0, 0, zVelocityPHi + 200)
JSR SetPointToOrigin   \
\ starting with the zeroes

LDA zVelocityPHi       \ And then setting the low byte of the z-coordinate
CLC
STA zPointLo+228

LDA #&FF               \ Set the point with ID 95 to (&FFF6, &FFFC, &FF14)
LDX #95                \                           = (-10, -4, -236)
JSR SetPoint           \
\ which is the vector from the plane back to object 12
\ at the trailing end of the left bullet's trail
\

LDA #&14               \ And then set the low bytes
STA zPointLo+95
LDA #&F6
STA xPointLo+95
LDA #&FC
STA yPointLo+95

LDA #228               \ Set GG to point ID 228, to pass to the call to
STA GG                 \ SetPointCoords

LDA #9                 \ Set the matrix number so the call to SetPointCoords
STA matrixNumber       \ uses matrix 2 in the calculation, so it rotates the
\ point from the plane's frame of reference to the
\ outside world's frame of reference (so we spawn the
\ bullets using coordinates that are relative to the
\ plane, but calculate them in the following as
\ world-relative coordinates, so they are then
\ independent of the plane's future movement)

STA firingStatus       \ Set firingStatus = 9, which is a non-zero value, to
\ indicate that there are bullets are in the air (the
\ value 9 isn't significant beyond the fact that it is
\ non-zero)

JSR SetPointCoords     \ Calculate the coordinates for point 228, which also
\ returns the coordinates in (xTemp1, yTemp1, zTemp1)

LDX #229               \ We now copy the coordinates from (xTemp1, yTemp1,
\ zTemp1) to points 229, 230 and 231, so we set a
\ counter in X for the point IDs

.fire1

JSR CopyTempToPoint    \ Copy from (xTemp1, yTemp1, zTemp1) into the
\ coordinates for point X

INX                    \ Increment the point ID

CPX #232               \ Loop back until we have copied (xTemp1, yTemp1,
BNE fire1              \ zTemp1) into points 229, 230 and 231

LDA #95                \ Set GG to point ID 95, to pass to the call to
STA GG                 \ SetPointCoords

JSR SetPointCoords     \ Calculate the coordinates for point 95

LDX #LO(xPlaneLo)      \ Set X so the call to CopyWorkToPoint copies the
\ coordinates from (xPlane, yPlane, zPlane)

LDY #96                \ Set Y so the call to CopyPointToWork copies the
\ coordinates to point 96

JSR CopyWorkToPoint    \ Copy the coordinates from (xPlane, yPlane, zPlane)
\ to point 96

LDY #12                \ We now set the object coordinates for objects 12 to 15
\ to the coordinates in point 96, so set Y as the object
\ ID, starting with 12

LDX #96                \ And set X as the point ID to add in the call to

.fire2

JSR SetObjectToOrigin  \ Set the object coordinates for object Y to (0, 0, 0)

JSR AddPointToObject   \ Add the vector in point 96 to the object coordinates,
\ so this sets the location of the object to that of
\ point 96, i.e. the coordinates of the plane

INY                    \ Increment the object ID

CPY #16                \ Loop back until we have set objects 12 to 15 to the
BNE fire2              \ coordinates in point 96, so they are all now at the
\ plane's coordinates

LDY #12                \ Add the vector in point 95 to the object coordinates
LDX #95                \ for object 12

STX objectAnchorPoint  \ Store the vector in point 95 as the object anchor
\ point, so the following calculations use this as the
\ anchor point
\
\ This means that the following coordinate calculations
\ will return the vector from the plane's location to
\ each point, as we are telling SetObjPointCoords to
\ take the anchor point vector (from the plane to point
\ 95) and add on the vector of the object point (which
\ is the interior vector of the point within the object)

LDA #96                \ Calculate the coordinates for object point 96 with
STA GG                 \ anchor point 95
JSR SetObjPointCoords

LDA #97                \ Calculate the coordinates for object point 97 with
STA GG                 \ anchor point 95, which also sets (xTemp1 yTemp1
JSR SetObjPointCoords  \ zTemp1) to the rotated vector from the anchor point
\ (point 95) to point 97

LDX #98                \ Set point 98's coordinates to point 96's coordinates +
LDY #96                \ (xTemp1 yTemp1 zTemp1), the latter containing the
JSR AddTempToPoint     \ vector from point 95 to point 97

\ Finally, we add the following vectors to the object
\ points for objects 13 to 15:
\
\   * Move object 13 by the vector in point 96
\
\   * Move object 14 by the vector in point 97
\
\   * Move object 15 by the vector in point 98

LDY #15                \ Set Y as the object ID that gets moved in the call to

LDX #98                \ Set X as the point ID to add to the object coordinates
\ in the call to AddPointToObject

.fire3

JSR AddPointToObject   \ Add the vector in point X to the object coordinates
\ for point Y

DEX                    \ Decrement the point ID

DEY                    \ Decrement the object ID

CPY #12                \ Loop back until we have added points 96 to 98 to
BNE fire3              \ objects 13 to 15

RTS                    \ Return from the subroutine
```