avocado-old/packages/physics/circle.js
2019-04-28 23:45:03 -05:00

48 lines
767 B
JavaScript

import {compose, Property} from '@avocado/core';
import {Shape} from './shape';
const decorate = compose(
Property('radius', {
track: true,
}),
);
export class CircleShape extends decorate(Shape) {
constructor() {
super();
this.on('radiusChanged', () => {
this.emit('aabbChanged');
});
}
get aabb() {
const [x, y] = this.position;
const halfRadius = this.radius / 2;
return [
x - halfRadius,
y - halfRadius,
x + halfRadius,
y + halfRadius,
];
}
fromJSON(json) {
super.fromJSON(json);
if (json.radius) {
this.radius = json.radius;
}
return this;
}
toJSON() {
return {
...super.toJSON(),
type: 'circle',
radius: this.radius,
}
}
}