2022-10-13 14:00:34 +02:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { useKeyPress } from '../../hooks/useKeyPress';
|
2022-10-18 18:01:53 +02:00
|
|
|
import { SkillCheckProps } from './index';
|
2022-10-18 17:59:02 +02:00
|
|
|
import { useInterval } from '@chakra-ui/react';
|
2022-10-13 14:00:34 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
angle: number;
|
|
|
|
|
offset: number;
|
|
|
|
|
multiplier: number;
|
2022-10-18 17:59:02 +02:00
|
|
|
skillCheck: SkillCheckProps;
|
|
|
|
|
handleComplete: (success: boolean) => void;
|
2022-10-13 14:00:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:59:02 +02:00
|
|
|
const Indicator: React.FC<Props> = ({ angle, offset, multiplier, handleComplete, skillCheck }) => {
|
2022-10-13 14:00:34 +02:00
|
|
|
const [indicatorAngle, setIndicatorAngle] = useState(-90);
|
2022-10-18 17:59:02 +02:00
|
|
|
const [gameState, setGameState] = useState(false);
|
2022-10-13 14:00:34 +02:00
|
|
|
const isKeyPressed = useKeyPress('e');
|
|
|
|
|
|
2022-10-18 17:59:02 +02:00
|
|
|
useInterval(
|
|
|
|
|
() => {
|
|
|
|
|
setIndicatorAngle((prevState) => {
|
|
|
|
|
return (prevState += multiplier);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
gameState ? 1 : null
|
|
|
|
|
);
|
2022-10-13 14:00:34 +02:00
|
|
|
|
2022-10-18 17:59:02 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
setIndicatorAngle(-90);
|
|
|
|
|
setGameState(true);
|
|
|
|
|
}, [skillCheck]);
|
2022-10-13 14:00:34 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-10-18 17:59:02 +02:00
|
|
|
if (indicatorAngle + 90 >= 360) {
|
|
|
|
|
setGameState(false);
|
|
|
|
|
handleComplete(false);
|
2022-10-13 14:00:34 +02:00
|
|
|
}
|
2022-10-18 17:59:02 +02:00
|
|
|
}, [indicatorAngle]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isKeyPressed) return;
|
2022-10-13 14:00:34 +02:00
|
|
|
|
2022-10-18 17:59:02 +02:00
|
|
|
setGameState(false);
|
2022-10-18 18:01:53 +02:00
|
|
|
if (indicatorAngle < angle || indicatorAngle > angle + (315 - offset)) handleComplete(false);
|
|
|
|
|
else handleComplete(true);
|
2022-10-18 17:59:02 +02:00
|
|
|
}, [isKeyPressed]);
|
2022-10-13 14:00:34 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<circle
|
|
|
|
|
r={50}
|
|
|
|
|
cx={250}
|
|
|
|
|
cy={250}
|
|
|
|
|
fill="transparent"
|
|
|
|
|
stroke="red"
|
|
|
|
|
strokeWidth={15}
|
|
|
|
|
strokeDasharray={315}
|
|
|
|
|
strokeDashoffset={312}
|
|
|
|
|
transform={`rotate(${indicatorAngle}, 250, 250)`}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Indicator;
|