the-world-engine
Version:
three.js based, unity like game engine for browser
100 lines (92 loc) • 2.92 kB
JavaScript
import { Vector2, Vector3 } from "three/src/Three";
import { EdgeShape } from "../../../../box2d.ts/build/index";
import { PrefabRef } from "../../../hierarchy_object/PrefabRef";
import { Color } from "../../../render/Color";
import { CssEdgeRenderer } from "../../render/CssEdgeRenderer";
import { ObjectAttacher2D } from "../ObjectAttacher2D";
import { getOrCreatePhysicsDebugRenderObject } from "../PhysicsDebugRender";
import { Collider2D } from "./Collider2D";
export class EdgeCollider2D extends Collider2D {
Va=[ new Vector2(-2, 0), new Vector2(2, 0) ];
bl=0;
gl=true;
pl=null;
rd=null;
ml() {
if (this.gl) {
let e = this.gameObject.getComponent(ObjectAttacher2D);
if (!e) e = this.gameObject.addComponent(ObjectAttacher2D);
if (this.pl) {
this.rd.points = this.Va;
} else {
const t = getOrCreatePhysicsDebugRenderObject(this.engine);
const r = new PrefabRef;
this.pl = t.addChildFromBuilder(this.engine.instantiater.buildGameObject(this.gameObject.name + "_debug_edge").withChild(this.engine.instantiater.buildGameObject("debug_edge", new Vector3(this.offset.x, this.offset.y, 200)).withComponent(CssEdgeRenderer, (e => {
e.points = this.Va;
e.viewScale = .01;
e.edgeWidth = 2;
e.edgeColor = new Color(1, 1, 0, .3);
e.pointerEvents = false;
})).getComponent(CssEdgeRenderer, r)));
this.rd = r.ref;
e.target = this.pl;
}
}
}
onEnable() {
super.onEnable();
this.ml();
}
onDisable() {
super.onDisable();
if (this.pl) {
this.pl.destroy();
this.pl = null;
}
}
createShapes() {
const e = [];
const t = this.Va;
for (let r = 0; r < t.length - 1; ++r) {
const s = new EdgeShape;
s.SetTwoSided(t[r], t[r + 1]);
s.m_radius = this.bl;
e.push(s);
}
return e;
}
get points() {
return this.Va;
}
set points(e) {
this.Va.length = 0;
for (let t = 0; t < e.length; ++t) {
this.Va.push(e[t].clone());
}
this.updateFixture();
this.ml();
}
get edgeRadius() {
return this.bl;
}
set edgeRadius(e) {
this.bl = e;
this.updateFixture();
}
get debugDraw() {
return this.gl;
}
set debugDraw(e) {
this.gl = e;
if (this.enabled && this.gameObject.activeInHierarchy) {
if (e) {
this.ml();
} else {
if (this.pl) {
this.pl.destroy();
this.pl = null;
}
}
}
}
}