101 lines
1.8 KiB
JavaScript
101 lines
1.8 KiB
JavaScript
import {Trait} from '@avocado/entity';
|
|
|
|
import {Camera} from '../camera';
|
|
|
|
export default class Followed extends Trait {
|
|
|
|
static defaultParams() {
|
|
return {
|
|
viewSize: [320, 180],
|
|
}
|
|
}
|
|
|
|
static describeParams() {
|
|
return {
|
|
viewSize: {
|
|
type: 'vector',
|
|
label: 'View size',
|
|
},
|
|
};
|
|
}
|
|
|
|
static type() {
|
|
return 'followed';
|
|
}
|
|
|
|
constructor(entity, params, state) {
|
|
super(entity, params, state);
|
|
const camera = new Camera();
|
|
camera.viewSize = this.params.viewSize;
|
|
this._camera = camera;
|
|
this.updatePosition();
|
|
this.onRoomSizeChanged();
|
|
}
|
|
|
|
destroy() {
|
|
if (!this.entity.is('roomed')) {
|
|
return;
|
|
}
|
|
const room = this.entity.room;
|
|
if (room) {
|
|
room.off('sizeChanged', this.onRoomSizeChanged);
|
|
}
|
|
}
|
|
|
|
get camera() {
|
|
return this._camera;
|
|
}
|
|
|
|
onRoomSizeChanged() {
|
|
if (!this.entity.is('roomed')) {
|
|
return;
|
|
}
|
|
const room = this.entity.room;
|
|
if (room) {
|
|
this._camera.areaSize = room.size;
|
|
}
|
|
}
|
|
|
|
updatePosition() {
|
|
if (!this.entity.is('positioned')) {
|
|
return;
|
|
}
|
|
this._camera.position = this.entity.position;
|
|
}
|
|
|
|
listeners() {
|
|
return {
|
|
|
|
addedToRoom: () => {
|
|
this.onRoomSizeChanged();
|
|
this.entity.room.on('sizeChanged', this.onRoomSizeChanged, this);
|
|
},
|
|
|
|
removedFromRoom: (room) => {
|
|
room.off('sizeChanged', this.onRoomSizeChanged);
|
|
},
|
|
|
|
traitAdded: (type) => {
|
|
this.onRoomSizeChanged();
|
|
if (-1 === [
|
|
'positioned',
|
|
'followed',
|
|
].indexOf(type)) {
|
|
return;
|
|
}
|
|
this.updatePosition();
|
|
},
|
|
|
|
}
|
|
}
|
|
|
|
tick(elapsed) {
|
|
this.updatePosition();
|
|
}
|
|
|
|
renderTick(elapsed) {
|
|
this._camera.tick(elapsed);
|
|
}
|
|
|
|
}
|