76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
|
import { IcyNetUser } from '../../common/types/user';
|
||
|
import * as THREE from 'three';
|
||
|
import { PonyEntity } from './pony';
|
||
|
import { Packet } from '../../common/types/packet';
|
||
|
|
||
|
export class PlayerEntity extends PonyEntity {
|
||
|
private uncommittedPacket: Packet = {};
|
||
|
constructor(public user: IcyNetUser) {
|
||
|
super();
|
||
|
}
|
||
|
|
||
|
public static fromUser(user: IcyNetUser, scene: THREE.Scene): PlayerEntity {
|
||
|
const entity = new PlayerEntity(user);
|
||
|
entity.initialize();
|
||
|
entity.addNameTag(user.display_name);
|
||
|
scene.add(entity.container);
|
||
|
return entity;
|
||
|
}
|
||
|
|
||
|
public setVelocity(velocity: THREE.Vector3) {
|
||
|
this.velocity.copy(velocity);
|
||
|
}
|
||
|
|
||
|
public setAngularVelocity(velocity: THREE.Vector3) {
|
||
|
this.angularVelocity.copy(velocity);
|
||
|
}
|
||
|
|
||
|
public setPosition(pos: THREE.Vector3) {
|
||
|
this.container.position.copy(pos);
|
||
|
}
|
||
|
|
||
|
public setRotation(rot: THREE.Vector3 | THREE.Euler) {
|
||
|
this.container.rotation.copy(
|
||
|
rot instanceof THREE.Euler
|
||
|
? rot
|
||
|
: new THREE.Euler(rot.x, rot.y, rot.z, 'XYZ'),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
public addUncommittedChanges(packet: Packet) {
|
||
|
this.uncommittedPacket = { ...this.uncommittedPacket, ...packet };
|
||
|
}
|
||
|
|
||
|
public update(dt: number) {
|
||
|
super.update(dt);
|
||
|
this.commitServerUpdate();
|
||
|
}
|
||
|
|
||
|
private setFromPacket(packet: Packet) {
|
||
|
if (packet.velocity) {
|
||
|
this.setVelocity(new THREE.Vector3().fromArray(packet.velocity));
|
||
|
}
|
||
|
|
||
|
if (packet.position) {
|
||
|
this.setPosition(new THREE.Vector3().fromArray(packet.position));
|
||
|
}
|
||
|
|
||
|
if (packet.rotation) {
|
||
|
this.setRotation(new THREE.Euler().fromArray(packet.rotation));
|
||
|
}
|
||
|
|
||
|
if (packet.angular) {
|
||
|
this.setAngularVelocity(new THREE.Vector3().fromArray(packet.angular));
|
||
|
}
|
||
|
|
||
|
if (packet.animState) {
|
||
|
this.setWalkAnimationState(packet.animState);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private commitServerUpdate() {
|
||
|
this.setFromPacket(this.uncommittedPacket);
|
||
|
this.uncommittedPacket = {};
|
||
|
}
|
||
|
}
|