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(); } 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, this); }, removedFromRoom: (room) => { room.off('sizeChanged', this.onRoomSizeChanged); }, traitAdded: (type) => { if (-1 === [ 'positioned', 'followed', ].indexOf(type)) { return; } this.updatePosition(); }, } } tick(elapsed) { this.updatePosition(); this._camera.tick(elapsed); } }