Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shift plus moving moves character as far as it can #1071

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions components/level/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,28 +335,28 @@ export default function Game({
onNext();
}

return;
return false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to return a boolean here, or if we do it should be more clear what the boolean means

}

if (code === 'KeyP') {
if (onPrev) {
onPrev();
}

return;
return false;
}

// check if code is the shift key
if (code.startsWith('Shift')) {
shiftKeyDown.current = true;

return;
return false;
}

// check if code starts with the words Digit
if (code.startsWith('Digit') || code === 'KeyB') {
if (disableCheckpoints) {
return;
return false;
}

if (!pro) {
Expand All @@ -369,7 +369,7 @@ export default function Game({
}
);

return;
return false;
}

if (code.startsWith('Digit')) {
Expand All @@ -384,7 +384,7 @@ export default function Game({
loadCheckpoint(BEST_CHECKPOINT_INDEX);
}

return;
return false;
}

setGameState(prevGameState => {
Expand Down Expand Up @@ -462,7 +462,9 @@ export default function Game({
return prevGameState;
}

if (!makeMove(newGameState, direction, !disableAutoUndo)) {
const move = makeMove(newGameState, direction, !disableAutoUndo);

if (!move) {
return prevGameState;
}

Expand All @@ -484,6 +486,8 @@ export default function Game({

return onSuccessfulMove(newGameState);
});

return true;
}, [disableAutoUndo, disableCheckpoints, disablePlayAttempts, enableSessionCheckpoint, fetchPlayAttempt, game.displayName, isComplete, level._id, level.data, level.leastMoves, loadCheckpoint, onComplete, onMove, onNext, onPrev, onSolve, pro, saveCheckpoint, saveSessionToSessionStorage, trackStats]);

useEffect(() => {
Expand Down Expand Up @@ -526,7 +530,19 @@ export default function Game({
event.preventDefault();
}

handleKeyDown(code);
let timesToMove = 1;

// if code starts with arrow
if (shiftKeyDown.current && code.startsWith('Arrow')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic should be moved to within the setGameState of handleKeyDown

Need a condition around this section of code:

      if (!makeMove(newGameState, direction, !disableAutoUndo)) {
        return prevGameState;
      }

to check if the shift key is pressed, and if so call makeMove in a loop until it fails

Would need to double check that the makeMove function doesn't mess up the gameState object when the function returns false (since it updates in-place)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was doing this first but then realized that undo may not work as expected. I can try it and see but I think that we wouldn't be storing the intermediate state

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... I wonder what would be best there. Maybe it's nice if a single undo removes all the moves at once? Since the shift + move is kind of like one "move"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it is less confusing if you can undo one at a time and not be in a batch...

// @TODO: this 39 is based on the max width of grid being 40...
// ideally we just keep moving until handleKeyDown fails but based on how the code is written,
// it isn't straightforward how to have it return false if it fails
timesToMove = 39;
}

for (let i = 0; i < timesToMove; i ++) {
handleKeyDown(code);
}
}, [handleKeyDown, preventKeyDownEvent]);

const handleKeyUpEvent = useCallback((event: KeyboardEvent) => {
Expand Down
Loading