feat: followed

This commit is contained in:
cha0s 2019-04-05 12:25:16 -04:00
parent f75b44178f
commit bc3762a3bf

View File

@ -0,0 +1,73 @@
import {Camera} from '@avocado/topdown';
import {Trait} from '../trait';
export class Followed extends Trait {
static defaultParams() {
return {
viewSize: [320, 180],
}
}
initialize() {
const camera = new Camera();
camera.viewSize = this.params.get('viewSize').toJS();
this._camera = camera;
this.updatePosition();
this.onRoomSizeChanged = this.onRoomSizeChanged.bind(this);
this.onRoomSizeChanged();
}
get camera() {
return this._camera;
}
onRoomSizeChanged() {
if (!this.entity.is('roomed')) {
return;
}
this._camera.areaSize = this.entity.room.size;
}
updatePosition() {
if (!this.entity.is('positioned')) {
return;
}
this._camera.position = this.entity.position;
}
listeners() {
return {
// TODO won't catch initial room size changes.
addedToRoom: () => {
this.onRoomSizeChanged();
this.entity.room.on('sizeChanged', this.onRoomSizeChanged);
},
positionChanged: () => {
this.updatePosition();
},
removedFromRoom: (room) => {
room.off('sizeChanged', this.onRoomSizeChanged);
},
traitAdded: (type) => {
if (-1 === [
'positioned',
'followed',
].indexOf(type)) {
return;
}
this.updatePosition();
},
tick: (elapsed) => {
this._camera.tick(elapsed);
},
}
}
}