Last active
July 16, 2019 11:49
-
-
Save doorbash/445310840227fd51d75f2da4f4e4b8a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Schema, type, ArraySchema, MapSchema } from "@colyseus/schema" | |
import { Room, Client } from "colyseus" | |
class PrimitivesTest extends Schema { | |
@type("uint8") | |
_uint8: number = 255; | |
@type("uint16") | |
_uint16: number = 65535; | |
@type("uint32") | |
_uint32: number = 4294967295; | |
@type("uint64") | |
_uint64: number = 18446744073709551615; | |
@type("int8") | |
_int8: number = -128; | |
@type("int16") | |
_int16: number = -32768; | |
@type("int32") | |
_int32: number = -2147483648; | |
@type("int64") | |
_int64: number = -9223372036854775808; | |
@type("float32") | |
_float32_n: number = -3.4e+38; | |
@type("float32") | |
_float32_p: number = +3.4E+38; | |
@type("float64") | |
_float_64_n:number = -1.7E+308; | |
@type("float64") | |
_float_64_p:number = +1.7E+308; | |
@type("boolean") | |
_boolean:boolean = true; | |
@type("string") | |
_string:string = "hello world!"; | |
} | |
class Player extends Schema { | |
@type("int32") | |
x:number = 10; | |
} | |
class Cell extends Schema { | |
@type("float32") | |
x:number; | |
@type("float32") | |
y:number; | |
} | |
class MyState extends Schema { | |
@type(PrimitivesTest) | |
primitives:PrimitivesTest; | |
@type([Player]) | |
players = new ArraySchema<Player>(); | |
@type({map: Cell}) | |
cells = new MapSchema<Cell>(); | |
} | |
export class ChatRoom extends Room { | |
maxClients = 4; | |
autoDispose = true; | |
onInit(options) { | |
console.log("Room created!", options); | |
this.setState(new MyState()); | |
var player:Player = new Player(); | |
player.x = 1 | |
this.state.players.push(player); | |
player = new Player(); | |
player.x = 2 | |
this.state.players.push(player); | |
player = new Player(); | |
player.x = 3 | |
this.state.players.push(player); | |
player = new Player(); | |
player.x = 4 | |
this.state.players.push(player); | |
player = new Player(); | |
player.x = 5 | |
this.state.players.push(player); | |
player = new Player(); | |
player.x = 6 | |
this.state.players.push(player); | |
var cell:Cell = new Cell(); | |
cell.x = 1; | |
cell.y = 1; | |
this.state.cells["xxx"] = cell; | |
cell = new Cell(); | |
cell.x = 2; | |
cell.y = 2; | |
this.state.cells["yyy"] = cell; | |
cell = new Cell(); | |
cell.x = 3; | |
cell.y = 3; | |
this.state.cells["zzz"] = cell; | |
this.clock.setInterval(() => { | |
if(this.state.cells["yyy"] !== undefined){ | |
delete this.state.cells["yyy"] | |
} else { | |
var cell:Cell = new Cell(); | |
cell.x = 100; | |
cell.y = 100; | |
this.state.cells["yyy"] = cell; | |
} | |
}, 5000); | |
} | |
onJoin?(client: Client, options?: any, auth?: any): void | Promise<any> { | |
console.log('onJoin(', client.id, ')', options); | |
} | |
onLeave(client) { | |
console.log("onLeave(" + client.sessionId + ")"); | |
} | |
onMessage(client, data) { | |
console.log("Room received message from", client.id, ":", data); | |
} | |
onDispose() { | |
console.log("Dispose Room"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment