2024-11-10 21:54:20 -06:00
|
|
|
import {
|
|
|
|
|
I_HAT,
|
|
|
|
|
J_HAT,
|
|
|
|
|
K_HAT,
|
|
|
|
|
TrackDefinition,
|
|
|
|
|
TurretDefinition,
|
|
|
|
|
} from '@blitzkit/core';
|
2024-09-21 20:26:58 -05:00
|
|
|
import { type RefObject, useEffect } from 'react';
|
2024-07-24 20:35:01 -05:00
|
|
|
import { Euler, Group, Vector3 } from 'three';
|
2024-09-22 21:12:18 -05:00
|
|
|
import { degToRad } from 'three/src/math/MathUtils.js';
|
2024-09-07 20:40:06 -05:00
|
|
|
import { correctZYTuple } from '../core/blitz/correctZYTuple';
|
2024-11-10 21:54:20 -06:00
|
|
|
import { modelTransformEvent } from '../core/blitzkit/modelTransform';
|
|
|
|
|
import { Duel } from '../stores/duel';
|
2024-07-30 13:24:55 -05:00
|
|
|
import { useTankModelDefinition } from './useTankModelDefinition';
|
2024-07-24 20:35:01 -05:00
|
|
|
|
|
|
|
|
export function useTankTransform(
|
2024-11-10 21:54:20 -06:00
|
|
|
track: TrackDefinition,
|
|
|
|
|
turret: TurretDefinition,
|
2024-07-24 20:35:01 -05:00
|
|
|
turretContainer: RefObject<Group>,
|
|
|
|
|
gunContainer: RefObject<Group>,
|
|
|
|
|
) {
|
2024-07-30 13:24:55 -05:00
|
|
|
const tankModelDefinition = useTankModelDefinition();
|
2024-11-10 21:54:20 -06:00
|
|
|
const duelStore = Duel.useStore();
|
2024-07-24 20:35:01 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-11-10 21:54:20 -06:00
|
|
|
const trackModelDefinition = tankModelDefinition.tracks[track.id];
|
|
|
|
|
const turretModelDefinition = tankModelDefinition.turrets[turret.id];
|
2024-07-24 20:35:01 -05:00
|
|
|
const hullOrigin = correctZYTuple(trackModelDefinition.origin);
|
2024-10-07 18:45:11 -05:00
|
|
|
const turretOrigin = correctZYTuple(tankModelDefinition.turret_origin);
|
|
|
|
|
const gunOrigin = correctZYTuple(turretModelDefinition.gun_origin);
|
2024-07-24 20:35:01 -05:00
|
|
|
const turretPosition = new Vector3();
|
|
|
|
|
const turretRotation = new Euler();
|
|
|
|
|
const gunPosition = new Vector3();
|
|
|
|
|
const gunRotation = new Euler();
|
|
|
|
|
|
2024-11-10 21:54:20 -06:00
|
|
|
function handleModelTransform() {
|
|
|
|
|
const { yaw, pitch } = duelStore.getState().protagonist;
|
2024-07-24 20:35:01 -05:00
|
|
|
gunPosition
|
|
|
|
|
.set(0, 0, 0)
|
|
|
|
|
.sub(hullOrigin)
|
|
|
|
|
.sub(turretOrigin)
|
|
|
|
|
.sub(gunOrigin)
|
|
|
|
|
.applyAxisAngle(I_HAT, pitch)
|
|
|
|
|
.add(gunOrigin)
|
|
|
|
|
.add(turretOrigin)
|
|
|
|
|
.add(hullOrigin);
|
|
|
|
|
gunRotation.set(pitch, 0, 0);
|
|
|
|
|
gunContainer.current?.position.copy(gunPosition);
|
|
|
|
|
gunContainer.current?.rotation.copy(gunRotation);
|
|
|
|
|
|
|
|
|
|
if (yaw === undefined) return;
|
|
|
|
|
|
|
|
|
|
turretPosition
|
|
|
|
|
.set(0, 0, 0)
|
|
|
|
|
.sub(hullOrigin)
|
|
|
|
|
.sub(turretOrigin)
|
|
|
|
|
.applyAxisAngle(new Vector3(0, 0, 1), yaw);
|
|
|
|
|
turretRotation.set(0, 0, yaw);
|
|
|
|
|
|
2024-10-07 18:45:11 -05:00
|
|
|
if (tankModelDefinition.initial_turret_rotation) {
|
2024-07-24 20:35:01 -05:00
|
|
|
const initialPitch = -degToRad(
|
2024-10-07 18:45:11 -05:00
|
|
|
tankModelDefinition.initial_turret_rotation.pitch,
|
2024-10-05 20:55:44 -05:00
|
|
|
);
|
|
|
|
|
const initialYaw = -degToRad(
|
2024-10-07 18:45:11 -05:00
|
|
|
tankModelDefinition.initial_turret_rotation.yaw,
|
2024-10-05 20:55:44 -05:00
|
|
|
);
|
|
|
|
|
const initialRoll = -degToRad(
|
2024-10-07 18:45:11 -05:00
|
|
|
tankModelDefinition.initial_turret_rotation.roll,
|
2024-07-24 20:35:01 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
turretPosition
|
|
|
|
|
.applyAxisAngle(I_HAT, initialPitch)
|
|
|
|
|
.applyAxisAngle(J_HAT, initialRoll)
|
|
|
|
|
.applyAxisAngle(K_HAT, initialYaw);
|
|
|
|
|
turretRotation.x += initialPitch;
|
|
|
|
|
turretRotation.y += initialRoll;
|
|
|
|
|
turretRotation.z += initialYaw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
turretPosition.add(turretOrigin).add(hullOrigin);
|
|
|
|
|
turretContainer.current?.position.copy(turretPosition);
|
|
|
|
|
turretContainer.current?.rotation.copy(turretRotation);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-10 21:54:20 -06:00
|
|
|
handleModelTransform();
|
|
|
|
|
|
2024-07-24 20:35:01 -05:00
|
|
|
modelTransformEvent.on(handleModelTransform);
|
|
|
|
|
|
2024-11-10 21:54:20 -06:00
|
|
|
const unsubscribes = [
|
|
|
|
|
duelStore.subscribe(
|
|
|
|
|
(state) => state.protagonist.pitch,
|
|
|
|
|
handleModelTransform,
|
|
|
|
|
),
|
|
|
|
|
duelStore.subscribe(
|
|
|
|
|
(state) => state.protagonist.yaw,
|
|
|
|
|
handleModelTransform,
|
|
|
|
|
),
|
|
|
|
|
() => modelTransformEvent.off(handleModelTransform),
|
|
|
|
|
];
|
|
|
|
|
|
2024-07-24 20:35:01 -05:00
|
|
|
return () => {
|
2024-11-10 21:54:20 -06:00
|
|
|
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
2024-07-24 20:35:01 -05:00
|
|
|
};
|
2024-11-10 21:54:20 -06:00
|
|
|
}, [track, turret]);
|
2024-07-24 20:35:01 -05:00
|
|
|
}
|