the-world-engine
Version:
three.js based, unity like game engine for browser
205 lines (202 loc) • 5.47 kB
JavaScript
import { Vector3 } from "three/src/Three";
import { Component } from "../../hierarchy_object/Component";
import { CssSpriteRenderer } from "../render/CssSpriteRenderer";
export class GridCollider extends Component {
ha=new Map;
do=false;
co=false;
ho=new Map;
po=null;
fo=false;
$h=[];
Nh=false;
start() {
this.Nh = true;
const t = this.$h;
for (let i = 0; i < t.length; ++i) {
t[i]();
}
this.$h.length = 0;
if (!this.po) {
throw new Error("GridCollider: gridObjectCollideMap must be set");
}
}
onEnable() {
if (this.do && !this.co) {
this.lo();
this.co = true;
}
this.mo();
}
onDisable() {
if (this.do) {
this.ao();
this.co = false;
}
if (this.fo) {
this.uo();
}
}
onDestroy() {
this.ao();
this.uo();
}
addCollider(t, i) {
if (!this.Nh) {
this.$h.push((() => {
this.addCollider(t, i);
}));
return;
}
if (this.ha.has(`${t}_${i}`)) {
return;
}
this.ha.set(`${t}_${i}`, true);
if (this.do) {
this.Gr(t, i);
}
if (this.gridObjectCollideMap) {
this.Co(t, i);
}
}
addColliderFromTwoDimensionalArray(t, i, s) {
if (!this.Nh) {
this.$h.push((() => {
this.addColliderFromTwoDimensionalArray(t, i, s);
}));
return;
}
for (let e = 0; e < t.length; e++) {
for (let h = 0; h < t[e].length; h++) {
if (t[e][h] === 1) {
this.addCollider(h + i, t.length - e + s - 1);
}
}
}
}
removeCollider(t, i) {
if (!this.Nh) {
this.$h.push((() => {
this.removeCollider(t, i);
}));
return;
}
this.ha.delete(`${t}_${i}`);
if (this.do) {
this._o(t, i);
}
if (this.gridObjectCollideMap) {
this.vo(t, i);
}
}
lo() {
this.ha.forEach(((t, i) => {
const [s, e] = i.split("_").map(Number);
this.Gr(s, e);
}));
}
ao() {
this.ho.forEach((t => {
t.destroy();
}));
this.ho.clear();
}
Gr(t, i) {
if (!this.po) return;
const s = this.po.gridCellWidth;
const e = this.po.gridCellHeight;
const h = t * s;
const o = i * e;
const r = {
ref: null
};
this.gameObject.addChildFromBuilder(this.engine.instantiater.buildGameObject("debug-image", new Vector3(h, o, 41e4)).withComponent(CssSpriteRenderer, (t => {
t.opacity = .5;
t.imageWidth = s;
t.imageHeight = e;
})).getGameObject(r));
this.ho.set(`${t}_${i}`, r.ref);
}
_o(t, i) {
const s = this.ho.get(`${t}_${i}`);
if (s) {
s.destroy();
this.ho.delete(`${t}_${i}`);
}
}
Co(t, i) {
if (!this.po) return;
const s = this.transform.position;
const e = this.po.gridCellWidth;
const h = this.po.gridCellHeight;
const o = this.po.gridCenter;
const r = Math.round(t + (s.x + o.x) / e);
const n = Math.round(i + (s.y + o.y) / h);
this.po.addCollider(r, n);
}
mo() {
if (!this.po) return;
const t = this.transform.position;
const i = this.po.gridCellWidth;
const s = this.po.gridCellHeight;
const e = this.po.gridCenter;
this.ha.forEach(((h, o) => {
const [r, n] = o.split("_").map(Number);
const l = Math.round(r + t.x / i + e.x);
const a = Math.round(n + t.y / s + e.y);
this.po.addCollider(l, a);
}));
}
vo(t, i) {
if (!this.po) return;
const s = this.transform.position;
const e = this.po.gridCellWidth;
const h = this.po.gridCellHeight;
const o = this.po.gridCenter;
const r = Math.round(t + (s.x + o.x) / e);
const n = Math.round(i + (s.y + o.y) / h);
this.po.removeCollider(r, n);
}
uo() {
if (!this.po) return;
const t = this.transform.position;
const i = this.po.gridCellWidth;
const s = this.po.gridCellHeight;
const e = this.po.gridCenter;
this.ha.forEach(((h, o) => {
const [r, n] = o.split("_").map(Number);
const l = Math.round(r + (t.x + e.x) / i);
const a = Math.round(n + (t.y + e.y) / s);
this.po.removeCollider(l, a);
}));
}
get gridObjectCollideMap() {
return this.po;
}
set gridObjectCollideMap(t) {
if (this.fo) {
this.uo();
}
this.po = t;
this.mo();
this.fo = true;
if (this.po && this.do) {
this.ao();
this.lo();
}
}
get showCollideSpot() {
return this.do;
}
set showCollideSpot(t) {
if (this.do === t) return;
this.do = t;
if (this.do && !this.co) {
this.lo();
this.co = true;
} else {
this.ao();
this.co = false;
}
}
}