fix: quack quack

This commit is contained in:
cha0s 2022-03-24 05:46:47 -05:00
parent b5a71ccdc6
commit 9d0897608e
2 changed files with 8 additions and 15 deletions

View File

@ -6,9 +6,6 @@ import {
} from 'matter-js'; } from 'matter-js';
import AbstractBody from '../abstract/body'; import AbstractBody from '../abstract/body';
import ShapeList from '../shape/list';
import CircleShape from '../shape/circle';
import RectangleShape from '../shape/rectangle';
// Translate "real" coordinates to physics coordinates. // Translate "real" coordinates to physics coordinates.
export const SCALE = 1 / 12; export const SCALE = 1 / 12;
@ -55,7 +52,7 @@ export default class Body extends AbstractBody {
static bodyFromShape(shape) { static bodyFromShape(shape) {
const shapePosition = Vector.scale(shape.position, 1 / SCALE); const shapePosition = Vector.scale(shape.position, 1 / SCALE);
if (shape instanceof ShapeList) { if (shape.children) {
const children = []; const children = [];
for (let i = 0; i < shape.children.length; i++) { for (let i = 0; i < shape.children.length; i++) {
const child = shape.children[i]; const child = shape.children[i];
@ -71,12 +68,12 @@ export default class Body extends AbstractBody {
const {x, y} = body.position; const {x, y} = body.position;
return [body, shapePosition, [x, y]]; return [body, shapePosition, [x, y]];
} }
if (shape instanceof RectangleShape) { if (shape.width) {
const [width, height] = Vector.scale(shape.size, 1 / SCALE); const [width, height] = Vector.scale(shape.size, 1 / SCALE);
const body = Bodies.rectangle(0, 0, width, height); const body = Bodies.rectangle(0, 0, width, height);
return [body, shapePosition, [0, 0]]; return [body, shapePosition, [0, 0]];
} }
if (shape instanceof CircleShape) { if (shape.radius) {
const body = Bodies.circle(0, 0, shape.radius / SCALE); const body = Bodies.circle(0, 0, shape.radius / SCALE);
return [body, shapePosition, [0, 0]]; return [body, shapePosition, [0, 0]];
} }

View File

@ -5,25 +5,21 @@ import {
Renderable, Renderable,
} from '@avocado/graphics'; } from '@avocado/graphics';
import ShapeList from './list';
import CircleShape from './circle';
import PolygonShape from './polygon';
export default class ShapeView extends Renderable { export default class ShapeView extends Renderable {
constructor(shape) { constructor(shape) {
super(); super();
this.container = new Container(); this.container = new Container();
this.shape = shape; this.shape = shape;
if (shape instanceof PolygonShape) { if (shape.vertices) {
this.redrawPolygonLines(); this.redrawPolygonLines();
shape.on('aabbChanged', this.onPolygonShapeAabbChanged, this); shape.on('aabbChanged', this.onPolygonShapeAabbChanged, this);
} }
if (shape instanceof CircleShape) { if (shape.radius) {
this.redrawCircle(); this.redrawCircle();
shape.on('aabbChanged', this.onCircleShapeAabbChanged, this); shape.on('aabbChanged', this.onCircleShapeAabbChanged, this);
} }
if (shape instanceof ShapeList) { if (shape.children) {
for (let i = 0; i < shape.children.length; i++) { for (let i = 0; i < shape.children.length; i++) {
const child = shape.children[i]; const child = shape.children[i];
const childShapeView = new ShapeView(child); const childShapeView = new ShapeView(child);
@ -36,10 +32,10 @@ export default class ShapeView extends Renderable {
destroy() { destroy() {
super.destroy(); super.destroy();
if (this.shape instanceof PolygonShape) { if (this.shape.vertices) {
this.shape.off('aabbChanged', this.onPolygonShapeAabbChanged); this.shape.off('aabbChanged', this.onPolygonShapeAabbChanged);
} }
if (this.shape instanceof CircleShape) { if (this.shape.radius) {
this.shape.off('aabbChanged', this.onCircleShapeAabbChanged); this.shape.off('aabbChanged', this.onCircleShapeAabbChanged);
} }
this.shape.off('aabbChanged', this.onShapePositionChanged); this.shape.off('aabbChanged', this.onShapePositionChanged);