201 lines
4.9 KiB
TypeScript
201 lines
4.9 KiB
TypeScript
import {
|
|
AnimationAction,
|
|
AnimationClip,
|
|
AnimationMixer,
|
|
Object3D,
|
|
Skeleton,
|
|
SkinnedMesh,
|
|
Vector3,
|
|
} from 'three';
|
|
import { GameObject } from '../types/game-object';
|
|
import { Ticking } from '../types/ticking';
|
|
import { EditorProperty } from '../decorators/property';
|
|
import { MeshPart } from './mesh.object';
|
|
import { clamp } from 'three/src/math/MathUtils.js';
|
|
import { NameTag } from './nametag.object';
|
|
import { CanvasUtils } from '../canvas/utils';
|
|
|
|
export type HumanoidBodyPart =
|
|
| 'Head'
|
|
| 'Torso'
|
|
| 'ArmLeft'
|
|
| 'ArmRight'
|
|
| 'LegRight'
|
|
| 'LegLeft';
|
|
|
|
export class Humanoid extends GameObject implements Ticking {
|
|
public isTickingObject = true;
|
|
public objectType = 'Humanoid';
|
|
public name = 'Humanoid';
|
|
private ready = false;
|
|
private skeleton!: Skeleton;
|
|
private _health = 100;
|
|
private _maxHealth = 100;
|
|
private _velocity = new Vector3(0, 0, 0);
|
|
private _lookAt = new Vector3(0, 0, 1);
|
|
private _currentLookAt = new Vector3(0, 0, 1);
|
|
private _animState = 0;
|
|
public static bodyPartNames = [
|
|
'Head',
|
|
'Torso',
|
|
'ArmLeft',
|
|
'ArmRight',
|
|
'LegRight',
|
|
'LegLeft',
|
|
] as HumanoidBodyPart[];
|
|
|
|
public static bodyBoneNames = [
|
|
'BHead',
|
|
'BTorso',
|
|
'BArmL',
|
|
'BArmR',
|
|
'BLegR',
|
|
'BLegL',
|
|
] as string[];
|
|
|
|
public static bodyAnimationNames = ['Idle', 'Walk'];
|
|
|
|
public static nameTagBuilder = new CanvasUtils({
|
|
fill: false,
|
|
textBorderColor: '#000',
|
|
foregroundColor: '#fff',
|
|
textShadowBlur: 2,
|
|
textBorderSize: 1,
|
|
});
|
|
|
|
private mixer!: AnimationMixer;
|
|
private idleAction!: AnimationAction;
|
|
private walkAction!: AnimationAction;
|
|
|
|
private nameTag?: NameTag;
|
|
|
|
@EditorProperty({ type: Number })
|
|
get health() {
|
|
return this._health;
|
|
}
|
|
set health(value: number) {
|
|
const health = clamp(Math.floor(value), 0, this.maxHealth);
|
|
if (health === 0) this.die();
|
|
this.health = health;
|
|
}
|
|
|
|
@EditorProperty({ type: Number })
|
|
get maxHealth() {
|
|
return this._maxHealth;
|
|
}
|
|
set maxHealth(value: number) {
|
|
const maxHealth = Math.floor(value);
|
|
if (this.health > maxHealth) {
|
|
this.health = maxHealth;
|
|
}
|
|
this._maxHealth = maxHealth;
|
|
}
|
|
|
|
get alive() {
|
|
return this.health > 0;
|
|
}
|
|
|
|
private get bodyParts() {
|
|
return Humanoid.bodyPartNames.map((key) => this.getBodyPartByName(key));
|
|
}
|
|
|
|
getBodyPartByName(name: string) {
|
|
return this.parent?.getObjectByName(name) as MeshPart;
|
|
}
|
|
|
|
getBoneForBodyPart(part: HumanoidBodyPart) {
|
|
return this.skeleton.getBoneByName(
|
|
Humanoid.bodyBoneNames[Humanoid.bodyPartNames.indexOf(part)]
|
|
);
|
|
}
|
|
|
|
initialize(): void {
|
|
if (!this.parent)
|
|
throw new Error('Cannot initialize Humanoid to empty parent');
|
|
|
|
this.skeleton = (this.bodyParts[0]?.getMesh() as SkinnedMesh).skeleton;
|
|
|
|
if (!this.skeleton) throw new Error('Could not find Skeleton for Humanoid');
|
|
this.skeleton.pose();
|
|
|
|
this.mixer = new AnimationMixer(this.parent);
|
|
this.ready = true;
|
|
|
|
const idleClip = AnimationClip.findByName(this.parent.animations, 'Idle');
|
|
const walkClip = AnimationClip.findByName(this.parent.animations, 'Walk');
|
|
this.idleAction = this.mixer.clipAction(idleClip);
|
|
this.walkAction = this.mixer.clipAction(walkClip);
|
|
this.idleAction.play();
|
|
|
|
this.createNameTag();
|
|
}
|
|
|
|
setVelocity(velocity: Vector3) {
|
|
this._velocity.copy(velocity);
|
|
|
|
this.setWalkAnimationState(!!this._velocity.length() ? 1 : 0);
|
|
}
|
|
|
|
setLook(vector: Vector3) {
|
|
this._lookAt.lerp(vector, 0.15);
|
|
}
|
|
|
|
tick(dt: number): void {
|
|
if (!this.ready) return;
|
|
this.mixer.update(dt);
|
|
this.parent?.position.add(this._velocity.clone().multiplyScalar(dt));
|
|
|
|
this._currentLookAt.copy(this.parent!.position);
|
|
this._currentLookAt.add(this._lookAt);
|
|
this.parent?.lookAt(this._currentLookAt);
|
|
}
|
|
|
|
setWalkAnimationState(index: number) {
|
|
const previousState = this._animState;
|
|
this._animState = index;
|
|
if (previousState === index) return;
|
|
|
|
if (index === 1) {
|
|
this.walkAction.reset().crossFadeFrom(this.idleAction, 0.5, false).play();
|
|
} else {
|
|
this.walkAction.crossFadeTo(this.idleAction.reset(), 0.5, false).play();
|
|
}
|
|
}
|
|
|
|
detach(bodyPart?: HumanoidBodyPart) {
|
|
if (!bodyPart) {
|
|
Humanoid.bodyPartNames.forEach((part) => this.detach(part));
|
|
return;
|
|
}
|
|
|
|
const part = this.getBodyPartByName(bodyPart);
|
|
if (!part) return;
|
|
const partMesh = part.getMesh() as SkinnedMesh;
|
|
|
|
if (partMesh.bindMode === 'detached') return;
|
|
partMesh.bindMode = 'detached';
|
|
partMesh.updateMatrixWorld(true);
|
|
|
|
const bone = this.getBoneForBodyPart(bodyPart);
|
|
if (!bone) return;
|
|
bone.removeFromParent();
|
|
this.skeleton.update();
|
|
}
|
|
|
|
die() {
|
|
if (!this.alive) return;
|
|
this.health = 0;
|
|
this.detach();
|
|
}
|
|
|
|
private createNameTag() {
|
|
if (this.nameTag) {
|
|
this.nameTag.dispose();
|
|
}
|
|
|
|
this.nameTag = NameTag.create(Humanoid.nameTagBuilder, this.parent!.name);
|
|
this.nameTag.position.set(0, 1.5, 0);
|
|
this.add(this.nameTag);
|
|
}
|
|
}
|