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

feat(game-screen): wip add turbo funcionality #16

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all 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
65 changes: 53 additions & 12 deletions src/pages/GameScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,26 @@ const GameScreen = () => {
const [currentObstaclePosition, setCurrentObstaclePosition] = useState(
getRandomPosition,
);
const timer = useTimer();
const [newObstacleFlag, setNewObstacleFlag] = useState(true);
const [obstacleTimespan, setObstacleTimespan] = useState(1500);
const [isTurboActive, setTurboActive] = useState(false);
const obstacleTimer = useTimer();
const turboTimer = useTimer();
const firstRender = useRef(true);

const shouldHandleControls =
countdownValue === -1 && !isPaused && !hasCrashed;
const shouldHandleUI = countdownValue === -1 && !hasCrashed;
const obstacleTimespan = 1500;
const didCrash =
countdownValue === -1 &&
!firstRender.current &&
timer.time >= obstacleTimespan - 200 &&
obstacleTimer.time >= obstacleTimespan - 200 &&
currentPosition === currentObstaclePosition;

const togglePaused = () => {
if (shouldHandleUI) {
isPaused ? timer.resume() : timer.pause();
isPaused ? obstacleTimer.resume() : obstacleTimer.pause();
isPaused ? turboTimer.resume() : turboTimer.pause();
setPaused(!isPaused);
}
};
Expand All @@ -106,6 +109,13 @@ const GameScreen = () => {
}
};

const turbo = () => {
if (turboTimer.time >= 5000) {
setTurboActive(true);
turboTimer.pause();
}
};

const handleTap = (position: Position) => {
return () => {
if (shouldHandleControls) {
Expand All @@ -117,10 +127,12 @@ const GameScreen = () => {
const reset = () => {
setCrashed(false);
setCurrentPosition(Position.Middle);
timer.reset();
setCountdownValue(3);
firstRender.current = true;
timer.resume();
obstacleTimer.reset();
obstacleTimer.resume();
turboTimer.reset();
turboTimer.resume();
};

const handleKeyboard = (event: React.KeyboardEvent): void => {
Expand Down Expand Up @@ -149,6 +161,12 @@ const GameScreen = () => {
moveRight();
break;
}
case 'W':
case 'w':
case 'ArrowUp': {
turbo();
break;
}
}
}

Expand All @@ -163,27 +181,50 @@ const GameScreen = () => {
};

useEventListener('keydown', handleKeyboard);

useEffect(() => {
if (firstRender.current) {
turboTimer.reset();
turboTimer.resume();
}
}, [firstRender, turboTimer]);

useEffect(() => {
if (countdownValue >= 0) {
setTimeout(() => setCountdownValue(countdownValue - 1), 1000);
setTimeout(() => setCountdownValue(prev => prev - 1), 1000);
}
}, [countdownValue]);

useEffect(() => {
if (isTurboActive) {
setObstacleTimespan(750);
setTimeout(() => {
setObstacleTimespan(1500);
setTurboActive(false);
turboTimer.reset();
turboTimer.resume();
}, 3000);
}
}, [isTurboActive, obstacleTimespan, turboTimer]);

useEffect(() => {
if (didCrash) {
setCrashed(true);
timer.pause();
obstacleTimer.pause();
turboTimer.pause();
}
}, [didCrash, obstacleTimer, turboTimer]);

if (countdownValue === -1 && timer.time >= obstacleTimespan) {
useEffect(() => {
if (countdownValue === -1 && obstacleTimer.time >= obstacleTimespan) {
if (firstRender.current) firstRender.current = false;
setCurrentObstaclePosition(getRandomPosition());
setNewObstacleFlag(!newObstacleFlag);
timer.reset();
obstacleTimer.reset();
}
}, [
timer,
didCrash,
obstacleTimer,
obstacleTimespan,
countdownValue,
newObstacleFlag,
currentObstaclePosition,
Expand Down