UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

174 lines (170 loc) 4.58 kB
import { Vector2, Vector3 } from "three/src/Three"; import { Component } from "../../hierarchy_object/Component"; import { PrefabRef } from "../../hierarchy_object/PrefabRef"; import { CssSpriteRenderer } from "../render/CssSpriteRenderer"; export class GridCollideMap extends Component { ha=new Map; wh=1; gh=1; Yr=false; so=false; ho=new Map; ln=false; $h=[]; Nh=false; start() { this.Nh = true; const t = this.$h; for (let e = 0; e < t.length; ++e) { t[e](); } this.$h.length = 0; } onEnable() { if (this.Yr && !this.so) { this.lo(); this.so = true; } this.ln = true; } onDisable() { this.ao(); this.so = false; this.ln = false; } addCollider(t, e) { if (!this.Nh) { this.$h.push((() => { this.addCollider(t, e); })); return; } if (this.ha.has(`${t}_${e}`)) return; this.ha.set(`${t}_${e}`, true); if (this.Yr) { this.Gr(t * this.gridCellWidth, e * this.gridCellHeight); } } addColliderFromTwoDimensionalArray(t, e, i) { if (!this.Nh) { this.$h.push((() => { this.addColliderFromTwoDimensionalArray(t, e, i); })); return; } for (let r = 0; r < t.length; r++) { for (let s = 0; s < t[r].length; s++) { if (t[r][s] === 1) { this.addCollider(s + e, t.length - (r + i) - 1); } } } } removeCollider(t, e) { if (!this.Nh) { this.$h.push((() => { this.removeCollider(t, e); })); return; } this.ha.delete(`${t}_${e}`); if (this.Yr) { this._o(t * this.gridCellWidth, e * this.gridCellHeight); } } removeAllColliders() { this.ha.clear(); this.ao(); } lo() { this.ha.forEach(((t, e) => { const [i, r] = e.split("_").map(Number); this.Gr(i * this.gridCellWidth, r * this.gridCellHeight); })); } ao() { this.ho.forEach((t => { t.destroy(); })); this.ho.clear(); } Gr(t, e) { const i = new PrefabRef; this.gameObject.addChildFromBuilder(this.engine.instantiater.buildGameObject("debug-image", new Vector3(t, e, 41e4)).withComponent(CssSpriteRenderer, (t => { t.opacity = .5; t.pointerEvents = false; t.imageWidth = this.gridCellWidth; t.imageHeight = this.gridCellHeight; })).getGameObject(i)); this.ho.set(`${t}_${e}`, i.ref); } _o(t, e) { const i = this.ho.get(`${t}_${e}`); if (i) { i.destroy(); this.ho.delete(`${t}_${e}`); } } checkCollision(t, e, i, r) { if (!this.ln) return false; const s = this.transform.position; t -= s.x; e -= s.y; const h = Math.floor(t / this.gridCellWidth); const o = Math.floor((t + i) / this.gridCellWidth); const l = Math.floor(e / this.gridCellHeight); const n = Math.floor((e + r) / this.gridCellHeight); for (let t = l; t <= n; t++) { for (let e = h; e <= o; e++) { if (this.ha.get(`${e}_${t}`)) { return true; } } } return false; } getCollidersToArray() { const t = []; this.ha.forEach(((e, i) => { const [r, s] = i.split("_").map(Number); t.push(new Vector2(r, s)); })); return t; } get gridCellWidth() { return this.wh; } set gridCellWidth(t) { this.wh = t; } get gridCellHeight() { return this.gh; } set gridCellHeight(t) { this.gh = t; } get showCollider() { return this.Yr; } set showCollider(t) { if (this.Yr === t) return; this.Yr = t; if (this.Yr && !this.so) { this.lo(); this.so = true; } else { this.ao(); this.so = false; } } get gridCenter() { const t = this.transform.position; return new Vector2(t.x, t.y); } get gridCenterX() { return this.transform.position.x; } get gridCenterY() { return this.transform.position.y; } }