Hi, I am experimenting with this library and have a setup like this (JS, a lot omitted for brevity):
class MyRoom extends Room {
onInit () {
this.setState({ players: {} })
this.tick = this.tick.bind(this)
this.setSimulationInterval(this.tick, 1000 / 30)
}
onJoin (client, options) {
this.state.players[client.sessionId] = new Player(sessionId, this)
}
tick () {
Object.values(this.state.players).forEach(player => player.update())
}
}
class Player {
constructor (id, game) {
this.id = id
this.game = game // <-- this is the main problem
this.x = randX()
this.y = randY()
this.update = this.update.bind(this)
}
update () {
// perform some calculations and update this.x and this.y
// this involves this.game (for collision detections against other players, for example)
}
}
As you can see, the room has a game loop which calls Player#update
every frame (this just moves the player according to his velocity vector and checks collisions against walls and other players via this.game
).
The problem is that this.game
puts game
on the player state object which is transmitted to the clients, as well as the update
function. (This actually crashes the process because it cannot format it correctly). Using nosync
also doesn't work because then for some reason this.game
is not available in Player#update
.
Ideally I could have a this.state
field on Player
with the actual state, and the rest left as variables for use in the class (similar to how the room works), ie:
class Player {
constructor (id, game) {
this.id = id
this.game = game
this.state = { x: randX(), y: randY() }
}
}
What's the recommended approach here?