fix: archi player
This commit is contained in:
+2
-2
@@ -5,7 +5,7 @@ import { DebugHelpers } from "@/utils/debug/scene/DebugHelpers";
|
|||||||
import { Environment } from "@/world/Environment";
|
import { Environment } from "@/world/Environment";
|
||||||
import { Lighting } from "@/world/Lighting";
|
import { Lighting } from "@/world/Lighting";
|
||||||
import { Map } from "@/world/Map";
|
import { Map } from "@/world/Map";
|
||||||
import { FPSController } from "@/world/player/FPSController";
|
import { PlayerComponent } from "@/world/player/PlayerComponent";
|
||||||
|
|
||||||
export function World(): React.JSX.Element {
|
export function World(): React.JSX.Element {
|
||||||
const cameraMode = useCameraMode();
|
const cameraMode = useCameraMode();
|
||||||
@@ -15,7 +15,7 @@ export function World(): React.JSX.Element {
|
|||||||
<Environment />
|
<Environment />
|
||||||
<Lighting />
|
<Lighting />
|
||||||
<DebugHelpers />
|
<DebugHelpers />
|
||||||
{cameraMode === "debug" ? <DebugCameraControls /> : <FPSController />}
|
{cameraMode === "debug" ? <DebugCameraControls /> : <PlayerComponent />}
|
||||||
<Suspense fallback={null}>
|
<Suspense fallback={null}>
|
||||||
<Map />
|
<Map />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { useEffect } from "react";
|
||||||
|
import { PointerLockControls } from "@react-three/drei";
|
||||||
|
import { useThree } from "@react-three/fiber";
|
||||||
|
import * as THREE from "three";
|
||||||
|
|
||||||
|
export const PLAYER_EYE_HEIGHT = 1.75;
|
||||||
|
|
||||||
|
const PLAYER_SPAWN_POSITION = new THREE.Vector3(0, PLAYER_EYE_HEIGHT, 6);
|
||||||
|
const PLAYER_LOOK_AT = new THREE.Vector3(0, PLAYER_EYE_HEIGHT, 0);
|
||||||
|
|
||||||
|
export function PlayerCamera(): React.JSX.Element {
|
||||||
|
const camera = useThree((state) => state.camera);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
camera.position.copy(PLAYER_SPAWN_POSITION);
|
||||||
|
camera.lookAt(PLAYER_LOOK_AT);
|
||||||
|
camera.updateProjectionMatrix();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.exitPointerLock?.();
|
||||||
|
};
|
||||||
|
}, [camera]);
|
||||||
|
|
||||||
|
return <PointerLockControls />;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { PlayerCamera } from "@/world/player/PlayerCamera";
|
||||||
|
import { PlayerController } from "@/world/player/PlayerController";
|
||||||
|
|
||||||
|
export function PlayerComponent(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<PlayerCamera />
|
||||||
|
<PlayerController />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
import { PointerLockControls } from "@react-three/drei";
|
|
||||||
import { useFrame, useThree } from "@react-three/fiber";
|
import { useFrame, useThree } from "@react-three/fiber";
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
|
import { PLAYER_EYE_HEIGHT } from "@/world/player/PlayerCamera";
|
||||||
|
|
||||||
const PLAYER_EYE_HEIGHT = 1.75;
|
const JUMP_HEIGHT = 1;
|
||||||
const PLAYER_SPAWN_POSITION = new THREE.Vector3(0, PLAYER_EYE_HEIGHT, 6);
|
const GRAVITY = 18;
|
||||||
const PLAYER_LOOK_AT = new THREE.Vector3(0, PLAYER_EYE_HEIGHT, 0);
|
const JUMP_VELOCITY = Math.sqrt(2 * GRAVITY * JUMP_HEIGHT);
|
||||||
const MOVE_SPEED = 5;
|
const MOVE_SPEED = 5;
|
||||||
|
|
||||||
type PlayerKeys = {
|
type PlayerKeys = {
|
||||||
@@ -22,47 +22,43 @@ const DEFAULT_KEYS: PlayerKeys = {
|
|||||||
right: false,
|
right: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function FPSController(): React.JSX.Element {
|
export function PlayerController(): null {
|
||||||
const camera = useThree((state) => state.camera);
|
const camera = useThree((state) => state.camera);
|
||||||
const keys = useRef<PlayerKeys>({ ...DEFAULT_KEYS });
|
const keys = useRef<PlayerKeys>({ ...DEFAULT_KEYS });
|
||||||
const interact = useRef<() => void>(() => {});
|
const interact = useRef<() => void>(() => {});
|
||||||
|
const verticalVelocity = useRef(0);
|
||||||
const forward = useRef(new THREE.Vector3());
|
const forward = useRef(new THREE.Vector3());
|
||||||
const right = useRef(new THREE.Vector3());
|
const right = useRef(new THREE.Vector3());
|
||||||
const movement = useRef(new THREE.Vector3());
|
const movement = useRef(new THREE.Vector3());
|
||||||
const up = useRef(new THREE.Vector3(0, 1, 0));
|
const up = useRef(new THREE.Vector3(0, 1, 0));
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
camera.position.copy(PLAYER_SPAWN_POSITION);
|
|
||||||
camera.lookAt(PLAYER_LOOK_AT);
|
|
||||||
camera.updateProjectionMatrix();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
document.exitPointerLock?.();
|
|
||||||
};
|
|
||||||
}, [camera]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyChange =
|
const handleKeyChange =
|
||||||
(pressed: boolean) =>
|
(pressed: boolean) =>
|
||||||
(event: KeyboardEvent): void => {
|
(event: KeyboardEvent): void => {
|
||||||
switch (event.code) {
|
switch (event.key.toLowerCase()) {
|
||||||
case "KeyZ":
|
case "z":
|
||||||
keys.current.forward = pressed;
|
keys.current.forward = pressed;
|
||||||
break;
|
break;
|
||||||
case "KeyS":
|
case "s":
|
||||||
keys.current.backward = pressed;
|
keys.current.backward = pressed;
|
||||||
break;
|
break;
|
||||||
case "KeyQ":
|
case "q":
|
||||||
keys.current.left = pressed;
|
keys.current.left = pressed;
|
||||||
break;
|
break;
|
||||||
case "KeyD":
|
case "d":
|
||||||
keys.current.right = pressed;
|
keys.current.right = pressed;
|
||||||
break;
|
break;
|
||||||
case "KeyE":
|
case "e":
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
interact.current();
|
interact.current();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case " ":
|
||||||
|
if (pressed && camera.position.y <= PLAYER_EYE_HEIGHT) {
|
||||||
|
verticalVelocity.current = JUMP_VELOCITY;
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -81,7 +77,7 @@ export function FPSController(): React.JSX.Element {
|
|||||||
window.removeEventListener("keyup", handleKeyUp);
|
window.removeEventListener("keyup", handleKeyUp);
|
||||||
keys.current = { ...DEFAULT_KEYS };
|
keys.current = { ...DEFAULT_KEYS };
|
||||||
};
|
};
|
||||||
}, []);
|
}, [camera]);
|
||||||
|
|
||||||
useFrame((_, delta) => {
|
useFrame((_, delta) => {
|
||||||
const currentForward = forward.current;
|
const currentForward = forward.current;
|
||||||
@@ -119,7 +115,13 @@ export function FPSController(): React.JSX.Element {
|
|||||||
camera.position.add(currentMovement);
|
camera.position.add(currentMovement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verticalVelocity.current -= GRAVITY * delta;
|
||||||
|
|
||||||
|
const nextY = camera.position.y + verticalVelocity.current * delta;
|
||||||
|
camera.position.set(camera.position.x, nextY, camera.position.z);
|
||||||
|
|
||||||
if (camera.position.y < PLAYER_EYE_HEIGHT) {
|
if (camera.position.y < PLAYER_EYE_HEIGHT) {
|
||||||
|
verticalVelocity.current = 0;
|
||||||
camera.position.set(
|
camera.position.set(
|
||||||
camera.position.x,
|
camera.position.x,
|
||||||
PLAYER_EYE_HEIGHT,
|
PLAYER_EYE_HEIGHT,
|
||||||
@@ -128,5 +130,5 @@ export function FPSController(): React.JSX.Element {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return <PointerLockControls />;
|
return null;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user