Files
ox_lib/web/src/features/skillcheck/indicator.tsx

77 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-10-13 14:00:34 +02:00
import { useEffect, useRef, useState } from 'react';
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { useKeyPress } from '../../hooks/useKeyPress';
import { fetchNui } from '../../utils/fetchNui';
import skillcheck, { SkillCheckProps } from './index';
import { useInterval } from '@chakra-ui/react';
import { Simulate } from 'react-dom/test-utils';
import keyPress = Simulate.keyPress;
2022-10-13 14:00:34 +02:00
interface Props {
angle: number;
offset: number;
multiplier: number;
skillCheck: SkillCheckProps;
handleComplete: (success: boolean) => void;
2022-10-13 14:00:34 +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);
const [gameState, setGameState] = useState(false);
2022-10-13 14:00:34 +02:00
const isKeyPressed = useKeyPress('e');
useInterval(
() => {
setIndicatorAngle((prevState) => {
return (prevState += multiplier);
});
},
gameState ? 1 : null
);
2022-10-13 14:00:34 +02:00
useEffect(() => {
setIndicatorAngle(-90);
setGameState(true);
}, [skillCheck]);
2022-10-13 14:00:34 +02:00
useEffect(() => {
if (indicatorAngle + 90 >= 360) {
// fetchNui('skillCheckOver', { success: false });
setGameState(false);
handleComplete(false);
2022-10-13 14:00:34 +02:00
}
}, [indicatorAngle]);
useEffect(() => {
if (!isKeyPressed) return;
//
2022-10-13 14:00:34 +02:00
setGameState(false);
if (isKeyPressed) {
2022-10-13 14:00:34 +02:00
if (indicatorAngle < angle || indicatorAngle > angle + (315 - offset)) {
// fetchNui('skillCheckOver', { success: false });
handleComplete(false);
2022-10-13 14:00:34 +02:00
} else {
// fetchNui('skillCheckOver', { success: true });
handleComplete(true);
2022-10-13 14:00:34 +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;