refactor: Camera::realOffset

This commit is contained in:
cha0s 2019-04-13 18:13:35 -05:00
parent cd8b59c218
commit 07f4f0487c

View File

@ -28,12 +28,16 @@ export class Camera extends decorate(class {}) {
constructor() {
super();
this._realOffset = [0, 0];
this.viewOrigin = [0, 0];
this.on('areaSizeChanged', this.onAreaSizeChanged, this);
this.on('realPositionChanged', this.onRealPositionChanged, this);
this.on('viewSizeChanged', this.onViewSizeChanged, this);
}
destroy() {
this.off('areaSizeChanged', this.onAreaSizeChanged);
this.off('realPositionChanged', this.onRealPositionChanged, this);
this.off('viewSizeChanged', this.onViewSizeChanged);
}
@ -41,8 +45,14 @@ export class Camera extends decorate(class {}) {
this.position = this.position;
}
onRealPositionChanged() {
this._realOffset = Vector.sub(this.realPosition, this.viewOrigin);
}
onViewSizeChanged() {
this.position = this.position;
this.viewOrigin = Vector.scale(this.viewSize, 0.5);
this._realOffset = Vector.sub(this.realPosition, this.viewOrigin);
}
get position() {
@ -56,17 +66,21 @@ export class Camera extends decorate(class {}) {
if (0 === Vector.area(this.viewSize)) {
return super.position = position;
}
const halfViewSize = Vector.scale(this.viewSize, 0.5);
const viewOrigin = this.viewOrigin;
super.position = Vector.clamp(
position,
halfViewSize,
Vector.sub(this.areaSize, halfViewSize)
viewOrigin,
Vector.sub(this.areaSize, viewOrigin)
);
}
get realOffset() {
return this._realOffset;
}
get rectangle() {
return Rectangle.compose(
Vector.sub(this.position, Vector.scale(this.viewSize, 0.5)),
Vector.sub(this.position, this.viewOrigin),
this.viewSize,
);
}