Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (45 sloc) 1.38 KB
import { Room } from "colyseus";
import { Schema, type, MapSchema } from "@colyseus/schema";
export class Player extends Schema {
@type("number")
x = Math.floor(Math.random() * 400);
@type("number")
y = Math.floor(Math.random() * 400);
}
export class State extends Schema {
@type({ map: Player })
players = new MapSchema<Player>();
something = "This attribute won't be sent to the client-side";
createPlayer (id: string) {
this.players[ id ] = new Player();
}
removePlayer (id: string) {
delete this.players[ id ];
}
movePlayer (id: string, movement: any) {
if (movement.x) {
this.players[ id ].x += movement.x * 10;
} else if (movement.y) {
this.players[ id ].y += movement.y * 10;
}
}
}
export class StateHandlerRoom extends Room<State> {
onInit (options) {
console.log("StateHandlerRoom created!", options);
this.setState(new State());
}
onJoin (client) {
this.state.createPlayer(client.sessionId);
}
onLeave (client) {
this.state.removePlayer(client.sessionId);
}
onMessage (client, data) {
console.log("StateHandlerRoom received message from", client.sessionId, ":", data);
this.state.movePlayer(client.sessionId, data);
}
onDispose () {
console.log("Dispose StateHandlerRoom");
}
}
You can’t perform that action at this time.