-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Shortcut key and auto check pause for pause game
- Loading branch information
1 parent
6ff8fd7
commit ea98379
Showing
5 changed files
with
98 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { ref, watch } from "vue"; | ||
|
||
import { useQuestionInput } from "~/components/main/QuestionInput/questionInputHelper"; | ||
import { useCourseStore } from "~/store/course"; | ||
import { useGameStore } from "~/store/game"; | ||
|
||
const inactivityTimer = ref<NodeJS.Timeout>(); | ||
const INACTIVITY_TIMEOUT = 5 * 60 * 1000; // 5分钟,单位毫秒 | ||
|
||
const showGamePauseModal = ref(false); | ||
/** | ||
* 用于处理游戏暂停页面(UI)逻辑 | ||
* 暂停 恢复 开启自动检测暂停等 | ||
* @returns | ||
*/ | ||
export function useGamePause() { | ||
const courseStore = useCourseStore(); | ||
const gameStore = useGameStore(); | ||
const { focusInput } = useQuestionInput(); | ||
|
||
function resumeGame() { | ||
showGamePauseModal.value = false; | ||
gameStore.resumeGame(); | ||
focusInput(); | ||
resetInactivityTimer(); | ||
} | ||
|
||
function pauseGame() { | ||
showGamePauseModal.value = true; | ||
gameStore.pauseGame(); | ||
disableAutoPauseCheck(); | ||
} | ||
|
||
function resetInactivityTimer() { | ||
disableAutoPauseCheck(); | ||
inactivityTimer.value = setTimeout(() => { | ||
pauseGame(); | ||
}, INACTIVITY_TIMEOUT); | ||
} | ||
|
||
function enableAutoPauseCheck() { | ||
watch( | ||
() => courseStore.statementIndex, | ||
() => { | ||
resetInactivityTimer(); | ||
}, | ||
{ | ||
immediate: true, | ||
}, | ||
); | ||
} | ||
|
||
function disableAutoPauseCheck() { | ||
if (inactivityTimer.value) { | ||
clearTimeout(inactivityTimer.value); | ||
} | ||
} | ||
|
||
return { | ||
showGamePauseModal, | ||
resumeGame, | ||
pauseGame, | ||
enableAutoPauseCheck, | ||
disableAutoPauseCheck, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters