68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
import * as Colyseus from "colyseus.js";
|
|
import { logError, writeLine } from "./logger.js";
|
|
import { markReceive } from "./received-packages.js";
|
|
|
|
export class Client {
|
|
client;
|
|
room;
|
|
index;
|
|
packageIndex = 0;
|
|
state = {
|
|
x: 0,
|
|
y: -887.56,
|
|
z: 0,
|
|
rotationX: 0,
|
|
rotationY: 0,
|
|
rotationZ: 0,
|
|
animation: 'Running',
|
|
nickname: `${this.index}#${this.packageIndex++}`,
|
|
characterKey: 'male',
|
|
};
|
|
|
|
constructor(index) {
|
|
this.index = index;
|
|
this.delta = { x: Math.floor(Math.random() * 2 - 1) * 3, z: Math.floor(Math.random() * 2 - 1) * 3};
|
|
}
|
|
|
|
async joinRoom() {
|
|
this.client = new Colyseus.Client('wss://forwinmeta.sequenxe.com/api/socket');
|
|
|
|
this.room = await this.client.joinOrCreate("MyRoom");
|
|
this.room.onStateChange((state) => {
|
|
if (this.index !== 0) {
|
|
return;
|
|
}
|
|
markReceive();
|
|
writeLine(state);
|
|
});
|
|
this.room.onError((code, message) => {
|
|
logError('error:', code, message);
|
|
})
|
|
}
|
|
|
|
testOnce() {
|
|
this.state.x = this.delta.x + this.state.x;
|
|
this.state.z = this.delta.z + this.state.z;
|
|
|
|
if(this.state.x < -800) {
|
|
this.delta.x = Math.random() * 5 + 5;
|
|
this.state.x += this.delta.x;
|
|
} else if(this.state.x > 800) {
|
|
this.delta.x = -(Math.random() * 5 + 5);
|
|
this.state.x += this.delta.x;
|
|
};
|
|
if(this.state.z < -800) {
|
|
this.delta.z = Math.random() * 5 + 5;
|
|
this.state.z += this.delta.z;
|
|
} else if(this.state.z > 800) {
|
|
this.delta.z = -(Math.random() * 5 + 5);
|
|
this.state.z += this.delta.z;
|
|
}
|
|
|
|
// this.state.nickname = `${this.index}#${this.packageIndex++}`;
|
|
|
|
|
|
this.room.send("updatePlayer", this.state);
|
|
}
|
|
}
|