animation, gathering, deposit, ui
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import {
|
||||
EasyEvent,
|
||||
Physics_internal,
|
||||
UpdateController,
|
||||
} from "@24tools/playable_template";
|
||||
import { Object3D } from "three";
|
||||
import { Body, Box } from "cannon-es";
|
||||
import { PhysicsBody, PhysicsLayer } from "../PhysicsC";
|
||||
import { Box3, Object3D, Vector3 } from "three";
|
||||
import { Body } from "cannon-es";
|
||||
|
||||
const PLAYER_RADIUS = 0.3;
|
||||
|
||||
@@ -18,15 +16,15 @@ export type TriggerEventPayload = {
|
||||
type TriggerRecord = {
|
||||
lootableObject: Object3D;
|
||||
triggerObject: Object3D;
|
||||
center: Vector3;
|
||||
radius: number;
|
||||
position: Body["position"];
|
||||
physicsBody: PhysicsBody;
|
||||
};
|
||||
|
||||
export class PhysicsTriggerC {
|
||||
private static inited = false;
|
||||
private static triggers: TriggerRecord[] = [];
|
||||
private static activeTriggers = new Set<TriggerRecord>();
|
||||
private static playerBody: Body | null = null;
|
||||
|
||||
static onTriggerEnter = new EasyEvent<TriggerEventPayload>();
|
||||
static onTriggerExit = new EasyEvent<TriggerEventPayload>();
|
||||
@@ -38,50 +36,62 @@ export class PhysicsTriggerC {
|
||||
UpdateController.Instance.onUpdate.addDelegate(() => this.update());
|
||||
}
|
||||
|
||||
static setPlayerBody(body: Body) {
|
||||
this.playerBody = body;
|
||||
}
|
||||
|
||||
static register(triggerObject: Object3D, lootableObject: Object3D) {
|
||||
const physicsBody = new PhysicsBody(
|
||||
triggerObject,
|
||||
true,
|
||||
0,
|
||||
PhysicsLayer.Trigger,
|
||||
PhysicsLayer.Player,
|
||||
);
|
||||
triggerObject.updateWorldMatrix(true, true);
|
||||
|
||||
const body = physicsBody.getPhysicsBody();
|
||||
body.allowSleep = false;
|
||||
|
||||
const shape = body.shapes[0] as Box;
|
||||
const halfExtents = shape.halfExtents;
|
||||
const center = new Vector3();
|
||||
const size = new Vector3();
|
||||
const bounds = new Box3().setFromObject(triggerObject);
|
||||
bounds.getCenter(center);
|
||||
bounds.getSize(size);
|
||||
|
||||
this.triggers.push({
|
||||
lootableObject,
|
||||
triggerObject,
|
||||
radius: Math.max(halfExtents.x, halfExtents.y, halfExtents.z),
|
||||
position: body.position,
|
||||
physicsBody,
|
||||
center,
|
||||
radius: Math.max(size.x, size.y, size.z) / 2,
|
||||
});
|
||||
}
|
||||
|
||||
static getActiveLootableObjects(): Object3D[] {
|
||||
const lootables: Object3D[] = [];
|
||||
for (const record of this.activeTriggers) {
|
||||
lootables.push(record.lootableObject);
|
||||
}
|
||||
return lootables;
|
||||
}
|
||||
|
||||
static unregister(lootableObject: Object3D) {
|
||||
const playerBody = this.playerBody;
|
||||
|
||||
this.triggers = this.triggers.filter((record) => {
|
||||
if (record.lootableObject !== lootableObject) return true;
|
||||
|
||||
record.physicsBody.destroy();
|
||||
if (this.activeTriggers.has(record) && playerBody) {
|
||||
this.onTriggerExit.Invoke({
|
||||
lootableObject: record.lootableObject,
|
||||
triggerObject: record.triggerObject,
|
||||
playerBody,
|
||||
});
|
||||
}
|
||||
|
||||
this.activeTriggers.delete(record);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private static update() {
|
||||
const playerBody = Physics_internal.physicsWorld?.bodies.find(
|
||||
(body) => body.collisionFilterGroup === PhysicsLayer.Player,
|
||||
);
|
||||
const playerBody = this.playerBody;
|
||||
if (!playerBody) return;
|
||||
|
||||
for (const trigger of this.triggers) {
|
||||
const dx = playerBody.position.x - trigger.position.x;
|
||||
const dy = playerBody.position.y - trigger.position.y;
|
||||
const dz = playerBody.position.z - trigger.position.z;
|
||||
const dx = playerBody.position.x - trigger.center.x;
|
||||
const dy = playerBody.position.y - trigger.center.y;
|
||||
const dz = playerBody.position.z - trigger.center.z;
|
||||
const distanceSq = dx * dx + dy * dy + dz * dz;
|
||||
const isInside = distanceSq <= (trigger.radius + PLAYER_RADIUS) ** 2;
|
||||
const wasInside = this.activeTriggers.has(trigger);
|
||||
@@ -97,11 +107,9 @@ export class PhysicsTriggerC {
|
||||
if (isInside) {
|
||||
this.activeTriggers.add(trigger);
|
||||
this.onTriggerEnter.Invoke(payload);
|
||||
console.log("[Gather Enter]", trigger.lootableObject.name);
|
||||
} else {
|
||||
this.activeTriggers.delete(trigger);
|
||||
this.onTriggerExit.Invoke(payload);
|
||||
console.log("[Gather Exit]", trigger.lootableObject.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user