57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { Packet } from 'src/net/packet';
|
|
import { PlayerSocket } from 'src/types/data-socket';
|
|
import { Humanoid } from './humanoid.object';
|
|
import { Group } from './group.object';
|
|
import { Vector3 } from 'three';
|
|
import { PacketType } from 'src/types/packet-type.enum';
|
|
|
|
export class Player {
|
|
public character: Group;
|
|
public controller: Humanoid;
|
|
public initialized = false;
|
|
|
|
constructor(
|
|
public socket: PlayerSocket,
|
|
public id: string,
|
|
public name: string,
|
|
private token: string,
|
|
) {}
|
|
|
|
public send(packet: Packet): void;
|
|
public send(packet: Buffer): void;
|
|
public send(packet: unknown): void {
|
|
if (packet instanceof Packet) {
|
|
return this.socket.send(packet.toBuffer());
|
|
}
|
|
this.socket.send(packet as Buffer);
|
|
}
|
|
|
|
public createPlayerCharacter(pos = new Vector3(0, 1, 0)) {
|
|
const group = new Group();
|
|
group.name = this.name;
|
|
group.position.copy(pos);
|
|
this.character = group;
|
|
|
|
const humanoid = new Humanoid();
|
|
humanoid.uuid = this.id;
|
|
group.add(humanoid);
|
|
this.controller = humanoid;
|
|
|
|
return group;
|
|
}
|
|
|
|
public handlePlayerPacket(packet: Packet) {
|
|
if (packet.packet === PacketType.PLAYER_MOVEMENT) {
|
|
const velocity = packet.read<Vector3>('vec3');
|
|
const lookAt = packet.read<Vector3>('vec3');
|
|
if (this.controller) {
|
|
this.controller.setVelocity(velocity);
|
|
this.controller.setLook(lookAt);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|