update: lock player movement during repair
This commit is contained in:
@@ -24,6 +24,8 @@ import {
|
|||||||
PLAYER_XZ_DAMPING_FACTOR,
|
PLAYER_XZ_DAMPING_FACTOR,
|
||||||
} from "@/data/player/playerConfig";
|
} from "@/data/player/playerConfig";
|
||||||
import { InteractionManager } from "@/managers/InteractionManager";
|
import { InteractionManager } from "@/managers/InteractionManager";
|
||||||
|
import { useGameStore } from "@/managers/stores/useGameStore";
|
||||||
|
import type { MissionStep } from "@/types/gameplay/repairMission";
|
||||||
import type { Vector3Tuple } from "@/types/three/three";
|
import type { Vector3Tuple } from "@/types/three/three";
|
||||||
|
|
||||||
type Keys = {
|
type Keys = {
|
||||||
@@ -73,11 +75,39 @@ function setMovementKey(keys: Keys, key: string, pressed: boolean): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isRepairMovementLocked(step: MissionStep): boolean {
|
||||||
|
return (
|
||||||
|
step === "inspected" ||
|
||||||
|
step === "fragmented" ||
|
||||||
|
step === "scanning" ||
|
||||||
|
step === "repairing" ||
|
||||||
|
step === "reassembling"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function useRepairMovementLocked(): boolean {
|
||||||
|
return useGameStore((state) => {
|
||||||
|
switch (state.mainState) {
|
||||||
|
case "bike":
|
||||||
|
return isRepairMovementLocked(state.bike.currentStep);
|
||||||
|
case "pylone":
|
||||||
|
return isRepairMovementLocked(state.pylone.currentStep);
|
||||||
|
case "ferme":
|
||||||
|
return isRepairMovementLocked(state.ferme.currentStep);
|
||||||
|
case "intro":
|
||||||
|
case "outro":
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function PlayerController({
|
export function PlayerController({
|
||||||
octree,
|
octree,
|
||||||
spawnPosition,
|
spawnPosition,
|
||||||
}: PlayerControllerProps): null {
|
}: PlayerControllerProps): null {
|
||||||
const camera = useThree((state) => state.camera);
|
const camera = useThree((state) => state.camera);
|
||||||
|
const movementLocked = useRepairMovementLocked();
|
||||||
|
const movementLockedRef = useRef(movementLocked);
|
||||||
const keys = useRef<Keys>({ ...DEFAULT_KEYS });
|
const keys = useRef<Keys>({ ...DEFAULT_KEYS });
|
||||||
const velocity = useRef(new THREE.Vector3());
|
const velocity = useRef(new THREE.Vector3());
|
||||||
const onFloor = useRef(false);
|
const onFloor = useRef(false);
|
||||||
@@ -104,16 +134,36 @@ export function PlayerController({
|
|||||||
camera.position.copy(capsule.current.end);
|
camera.position.copy(capsule.current.end);
|
||||||
}, [camera, spawnPosition]);
|
}, [camera, spawnPosition]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
movementLockedRef.current = movementLocked;
|
||||||
|
|
||||||
|
if (!movementLocked) return;
|
||||||
|
|
||||||
|
keys.current = { ...DEFAULT_KEYS };
|
||||||
|
wantsJump.current = false;
|
||||||
|
velocity.current.setX(0);
|
||||||
|
velocity.current.setZ(0);
|
||||||
|
}, [movementLocked]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interaction = InteractionManager.getInstance();
|
const interaction = InteractionManager.getInstance();
|
||||||
|
|
||||||
const handleKeyDown = (event: KeyboardEvent): void => {
|
const handleKeyDown = (event: KeyboardEvent): void => {
|
||||||
if (setMovementKey(keys.current, event.key, true)) {
|
if (setMovementKey(keys.current, event.key, true)) {
|
||||||
|
if (movementLockedRef.current) {
|
||||||
|
keys.current = { ...DEFAULT_KEYS };
|
||||||
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === JUMP_KEY) {
|
if (event.key === JUMP_KEY) {
|
||||||
|
if (movementLockedRef.current) {
|
||||||
|
wantsJump.current = false;
|
||||||
|
event.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
wantsJump.current = true;
|
wantsJump.current = true;
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
@@ -172,10 +222,12 @@ export function PlayerController({
|
|||||||
}
|
}
|
||||||
|
|
||||||
_wishDir.set(0, 0, 0);
|
_wishDir.set(0, 0, 0);
|
||||||
|
if (!movementLocked) {
|
||||||
if (keys.current.forward) _wishDir.add(_forward);
|
if (keys.current.forward) _wishDir.add(_forward);
|
||||||
if (keys.current.backward) _wishDir.sub(_forward);
|
if (keys.current.backward) _wishDir.sub(_forward);
|
||||||
if (keys.current.left) _wishDir.sub(_right);
|
if (keys.current.left) _wishDir.sub(_right);
|
||||||
if (keys.current.right) _wishDir.add(_right);
|
if (keys.current.right) _wishDir.add(_right);
|
||||||
|
}
|
||||||
if (_wishDir.lengthSq() > 0) _wishDir.normalize();
|
if (_wishDir.lengthSq() > 0) _wishDir.normalize();
|
||||||
|
|
||||||
const accel = onFloor.current
|
const accel = onFloor.current
|
||||||
|
|||||||
Reference in New Issue
Block a user