UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

127 lines (124 loc) 3.4 kB
import { Vector2, Vector3 } from "three/src/Three"; import { Component } from "../../hierarchy_object/Component"; import { CssSpriteRenderer } from "../render/CssSpriteRenderer"; export class GridEventMap extends Component { $r=new Map; wh=1; gh=1; Yr=false; Zr=[]; $h=[]; Nh=false; start() { this.Nh = true; const t = this.$h; for (let e = 0; e < t.length; ++e) { t[e](); } t.length = 0; } addEvent(t, e, i) { if (!this.Nh) { this.$h.push((() => { this.addEvent(t, e, i); })); return; } this.$r.set(`${t}_${e}`, i); if (this.Yr) { this.Gr(t * this.gridCellWidth, e * this.gridCellHeight); } } addEventsFromTwoDimensionalArray(t, e, i) { if (!this.Nh) { this.$h.push((() => { this.addEventsFromTwoDimensionalArray(t, e, i); })); return; } for (let s = 0; s < t.length; s++) { for (let r = 0; r < t[s].length; r++) { if (t[s][r] === null) continue; this.addEvent(r + e, t.length - (s + i) - 1, t[s][r]); } } } Xh() { this.$r.forEach(((t, e) => { const [i, s] = e.split("_").map(Number); this.Gr(i * this.gridCellWidth, s * this.gridCellHeight); })); } Yh() { const t = this.Zr; for (let e = 0; e < t.length; ++e) { t[e].destroy(); } this.Zr.length = 0; } Gr(t, e) { const i = { ref: null }; this.gameObject.addChildFromBuilder(this.engine.instantiater.buildGameObject("debug-image", new Vector3(t, e, 1e4)).withComponent(CssSpriteRenderer, (t => { t.opacity = .5; t.imageWidth = this.gridCellWidth; t.imageHeight = this.gridCellHeight; })).getGameObject(i)); this.Zr.push(i.ref); } tryInvokeEvent(t, e, i, s, r) { const h = this.transform.position; t -= h.x; e -= h.y; const n = Math.floor(t / this.gridCellWidth); const o = Math.floor((t + i) / this.gridCellWidth); const a = Math.floor(e / this.gridCellHeight); const l = Math.floor((e + s) / this.gridCellHeight); let _ = false; for (let t = a; t <= l; t++) { for (let e = n; e <= o; e++) { if (this.$r.has(`${e}_${t}`)) { this.$r.get(`${e}_${t}`)(e, t, r); _ = true; } } } return _; } get gridCellWidth() { return this.wh; } set gridCellWidth(t) { this.wh = t; } get gridCellHeight() { return this.gh; } set gridCellHeight(t) { this.gh = t; } get showEvents() { return this.Yr; } set showEvents(t) { this.Yr = t; if (this.Yr) { this.Xh(); } else { this.Yh(); } } get gridCenter() { const t = this.transform.position; return new Vector2(t.x, t.y); } get gridCenterX() { const t = this.transform.position; return t.x; } get gridCenterY() { const t = this.transform.position; return t.y; } }