Car geometry: MoveAndDrawCars

```       Name: MoveAndDrawCars                                         [Show more]
Type: Subroutine
Category: Car geometry
Summary: Move the cars around the track and draw any that are visible, up
to a maximum of five
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* MainDrivingLoop (Part 2 of 5) calls MoveAndDrawCars

.MoveAndDrawCars

LDA qualifyingTime     \ If bit 7 of qualifyingTime is set then this is a
BMI Delay              \ practice lap (i.e. qualifyingTime = 255), so there are
\ no other cars to draw
\
\ To maintain the same game speed as for races, we jump
\ to Delay to pause for a while before returning from
\ the subroutine using a tail call

LDX positionBehind     \ Set X to the position of the driver behind us

LDY driversInOrder,X   \ Set Y to the number of the driver in behind us

LDA objectStatus,Y     \ Clear bit 7 of the car object's status byte, to flag
AND #%01111111         \ the car behind us as being visible
STA objectStatus,Y

JSR MoveCars           \ Move the cars around the track

JSR ProcessOvertaking  \ Process overtaking manoeuvres for the non-player
\ drivers

JSR HideAllCars        \ Set all the cars to be hidden

JSR SetPlayerPositions \ Set the current player's position, plus the position
\ ahead and the position behind

LDX currentPosition    \ Set X to the current player's position

LDY #5                 \ We now work our way through the five nearest cars in
\ front of us, so set a loop counter in Y

.dcar1

BIT directionFacing    \ If bit 7 of directionFacing is clear, then we are

JSR GetPositionBehind  \ We are facing backwards, so set X to the number of
\ the position behind position X, to get the number of
\ the car that we are looking at

.dcar2

JSR GetPositionAhead   \ We are facing forwards, so set X to the number of the
\ position ahead of position X, to get the number of
\ the car that we are looking at

.dcar3

STY thisDriverNumber   \ Store the loop counter in thisDriverNumber so we can
\ retrieve it after the following call

STX thisPosition       \ Store the position of the car we are considering in
\ thisPosition

JSR BuildVisibleCar    \ Build the car object if it is visible, so we can draw
\ it below

LDX thisPosition       \ Retrieve the position of the car that we stored in
\ thisPosition above

LDY thisDriverNumber   \ Retrieve the value of the loop counter that we stored
\ in thisDriverNumber above

DEY                    \ Decrement the loop counter

BPL dcar1              \ Loop back until we have processed five cars in front

JSR DrawCars           \ Draw all the cars, with the closest car in front of us
\ split into four objects

LDX positionBehind     \ Set X to the position of the driver behind us

JSR BuildVisibleCar    \ Build the car object if it is visible, so it can be
\ shown in the mirror if close enough

RTS                    \ Return from the subroutine
```