feat: camera scaling

This commit is contained in:
cha0s 2021-03-14 05:48:57 -05:00
parent 05f553796f
commit d14b272320

View File

@ -9,7 +9,7 @@ const decorate = compose(
track: true,
}),
Property('drag', {
default: 0.2,
default: 0.15,
}),
Vector.Mixin('position', 'x', 'y', {
default: [0, 0],
@ -22,6 +22,10 @@ const decorate = compose(
default: [0, 0],
track: true,
}),
Vector.Mixin('scale', 'scaleX', 'scaleY', {
default: [1, 1],
track: true,
}),
Vector.Mixin('viewSize', 'viewWidth', 'viewHeight', {
default: [0, 0],
track: true,
@ -35,6 +39,7 @@ export default class Camera extends decorate(Class) {
this.viewOrigin = [0, 0];
this.on('areaSizeChanged', this.onAreaSizeChanged, this);
this.on('realPositionChanged', this.onRealPositionChanged, this);
this.on('scaleChanged', this.onViewSizeChanged, this);
this.on('viewSizeChanged', this.onViewSizeChanged, this);
}
@ -50,14 +55,14 @@ export default class Camera extends decorate(Class) {
}
onRealPositionChanged() {
this.realOffset = Vector.sub(this.realPosition, this.viewOrigin);
this.realOffset = Vector.mul(Vector.sub(this.realPosition, this.viewOrigin), this.scale);
}
onViewSizeChanged() {
// eslint-disable-next-line no-self-assign
this.position = this.position;
this.viewOrigin = Vector.scale(this.viewSize, 0.5);
this.realOffset = Vector.sub(this.realPosition, this.viewOrigin);
this.viewOrigin = Vector.div(Vector.scale(this.viewSize, 0.5), this.scale);
this.onRealPositionChanged();
}
get position() {
@ -65,11 +70,10 @@ export default class Camera extends decorate(Class) {
}
set position(position) {
if (0 === Vector.area(this.areaSize)) {
super.position = position;
return;
}
if (0 === Vector.area(this.viewSize)) {
if (
0 === Vector.area(this.areaSize)
|| 0 === Vector.area(this.viewSize)
) {
super.position = position;
return;
}
@ -89,17 +93,10 @@ export default class Camera extends decorate(Class) {
}
tick(elapsed) {
if (Vector.equals(this.position, this.realPosition)) {
return;
}
if (0 === this.drag) {
this.realPosition = this.position;
}
if (Vector.equalsClose(this.position, this.realPosition, 0.5)) {
if (0 === this.drag || Vector.equalsClose(this.position, this.realPosition, 0.5)) {
this.realPosition = this.position;
return;
}
// Sanity check.
const k = elapsed / this.drag;
if (k >= 1) {
this.realPosition = this.position;