clean: removed player useState

This commit is contained in:
Florian Sylvain
2024-03-14 19:26:24 +01:00
parent 461fb38ec8
commit bbc4323560
+16 -14
View File
@@ -44,7 +44,6 @@ export function Player(props: {
const [pivotX, setPivotX] = useState(0); const [pivotX, setPivotX] = useState(0);
const [pivotY, setPivotY] = useState(0); const [pivotY, setPivotY] = useState(0);
const [player, setPlayer] = useState<Vector3>(new Vector3(0, 0, 0));
const [mouseRef, setMouseRef] = useState(new Vector2()); const [mouseRef, setMouseRef] = useState(new Vector2());
const lightRef = useRef<DirectionalLight>(null); const lightRef = useRef<DirectionalLight>(null);
@@ -66,6 +65,8 @@ export function Player(props: {
useFrame((_, delta) => { useFrame((_, delta) => {
const move = new Vector3(); const move = new Vector3();
const player = model.scene.position;
// TODO Replace getKeys with the subscription system // TODO Replace getKeys with the subscription system
if (getKeys().moveForward) move.z += 1; if (getKeys().moveForward) move.z += 1;
if (getKeys().moveBackward) move.z -= 1; if (getKeys().moveBackward) move.z -= 1;
@@ -87,17 +88,9 @@ export function Player(props: {
camera.lookAt(adujstedPos); camera.lookAt(adujstedPos);
if (Object.values(getKeys()).some((key) => key)) { if (Object.values(getKeys()).some((key) => key)) {
setPlayer(player.clone().add(move)); player.add(move);
setIsWalking(true);
const targetRotation = new Quaternion(); const targetRotation = new Quaternion();
targetRotation.setFromEuler(
new Euler(
0,
pivotX + degToRad((Math.atan2(move.x, move.z) * 180) / Math.PI),
0
)
);
const cameraRotation = new Euler().setFromQuaternion( const cameraRotation = new Euler().setFromQuaternion(
camera.quaternion, camera.quaternion,
@@ -105,12 +98,14 @@ export function Player(props: {
); );
targetRotation.multiplyQuaternions( targetRotation.multiplyQuaternions(
targetRotation, targetRotation,
new Quaternion().setFromEuler(new Euler(0, -cameraRotation.y, 0)) new Quaternion().setFromEuler(
new Euler(0, cameraRotation.y - Math.PI, 0)
)
); );
model.scene.quaternion.slerp(targetRotation, 0.2); model.scene.quaternion.slerp(targetRotation, 0.2);
} else { } else {
setIsWalking(false); //a
} }
}); });
@@ -153,12 +148,19 @@ export function Player(props: {
return ( return (
<> <>
<primitive object={model.scene} position={player}></primitive> <primitive
object={model.scene}
position={model.scene.position}
></primitive>
<directionalLight <directionalLight
ref={lightRef} ref={lightRef}
target={model.scene} target={model.scene}
castShadow castShadow
position={[player.x + 100, 100, player.z + 100]} position={[
model.scene.position.x + 100,
100,
model.scene.position.z + 100,
]}
shadow-mapSize={[2048, 2048]} shadow-mapSize={[2048, 2048]}
intensity={6} intensity={6}
> >