avocado-old/packages/physics/circle.js

55 lines
892 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.onAabbChanged, this);
}
get aabb() {
const [x, y] = this.position;
const halfRadius = this.radius / 2;
return [
x - halfRadius,
y - halfRadius,
x + halfRadius,
y + halfRadius,
];
}
destroy() {
super.destroy();
this.off('radiusChanged', this.onAabbChanged);
}
fromJSON(json) {
super.fromJSON(json);
if (json.radius) {
this.radius = json.radius;
}
return this;
}
onAabbChanged() {
this.emit('aabbChanged');
}
toJSON() {
return {
...super.toJSON(),
type: 'circle',
radius: this.radius,
}
}
}