2022-04-09 11:29:54 +00:00
|
|
|
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
|
|
|
|
import * as THREE from 'three';
|
|
|
|
import modelLoaderInstance from './pony-loader';
|
2022-04-09 14:49:30 +00:00
|
|
|
import { FullStatePacket } from '../../common/types/packet';
|
2022-04-09 11:29:54 +00:00
|
|
|
import { NameTag } from './nametag';
|
|
|
|
import { CanvasUtils } from './canvas-utils';
|
|
|
|
|
|
|
|
const nameTagBuilder = new CanvasUtils();
|
|
|
|
|
|
|
|
export class PonyEntity {
|
|
|
|
public velocity = new THREE.Vector3(0, 0, 0);
|
|
|
|
public angularVelocity = new THREE.Vector3(0, 0, 0);
|
|
|
|
public mixer!: THREE.AnimationMixer;
|
|
|
|
public container!: THREE.Object3D;
|
|
|
|
public model!: THREE.Object3D;
|
|
|
|
public walkAnimationState = 0;
|
|
|
|
public idleAction: THREE.AnimationAction;
|
|
|
|
public walkAction: THREE.AnimationAction;
|
|
|
|
public nameTag?: NameTag;
|
2022-04-09 14:49:30 +00:00
|
|
|
public changes: FullStatePacket = {};
|
2022-04-09 11:29:54 +00:00
|
|
|
|
|
|
|
initialize() {
|
|
|
|
this.model = (SkeletonUtils as any).clone(modelLoaderInstance.ponyModel);
|
|
|
|
this.mixer = new THREE.AnimationMixer(this.model);
|
|
|
|
this.idleAction = this.mixer.clipAction(modelLoaderInstance.animations[0]);
|
|
|
|
this.walkAction = this.mixer.clipAction(modelLoaderInstance.animations[2]);
|
|
|
|
this.idleAction.play();
|
|
|
|
this.container = new THREE.Object3D();
|
|
|
|
this.container.add(this.model);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(dt: number) {
|
2022-04-09 12:57:20 +00:00
|
|
|
this.container.position.add(this.velocity.clone().multiplyScalar(dt));
|
2022-04-09 11:29:54 +00:00
|
|
|
this.container.rotation.setFromVector3(
|
|
|
|
new THREE.Vector3(
|
|
|
|
this.container.rotation.x,
|
|
|
|
this.container.rotation.y,
|
|
|
|
this.container.rotation.z,
|
2022-04-09 12:57:20 +00:00
|
|
|
).add(this.angularVelocity.clone().multiplyScalar(dt)),
|
2022-04-09 11:29:54 +00:00
|
|
|
);
|
|
|
|
this.mixer.update(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
public addNameTag(name: string) {
|
|
|
|
this.nameTag = new NameTag(nameTagBuilder, name);
|
|
|
|
this.nameTag.tag.position.set(0, 1.8, 0.5);
|
|
|
|
this.container.add(this.nameTag.tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setWalkAnimationState(index: number) {
|
|
|
|
const previousState = this.walkAnimationState;
|
|
|
|
this.walkAnimationState = index;
|
|
|
|
if (previousState === this.walkAnimationState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.changes.animState = index;
|
|
|
|
|
|
|
|
if (index === 1) {
|
|
|
|
this.walkAction.reset().crossFadeFrom(this.idleAction, 0.5, false).play();
|
|
|
|
} else {
|
|
|
|
this.walkAction.crossFadeTo(this.idleAction.reset(), 0.5, false).play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|