3d8566bc-063f-41e8-8934-68c9ac5b8211
This commit is contained in:
@@ -3,10 +3,11 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useGameStore } from "@/store/gameStore";
|
||||
|
||||
/** 100ms tick:产能 + 自动产晶体 + 自动解码 */
|
||||
/** 250ms tick:产能 + 自动产晶体 + 自动解码 + 成就检测 */
|
||||
export function useGameLoop() {
|
||||
const tick = useGameStore((s) => s.tick);
|
||||
const autoDecodeTick = useGameStore((s) => s.autoDecodeTick);
|
||||
const checkAchievements = useGameStore((s) => s.checkAchievements);
|
||||
const init = useGameStore((s) => s.init);
|
||||
const inited = useRef(false);
|
||||
|
||||
@@ -15,22 +16,30 @@ export function useGameLoop() {
|
||||
init();
|
||||
inited.current = true;
|
||||
}
|
||||
let achCounter = 0;
|
||||
const id = setInterval(() => {
|
||||
const now = Date.now();
|
||||
tick(now);
|
||||
autoDecodeTick();
|
||||
// 成就检测每秒执行一次(降低开销)
|
||||
achCounter++;
|
||||
if (achCounter % 4 === 0) {
|
||||
checkAchievements();
|
||||
}
|
||||
}, 250);
|
||||
return () => clearInterval(id);
|
||||
}, [tick, autoDecodeTick, init]);
|
||||
}, [tick, autoDecodeTick, checkAchievements, init]);
|
||||
|
||||
// 页面可见性:切回时补 tick
|
||||
// 页面可见性:切回时补 tick + 立即检测成就
|
||||
useEffect(() => {
|
||||
const onVis = () => {
|
||||
if (document.visibilityState === "visible") {
|
||||
tick(Date.now());
|
||||
checkAchievements();
|
||||
}
|
||||
};
|
||||
document.addEventListener("visibilitychange", onVis);
|
||||
return () => document.removeEventListener("visibilitychange", onVis);
|
||||
}, [tick]);
|
||||
}, [tick, checkAchievements]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user