diff --git a/leaky-ships/lib/hooks/useGameEvent.ts b/leaky-ships/lib/hooks/useGameEvent.ts index 7948092..31b5fd3 100644 --- a/leaky-ships/lib/hooks/useGameEvent.ts +++ b/leaky-ships/lib/hooks/useGameEvent.ts @@ -47,6 +47,10 @@ function useGameEvent(count: number) { .reduce((prev, curr) => [...prev, ...curr], []) }, [mode]) + const isHit = useCallback((x: number, y: number) => { + return hits.filter(h => h.x === x && h.y === y) + }, [hits]) + const settingTarget = useCallback((isGameTile: boolean, x: number, y: number) => { if (!isGameTile || isHit(x, y).length) return @@ -64,16 +68,12 @@ function useGameEvent(count: number) { } }) - }, [count, hits, targetList]) + }, [count, isHit, targetList]) const isSet = useCallback((x: number, y: number) => { return !!targetList(target).filter(field => x === field.x && y === field.y).length && target.show }, [target, targetList]) - const isHit = useCallback((x: number, y: number) => { - return hits.filter(h => h.x === x && h.y === y) - }, [hits]) - const composeTargetTiles = useCallback((target: Target): PointerProps[] => { const { preview, show } = target const result = targetList(target).map(({ x, y, type, edges }) => { @@ -88,7 +88,7 @@ function useGameEvent(count: number) { } }) return result - }, [hits, isSet, targetList]) + }, [isHit, isSet, targetList]) // handle visibility and position change of targetPreview useEffect(() => { @@ -101,7 +101,7 @@ function useGameEvent(count: number) { setTargetPreview(e => ({ ...e, show: false })) if (!show && mouseCursor.shouldShow && eventReady && appearOK && !isHit(x, y).length && !isSet && !hasLeft) setTargetPreview(e => ({ ...e, show: true })) - }, [targetPreview, mouseCursor.shouldShow, hits, eventReady, appearOK, lastLeftTile, target]) + }, [targetPreview, mouseCursor.shouldShow, isHit, eventReady, appearOK, lastLeftTile, target]) // enable targetPreview event again after 200 ms. useEffect(() => {