65 lines
1.3 KiB
JavaScript
65 lines
1.3 KiB
JavaScript
|
import {compose} from '@avocado/core';
|
||
|
import {Mixin, Property} from '@avocado/mixins'
|
||
|
import {Vector} from '@avocado/math'
|
||
|
|
||
|
export function VectorMixin(
|
||
|
vector = 'vector',
|
||
|
x = 'x',
|
||
|
y = 'y',
|
||
|
meta = {},
|
||
|
) {
|
||
|
meta = {
|
||
|
default: [0, 0],
|
||
|
emit: function (...args) {
|
||
|
if (this.emit) {
|
||
|
this.emit(...args);
|
||
|
}
|
||
|
},
|
||
|
eq: function(l, r) {
|
||
|
return Vector.equals(l, r);
|
||
|
},
|
||
|
get: function() {
|
||
|
return [
|
||
|
this[x],
|
||
|
this[y],
|
||
|
];
|
||
|
},
|
||
|
...meta,
|
||
|
};
|
||
|
meta.set = meta.set || function(vector) {
|
||
|
if (meta.track && meta.emit) {
|
||
|
if (this[vector][0] !== vector[0]) {
|
||
|
meta.emit.call(this, `${x}Changed`, this[vector][0], vector[0]);
|
||
|
}
|
||
|
if (this[vector][1] !== vector[1]) {
|
||
|
meta.emit.call(this, `${y}Changed`, this[vector][1], vector[1]);
|
||
|
}
|
||
|
}
|
||
|
this[vector] = [vector[0], vector[1]];
|
||
|
}
|
||
|
const decorate = compose(
|
||
|
Property(vector, meta),
|
||
|
);
|
||
|
return (Superclass) => {
|
||
|
return class Vector extends decorate(Superclass) {
|
||
|
|
||
|
get [x]() {
|
||
|
return this[vector][0];
|
||
|
}
|
||
|
|
||
|
set [x](x) {
|
||
|
this[vector] = [x, this[vector][1]];
|
||
|
}
|
||
|
|
||
|
get [y]() {
|
||
|
return this[vector][1];
|
||
|
}
|
||
|
|
||
|
set [y](y) {
|
||
|
this[vector] = [this[vector][0], y];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
};
|
||
|
}
|