From 74a5342ae70f459a6f5791176a0c47981dc5dcea Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 17 Mar 2021 05:01:17 -0500 Subject: [PATCH] feat: room changing! --- packages/universe/src/resources/universe.js | 17 +++++++--- packages/universe/src/traits/universed.js | 37 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/universe/src/resources/universe.js b/packages/universe/src/resources/universe.js index 4ac01b0..76ecd0d 100644 --- a/packages/universe/src/resources/universe.js +++ b/packages/universe/src/resources/universe.js @@ -24,11 +24,20 @@ export default (latus) => class Universe extends JsonResource { #tps; addPlayer({entity, socket, user}) { - const room = this.room(entity.currentRoom); - room.addEntityToLayer(entity, 0); - entity.startInforming(room); - const player = new Player({entity, socket, user}); + // eslint-disable-next-line no-param-reassign + entity.universe = this; + const player = new Player({ + entity, + socket, + user, + }); this.#players.push(player); + setTimeout(() => { + // eslint-disable-next-line no-param-reassign + entity.setPosition([20, 20]); + // eslint-disable-next-line no-param-reassign + entity.currentRoom = 'rooms/town.room.json'; + }, 5000); return player; } diff --git a/packages/universe/src/traits/universed.js b/packages/universe/src/traits/universed.js index 192dfce..3d3ab81 100644 --- a/packages/universe/src/traits/universed.js +++ b/packages/universe/src/traits/universed.js @@ -3,15 +3,50 @@ import {StateProperty, Trait} from '@avocado/traits'; import {compose} from '@latus/core'; const decorate = compose( - StateProperty('currentRoom'), + StateProperty('currentRoom', { + track: true, + }), ); export default () => class Universed extends decorate(Trait) { + #universe; + static defaultState() { return { currentRoom: 'rooms/town.room.json', }; } + onCurrentRoomChanged(oldRoom) { + if (!this.#universe) { + return; + } + if (oldRoom) { + const room = this.#universe.room(oldRoom); + this.entity.stopInforming(room); + room.removeEntityFromLayer(this.entity, 0); + } + if (this.entity.currentRoom) { + const room = this.#universe.room(this.entity.currentRoom); + room.addEntityToLayer(this.entity, 0); + this.entity.startInforming(room); + } + } + + listeners() { + return { + + currentRoomChanged: (oldRoom) => { + this.onCurrentRoomChanged(oldRoom); + }, + + }; + } + + set universe(universe) { + this.#universe = universe; + this.onCurrentRoomChanged(); + } + };