.CheckForKeyPresses LDA #%10000000 \ Set bit 7 of panKeyBeingPressed to indicate that no STA panKeyBeingPressed \ pan key is being pressed (we will update this below \ if a pan key is being pressed) LDX #&8E \ Scan the keyboard to see if function key f1 is being JSR ScanKeyboard \ pressed ("Quit game") BNE ckey1 \ If function key f1 is not being pressed, jump to ckey1 \ to skip the following SEC \ Function key f1 is not being pressed, which quits the ROR quitGame \ game, so set bit 7 of quitGame so that when we return \ to the start of the main game loop it jumps to the \ main title loop to restart the game .ckey1 LDX #&9D \ Scan the keyboard to see if SPACE is being pressed JSR ScanKeyboard \ ("Toggle sights on/off") BNE ckey4 \ If SPACE is not being pressed, jump to ckey4 to reset \ the value of spaceKeyDebounce to flag that SPACE is \ not being pressed \ If we get here then SPACE is being pressed LDA spaceKeyDebounce \ If spaceKeyDebounce is non-zero then we have already BNE ckey6 \ toggled the sights but the player is still holding \ down SPACE, so jump to ckey6 to avoid toggling the \ sights again LDA sightsAreVisible \ Flip bit 7 of sightsAreVisible to toggle the sights on EOR #%10000000 \ and off STA sightsAreVisible BPL ckey2 \ If bit 7 is now clear then we just turned the sights \ off, so jump to ckey2 to remove them from the screen \ Otherwise bit 7 is now set, so we need to show the \ sights JSR InitialiseSights \ Initialise the variables used to manage the sights, so \ the sights appear in the middle of the screen JSR DrawSights \ Draw the sights on the screen JMP ckey3 \ Jump to ckey3 to skip the following .ckey2 JSR RemoveSights \ Remove the sights from the screen .ckey3 LDA #%10000000 \ Set bit 7 of A to store in spaceKeyDebounce, to flag \ that we have toggled the sights (so we can make sure \ we don't keep toggling the sights if SPACE is being \ held down) BNE ckey5 \ Jump to ckey5 to set spaceKeyDebounce to the value of \ A in (this BNE is effectively a JMP as A is never \ zero) .ckey4 \ If we get here then SPACE is not being pressed LDA #0 \ Clear bit 7 of spaceKeyDebounce to record that SPACE \ is not being pressed .ckey5 STA spaceKeyDebounce \ Set spaceKeyDebounce to the value of A, so we record \ whether or not SPACE is being pressed to make sure \ we don't keep toggling the sights if SPACE is held \ down .ckey6 LDY #14 \ Scan the keyboard for all 14 game keys in the gameKeys JSR ScanForGameKeys \ table BPL ckey7 \ ScanForGameKeys will clear bit 7 of the result if at \ least one pan key is being pressed, in which case jump \ to ckey7 to skip the following, so pan keys take \ precedence over the other game keys (which are ignored \ while panning is taking place) \ If we get here then no pan keys are being pressed LDA #%01101011 \ Set a bit pattern in sightsInitialMoves to control the STA sightsInitialMoves \ initial movement of the sights when a pan key is \ pressed and held down \ \ Specifically, this value is shifted left once on each \ call to this routine, with a zero shifted into bit 0, \ and we only move the sights when a zero is shifted out \ of bit 7 \ \ This means that when we start moving the sights, they \ move like this, with each step happening on one call \ of the interrupt handler: \ \ 0 = Move \ 1 = Pause \ 1 = Pause \ 0 = Move \ 1 = Pause \ 0 = Move \ 1 = Pause \ 1 = Pause \ \ ...and then we move on every subsequent shift, as by \ now all bits of sightsInitialMoves are clear \ \ This means the sights move more slowly at the start, \ with a slight judder, before speeding up fully after \ eight steps (so this applies a bit of inertia to the \ movement of the sights) LDA keyLogger+1 \ Set A to the key logger entry for "A", "Q", "R", "T", \ "B", "H", or "U" (absorb, transfer, create robot, \ create tree, create boulder, hyperspace, U-turn) BPL FocusOnKeyAction \ If there is a key press in the key logger entry, jump \ to FocusOnKeyAction to start focusing effort on \ implementing the relevant action after returning from \ the subroutine using a tail call \ If we get here then the player is not pressing "A", \ "Q", "R", "T", "B", "H", or "U" (absorb, transfer, \ create robot, create tree, create boulder, hyperspace, \ U-turn) LDA #%01000000 \ Set bit 6 of uTurnStatus so that when the "U" key is STA uTurnStatus \ next pressed, this will trigger a U-turn in the \ ProcessActionKeys routine \ \ This implements debounce so that holding down "U" will \ not continuously perform U-turns, and instead the \ player has to release "U" before they can do a second \ U-turn \ \ A U-turn is only performed in ProcessActionKeys when \ bit 6 of uTurnStatus is set, at which point bit 6 is \ cleared, and this is the only place where bit 6 is \ set, so this ensures only one U-turn is performed \ until we get here again, when "U" has been released BNE focu1 \ Jump to focu1 to return from the subroutine (this BNE \ is effectively a JMP as A is never zero) .ckey7 \ If we get here then at least one pan key is being \ pressed LDX sightsAreVisible \ If bit 7 of sightsAreVisible is clear then the sights BPL ckey8 \ are not being shown, so jump to ckey8 to skip the \ following, as we don't need to move the sights when \ they aren't on-screen \ If we get here then the sights are visible, so the pan \ keys move the sights rather than panning the view ASL sightsInitialMoves \ Shift sightsInitialMoves to the left, so we pull the \ next bit from the pattern that determines the initial \ movement of the sights BCS focu1 \ If we shifted a 1 out of bit 7 of sightsInitialMoves, \ jump to focu1 to return from the subroutine without \ moving the sights, as a set bit indicates a pause in \ the initial movement of the sights JSR MoveSights \ Move the sights according to the pan key presses in \ the key logger \ \ If the player moves the sights off the edge of the \ screen, this routine will set panKeyBeingPressed to \ "press" the relevant pan key so that the screen \ scrolls in the same direction to bring the sights \ back into view JMP ckey10 \ Jump to ckey10 to process any pan "key press" from the \ call to MoveSights .ckey8 LDA keyLogger \ Set A to the key logger entry for "S" and "D" (pan \ left, pan right), which are used to move the sights BPL ckey9 \ If there is a key press in the key logger entry, jump \ to ckey9 to store this value in panKeyBeingPressed (so \ panning left or right takes precedence over panning up \ or down) LDA keyLogger+2 \ Set A to the key logger entry for "L" and "," (pan \ up, pan down), which are used to move the sights BMI focu1 \ If there is no key press in the key logger entry then \ no pan keys are being pressed, so jump to focu1 to \ return from the subroutine without recording a pan key \ press in panKeyBeingPressed .ckey9 STA panKeyBeingPressed \ Set panKeyBeingPressed to the key logger value of the \ pan key that's being pressed, as follows: \ \ * 0 = pan right \ \ * 1 = pan left \ \ * 2 = pan up \ \ * 3 = pan down .ckey10 LDA panKeyBeingPressed \ If bit 7 of panKeyBeingPressed is set then no pan keys BMI focu1 \ are being pressed, so jump to focu1 to return from the \ subroutine STA latestPanKeyPress \ Set latestPanKeyPress to the key logger value of the \ pan key that's being pressed, so it contains the most \ recent pan key press (i.e. the current one) \ Fall through into FocusOnKeyAction to start focusing \ effort on the pan that the player wants us to doName: CheckForKeyPresses [Show more] Type: Subroutine Category: Keyboard Summary: Check for various game key presses and update the key logger and relevant variables (during the interrupt handler)Context: See this subroutine in context in the source code References: This subroutine is called as follows: * IRQHandler calls CheckForKeyPresses
[X]
Subroutine DrawSights (category: Sights)
Draw the sights on the screen, saving the existing screen contents in the sights pixel byte stash
[X]
Subroutine FocusOnKeyAction (category: Keyboard)
Tell the game to start focusing effort on the action that has been initiated, such as a pan of the landscape, absorb, transfer etc.
[X]
Subroutine InitialiseSights (category: Sights)
Initialise the variables used to manage the sights, so the sights appear in the middle of the screen
[X]
Subroutine MoveSights (category: Sights)
Check for up/down/left/right key presses and move the sights accordingly, panning the screen if they go past the screen edges
[X]
Subroutine RemoveSights (category: Sights)
Remove the sights from the screen
[X]
Subroutine ScanForGameKeys (category: Keyboard)
Scan for game key presses and update the key logger
[X]
Subroutine ScanKeyboard (category: Keyboard)
Scan the keyboard for a specific key press
[X]
Label ckey1 is local to this routine
[X]
Label ckey10 is local to this routine
[X]
Label ckey2 is local to this routine
[X]
Label ckey3 is local to this routine
[X]
Label ckey4 is local to this routine
[X]
Label ckey5 is local to this routine
[X]
Label ckey6 is local to this routine
[X]
Label ckey7 is local to this routine
[X]
Label ckey8 is local to this routine
[X]
Label ckey9 is local to this routine
[X]
Entry point focu1 in subroutine FocusOnKeyAction (category: Keyboard)
Store the current setting of focusOnKeyAction in the previousFocus variable so we can detect (in the ProcessGameplay routine) whether the player is still holding down a pan key after we finish scrolling the screen for the previous pan... and then return from the subroutine
[X]
Variable keyLogger in workspace Main variable workspace
The four-byte key logger for logging game key presses
[X]
Variable latestPanKeyPress in workspace Main variable workspace
The key logger value of the latest pan key press, which will either be a current key press or the value from the last pan key press to be made
[X]
Variable panKeyBeingPressed in workspace Zero page
The direction in which the player is currently panning
[X]
Variable quitGame in workspace Main variable workspace
A flag to record whether the player has pressed function key f1 to quit the game
[X]
Variable sightsAreVisible in workspace Main variable workspace
Controls whether the sights are being shown
[X]
Variable sightsInitialMoves in workspace Main variable workspace
Controls the initial movement of the sights over the first eight calls to the CheckForKeyPresses routine
[X]
Variable spaceKeyDebounce (category: Keyboard)
A variable to flag whether the SPACE key has been pressed, so we can implement debounce
[X]
Variable uTurnStatus in workspace Main variable workspace
A flag to record whether we are performing or have just performed a U-turn