2022-04-09 11:29:54 +00:00
|
|
|
import { IcyNetUser } from '../../common/types/user';
|
|
|
|
import * as THREE from 'three';
|
|
|
|
import { PonyEntity } from './pony';
|
|
|
|
import { Packet } from '../../common/types/packet';
|
2022-04-09 12:57:20 +00:00
|
|
|
import { CanvasUtils } from './canvas-utils';
|
|
|
|
import { ChatBubble } from './chat-bubble';
|
|
|
|
|
|
|
|
const chatBuilder = new CanvasUtils();
|
2022-04-09 11:29:54 +00:00
|
|
|
|
|
|
|
export class PlayerEntity extends PonyEntity {
|
2022-04-09 13:17:10 +00:00
|
|
|
private uncommittedPackets: Packet[] = [];
|
2022-04-09 12:57:20 +00:00
|
|
|
private _chats: ChatBubble[] = [];
|
|
|
|
|
2022-04-09 11:29:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-09 12:57:20 +00:00
|
|
|
public addChat(message: string): void {
|
|
|
|
const lines = [];
|
|
|
|
let truncated = message;
|
|
|
|
|
|
|
|
while (truncated.length > 80) {
|
|
|
|
lines.push(truncated.substring(0, 80));
|
|
|
|
truncated = truncated.substring(80);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (truncated) {
|
|
|
|
lines.push(truncated);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newChat = new ChatBubble(chatBuilder, lines);
|
|
|
|
|
|
|
|
this._chats.forEach((item) => {
|
|
|
|
item.tag.position.add(new THREE.Vector3(0, item.height * 0.01, 0));
|
|
|
|
});
|
|
|
|
|
|
|
|
this._chats.unshift(newChat);
|
|
|
|
newChat.tag.position.set(0, 1.8, 0.5);
|
|
|
|
this.container.add(newChat.tag);
|
|
|
|
|
|
|
|
if (this._chats.length > 3) {
|
|
|
|
this._chats.splice(3, this._chats.length - 1).forEach((item) => {
|
|
|
|
this.container.remove(item.tag);
|
|
|
|
item.dispose();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
const i = this._chats.indexOf(newChat);
|
|
|
|
|
|
|
|
if (i !== -1) {
|
|
|
|
this.container.remove(newChat.tag);
|
|
|
|
this._chats.splice(i, 1);
|
|
|
|
newChat.dispose();
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
|
2022-04-09 11:29:54 +00:00
|
|
|
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) {
|
2022-04-09 13:17:10 +00:00
|
|
|
this.uncommittedPackets.push(packet);
|
2022-04-09 11:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2022-04-09 13:17:10 +00:00
|
|
|
if (this.uncommittedPackets.length) {
|
|
|
|
this.setFromPacket(this.uncommittedPackets.splice(0, 1)[0]);
|
|
|
|
}
|
2022-04-09 11:29:54 +00:00
|
|
|
}
|
|
|
|
}
|