avocado-old/packages/physics/shape.js

77 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-04-28 23:45:03 -05:00
import {compose, EventEmitter, Property} from '@avocado/core';
2019-03-22 11:25:20 -05:00
import {Vector} from '@avocado/math';
const decorate = compose(
EventEmitter,
Property('parent'),
Property('rotation', {
default: 0,
track: true,
}),
Property('scale', {
default: 1,
track: true,
}),
Vector.Mixin('origin', 'originX', 'originY', {
default: [0, 0],
track: true,
}),
Vector.Mixin('position', 'x', 'y', {
default: [0, 0],
track: true,
}),
)
export class Shape extends decorate(class {}) {
constructor() {
super();
this.on([
'originChanged',
'rotationChanged',
'scaleChanged',
], this.onAabbChanged, this);
}
2019-03-22 11:25:20 -05:00
get aabb() {
return [0, 0, 0, 0];
}
destroy() {
this.off([
'originChanged',
'rotationChanged',
'scaleChanged',
], this.onAabbChanged);
}
2019-03-22 11:25:20 -05:00
fromJSON(json) {
const keys = [
'origin',
'position',
'rotation',
'scale',
]
for (const key of keys) {
if (json[key]) {
this[key] = json[key];
}
}
return this;
}
onAabbChanged() {
this.emit('aabbChanged');
}
2019-04-13 03:32:44 -05:00
toJSON() {
return {
origin: this.origin,
position: this.position,
rotation: this.rotation,
scale: this.scale,
};
}
2019-03-22 11:25:20 -05:00
}