feat: room changing!

This commit is contained in:
cha0s 2021-03-17 05:01:17 -05:00
parent 667573ce63
commit 74a5342ae7
2 changed files with 49 additions and 5 deletions

View File

@ -24,11 +24,20 @@ export default (latus) => class Universe extends JsonResource {
#tps; #tps;
addPlayer({entity, socket, user}) { addPlayer({entity, socket, user}) {
const room = this.room(entity.currentRoom); // eslint-disable-next-line no-param-reassign
room.addEntityToLayer(entity, 0); entity.universe = this;
entity.startInforming(room); const player = new Player({
const player = new Player({entity, socket, user}); entity,
socket,
user,
});
this.#players.push(player); 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; return player;
} }

View File

@ -3,15 +3,50 @@ import {StateProperty, Trait} from '@avocado/traits';
import {compose} from '@latus/core'; import {compose} from '@latus/core';
const decorate = compose( const decorate = compose(
StateProperty('currentRoom'), StateProperty('currentRoom', {
track: true,
}),
); );
export default () => class Universed extends decorate(Trait) { export default () => class Universed extends decorate(Trait) {
#universe;
static defaultState() { static defaultState() {
return { return {
currentRoom: 'rooms/town.room.json', 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();
}
}; };