Jet Set Willy: Routine at 8FBC

| Routines

8FBC: Move Willy (3)

Used by the routines at 8DD3 and 8ED4. This routine moves Willy left or right if necessary.
8FBC | LD A,($85D0) | Pick up Willy's direction and movement flags from 85D0
8FBF | AND $02 | Is Willy moving left or right?
8FC1 | RET Z | Return if not
8FC2 | LD A,($85D6) | Pick up the rope status indicator from 85D6
8FC5 | DEC A | Is Willy on a rope?
8FC6 | BIT 7,A
8FC8 | RET Z | Return if so (Willy's movement along a rope is handled at 935E)
8FC9 | LD A,($85D0) | Pick up Willy's direction and movement flags from 85D0
8FCC | AND $01 | Is Willy facing right?
8FCE | JP Z,$9042 | Jump if so
Willy is moving left.
8FD1 | LD A,($85D2) | Pick up Willy's animation frame from 85D2
8FD4 | OR A | Is it 0?
8FD5 | JR Z,$8FDC | If so, jump to move Willy's sprite left across a cell boundary
8FD7 | DEC A | Decrement Willy's animation frame at 85D2
8FD8 | LD ($85D2),A
8FDB | RET |
Willy's sprite is moving left across a cell boundary. In the comments that follow, (x,y) refers to the coordinates of the top-left cell currently occupied by Willy's sprite.
8FDC | LD A,($85D1) | Pick up the airborne status indicator from 85D1
8FDF | LD BC,$0000 | Prepare BC for later addition
8FE2 | CP $00 | Is Willy jumping?
8FE4 | JR NZ,$900A | Jump if so
8FE6 | LD HL,($85D3) | Collect Willy's attribute buffer coordinates from 85D3
8FE9 | LD BC,$0000 | Prepare BC for later addition (again, redundantly)
8FEC | LD A,($80DA) | Pick up the direction byte of the ramp definition for the current room from 80DA
8FEF | DEC A | Now A=0x1F if the ramp goes up to the left, or 0x41 if it goes up to the right
8FF0 | OR $A1
8FF2 | XOR $E0
8FF4 | LD E,A | Point HL at the cell at (x-1,y+1) if the ramp goes up to the left, or at the cell at (x+1,y+2) if the ramp goes up to the right
8FF5 | LD D,$00
8FF7 | ADD HL,DE
8FF8 | LD A,($80C4) | Pick up the attribute byte of the ramp tile for the current room from 80C4
8FFB | CP (HL) | Is there a ramp tile in the cell pointed to by HL?
8FFC | JR NZ,$900A | Jump if not
8FFE | LD BC,$0020 | Prepare BC for later addition
9001 | LD A,($80DA) | Pick up the direction byte of the ramp definition for the current room from 80DA
9004 | OR A | Does the ramp go up to the right?
9005 | JR NZ,$900A | Jump if so
9007 | LD BC,$FFE0 | BC=-32 (the ramp goes up to the left)
900A | LD HL,($85D3) | Collect Willy's attribute buffer coordinates from 85D3
900D | LD A,L | Is Willy's screen x-coordinate 0 (on the far left)?
900E | AND $1F
9010 | JP Z,$948A | If so, move Willy into the room to the left
9013 | ADD HL,BC | Point HL at the cell at (x-1,y+1), or at the cell at (x-1,y) if Willy is on or about to step onto a ramp that goes up to the left, or at the cell at (x-1,y+2) if Willy is walking down a ramp
9014 | DEC HL
9015 | LD DE,$0020
9018 | ADD HL,DE
9019 | LD A,($80B2) | Pick up the attribute byte of the wall tile for the current room from 80B2
901C | CP (HL) | Is there a wall tile in the cell pointed to by HL?
901D | RET Z | Return if so without moving Willy (his path is blocked)
901E | LD A,($85CF) | Pick up Willy's y-coordinate (Y) from 85CF
9021 | SRA C | Now B=Y (if Willy is neither on nor about to step onto a ramp), or Y+16 (if Willy is walking down a ramp), or Y-16 (if Willy is on or about to step onto a ramp that goes up to the left); this will be Willy's new y-coordinate
9023 | ADD A,C
9024 | LD B,A
9025 | AND $0F | Is Willy at a point in a jump (left) where his sprite occupies three rows of cells?
9027 | JR Z,$9032 | Jump if not (Willy's sprite is cell-aligned)
9029 | LD A,($80B2) | Pick up the attribute byte of the wall tile for the current room from 80B2
902C | ADD HL,DE | Point HL at the cell at (x-1,y+2)
902D | CP (HL) | Is there a wall tile there?
902E | RET Z | Return if so without moving Willy (his path is blocked)
902F | OR A | Point HL at the cell at (x-1,y+1)
9030 | SBC HL,DE
9032 | OR A | Point HL at the cell at (x-1,y), or at the cell at (x-1,y-1) if Willy is on or about to step onto a ramp that goes up to the left, or at the cell at (x-1,y+1) if Willy is walking down a ramp
9033 | SBC HL,DE
9035 | LD ($85D3),HL | Save Willy's new attribute buffer coordinates (in HL) at 85D3
9038 | LD A,B | Save Willy's new y-coordinate at 85CF
9039 | LD ($85CF),A
903C | LD A,$03 | Change Willy's animation frame at 85D2 from 0 to 3
903E | LD ($85D2),A
9041 | RET |
Willy is moving right.
9042 | LD A,($85D2) | Pick up Willy's animation frame from 85D2
9045 | CP $03 | Is it 3?
9047 | JR Z,$904E | If so, jump to move Willy's sprite right across a cell boundary
9049 | INC A | Increment Willy's animation frame at 85D2
904A | LD ($85D2),A
904D | RET |
Willy's sprite is moving right across a cell boundary. In the comments that follow, (x,y) refers to the coordinates of the top-left cell currently occupied by Willy's sprite.
904E | LD A,($85D1) | Pick up the airborne status indicator from 85D1
9051 | LD BC,$0000 | Prepare BC for later addition
9054 | OR A | Is Willy jumping?
9055 | JR NZ,$9078 | Jump if so
9057 | LD HL,($85D3) | Collect Willy's attribute buffer coordinates from 85D3
905A | LD A,($80DA) | Pick up the direction byte of the ramp definition for the current room from 80DA
905D | DEC A | Now A=0x40 if the ramp goes up to the left, or 0x22 if it goes up to the right
905E | OR $9D
9060 | XOR $BF
9062 | LD E,A | Point HL at the cell at (x,y+2) if the ramp goes up to the left, or at the cell at (x+2,y+1) if the ramp goes up to the right
9063 | LD D,$00
9065 | ADD HL,DE
9066 | LD A,($80C4) | Pick up the attribute byte of the ramp tile for the current room from 80C4
9069 | CP (HL) | Is there a ramp tile in the cell pointed to by HL?
906A | JR NZ,$9078 | Jump if not
906C | LD BC,$0020 | Prepare BC for later addition
906F | LD A,($80DA) | Pick up the direction byte of the ramp definition for the current room from 80DA
9072 | OR A | Does the ramp go up to the left?
9073 | JR Z,$9078 | Jump if so
9075 | LD BC,$FFE0 | BC=-32 (the ramp goes up to the right)
9078 | LD HL,($85D3) | Collect Willy's attribute buffer coordinates from 85D3
907B | ADD HL,BC | Point HL at the cell at (x+2,y), or at the cell at (x+2,y+1) if Willy is walking down a ramp, or at the cell at (x+2,y-1) if Willy is on or about to step onto a ramp that goes up to the right
907C | INC HL
907D | INC HL
907E | LD A,L | Is Willy's screen x-coordinate 30 (on the far right)?
907F | AND $1F
9081 | JP Z,$949E | If so, move Willy into the room on the right
9084 | LD DE,$0020 | Prepare DE for addition
9087 | LD A,($80B2) | Pick up the attribute byte of the wall tile for the current room from 80B2
908A | ADD HL,DE | Point HL at the cell at (x+2,y+1), or at the cell at (x+2,y+2) if Willy is walking down a ramp, or at the cell at (x+2,y) if Willy is on or about to step onto a ramp that goes up to the right
908B | CP (HL) | Is there a wall tile in the cell pointed to by HL?
908C | RET Z | Return if so without moving Willy (his path is blocked)
908D | LD A,($85CF) | Pick up Willy's y-coordinate (Y) from 85CF
9090 | SRA C | Now B=Y (if Willy is neither on nor about to step onto a ramp), or Y+16 (if Willy is walking down a ramp), or Y-16 (if Willy is on or about to step onto a ramp that goes up to the right); this will be Willy's new y-coordinate
9092 | ADD A,C
9093 | LD B,A
9094 | AND $0F | Is Willy at a point in a jump (right) where his sprite occupies three rows of cells?
9096 | JR Z,$90A1 | Jump if not (Willy's sprite is cell-aligned)
9098 | LD A,($80B2) | Pick up the attribute byte of the wall tile for the current room from 80B2
909B | ADD HL,DE | Point HL at the cell at (x+2,y+2)
909C | CP (HL) | Is there a wall tile there?
909D | RET Z | Return if so without moving Willy (his path is blocked)
909E | OR A | Point HL at the cell at (x+2,y+1)
909F | SBC HL,DE
90A1 | LD A,($80B2) | Pick up the attribute byte of the wall tile for the current room from 80B2
90A4 | OR A | Point HL at the cell at (x+2,y), or at the cell at (x+2,y+1) if Willy is walking down a ramp, or at the cell at (x+2,y-1) if Willy is on or about to step onto a ramp that goes up to the right
90A5 | SBC HL,DE
90A7 | CP (HL) | Is there a wall tile in the cell pointed to by HL?
90A8 | RET Z | Return if so without moving Willy (his path is blocked)
90A9 | DEC HL | Point HL at the cell at (x+1,y), or at the cell at (x+1,y+1) if Willy is walking down a ramp, or at the cell at (x+1,y-1) if Willy is on or about to step onto a ramp that goes up to the right
90AA | LD ($85D3),HL | Save Willy's new attribute buffer coordinates (in HL) at 85D3
90AD | XOR A | Change Willy's animation frame at 85D2 from 3 to 0
90AE | LD ($85D2),A
90B1 | LD A,B | Save Willy's new y-coordinate at 85CF
90B2 | LD ($85CF),A
90B5 | RET |