The Theme: ResizeFeedingAlien

```       Name: ResizeFeedingAlien                                      [Show more]
Type: Subroutine
Category: The Theme
Summary: Change the size of an alien so it grows bigger as it feeds
Deep dive: Alien feeding and growth patterns
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* UpdateAliens (Part 2 of 5) calls ResizeFeedingAlien
* UpdateAliens (Part 4 of 5) calls ResizeFeedingAlien

This routine grows the alien in slot 31 or 32 by scaling the z-coordinates for
the alien's four object points. Because object points are stored with the
scale factor in bits 4 to 7, we can double the size of the alien by adding
16 to each z-coordinate, thus doubling the scale factor.

If bit 7 of A is set, then this routine resets the size to the smallest
feeding stage, which has a scale factor of 32 (i.e. 2^2).

Arguments:

A                    Determines how the alien is resized:

* If bit 7 is clear, double the size of the alien

* If bit 7 is set, reset the alien to stage 1

Y                    The number of the alien slot to process (31 or 32),
which determines which object points have their
z-coordinates updated in the zObjectPoint table:

* Resize object points 183 to 186 for alien slot 31

* Resize object points 188 to 191 for alien slot 32

Returns:

zObjectPoint         Updated z-coordinates for the relevant object points,
scaled as required

Y                    Y is unchanged

.ResizeFeedingAlien

STA K                  \ Store the A argument in K so we can retrieve it below

STY T                  \ Store Y in T so we can ensure it is unchanged by the
\ routine

LDX #3                 \ We do the following loop four times, to update the
\ alien's four object ID z-coordinates, so set a loop
\ counter in X

LDA #186               \ Set A to the object point ID to update for slot 31, so
\ we update points 183 to 186 in the loop

CPY #31                \ If this is alien slot 31, skip the following
BEQ size1              \ instruction

LDA #191               \ Set A to the object point ID to update for slot 32, so
\ we update points 188 to 191 in the loop

.size1

TAY                    \ Copy the object point ID we just set in A into Y, so
\ we can use it as an index

.size2

LDA zObjectPoint,Y     \ Set A to the z-coordinate of the object point

CLC                    \ Add 16 to the z-coordinate, which doubles the scale
ADC #16                \ factor in bits 4 to 7

BIT K                  \ If bit 7 of K is clear, skip the following two
BPL size3              \ instructions, as we are done

AND #15                \ Otherwise bit 7 of S is set, so we need to reset the
ORA #32                \ scale to the first feeding stage, which we can do like
\ this:
\
\   z = 32 + (z + 16) MOD 16
\
\ This gives us a result with 32 in bits 4 to 7 (i.e. a
\ scale factor of 2^2) while retaining the value in bits
\ 0 to 3

.size3

STA zObjectPoint,Y     \ Store the updated z-coordinate

DEY                    \ Decrement the object point ID

DEX                    \ Decrement the loop counter

BPL size2              \ Loop back until we have updated three slots

LDY T                  \ Restore Y from T, so it doesn't get changed by the
\ routine

RTS                    \ Return from the subroutine
```