Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

99 lines (76 sloc) 2.28 KB
import { Room, Client, generateId } from "colyseus";
import { Schema, type, MapSchema, ArraySchema } from "@colyseus/schema";
class Entity extends Schema {
@type("number")
x: number = 0;
@type("number")
y: number = 0;
}
class Player extends Entity {
@type("boolean")
connected: boolean = true;
}
class Enemy extends Entity {
@type("number")
power: number = Math.random() * 10;
}
class State extends Schema {
@type({ map: Entity })
entities = new MapSchema<Entity>();
@type(["number"])
arrayOfNumbers = new ArraySchema<number>();
}
export class DemoRoom extends Room {
onInit (options: any) {
console.log("DemoRoom created!", options);
this.setState(new State());
this.populateEnemies();
this.setPatchRate(1000 / 20);
this.setSimulationInterval((dt) => this.update(dt));
}
populateEnemies () {
for (let i=0; i<=3; i++) {
const enemy = new Enemy();
enemy.x = Math.random() * 2;
enemy.y = Math.random() * 2;
this.state.entities[generateId()] = enemy;
this.state.arrayOfNumbers.push(Math.random());
}
}
requestJoin (options: any) {
console.log("request join!", options);
return true;
}
onJoin (client: Client, options: any) {
console.log("client joined!", client.sessionId);
this.state.entities[client.sessionId] = new Player();
}
async onLeave (client: Client, consented: boolean) {
this.state.entities[client.sessionId].connected = false;
try {
if (consented) {
throw new Error("consented leave!");
}
console.log("let's wait for reconnection!")
const newClient = await this.allowReconnection(client, 10);
console.log("reconnected!", newClient.sessionId);
} catch (e) {
console.log("disconnected!", client.sessionId);
delete this.state.entities[client.sessionId];
}
}
onMessage (client: Client, data: any) {
console.log(data, "received from", client.sessionId);
if (data === "move_right") {
this.state.entities[client.sessionId].x += 0.01;
}
console.log(this.state.entities[client.sessionId].x);
this.broadcast({ hello: "hello world" });
}
update (dt?: number) {
// console.log("num clients:", Object.keys(this.clients).length);
}
onDispose () {
console.log("disposing DemoRoom...");
}
}
You can’t perform that action at this time.