UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

299 lines (293 loc) 8.48 kB
import { PrefabRef } from "../../hierarchy_object/PrefabRef"; import { Vector2, Vector3 } from "three/src/Three"; import { Component } from "../../hierarchy_object/Component"; import { ImageRenderingMode } from "../render/CssSpriteRenderer"; import { CssFilter } from "../render/filter/CssFilter"; import { CssCollideTilemapRenderer } from "./CssCollideTilemapRenderer"; export class CssCollideTilemapChunkRenderer extends Component { Jh=new Map; Qh=new Map; Zh=16; tn=1; en=1; sn=16; rn=16; hn=1.001; nn=null; on=true; ln=false; an=.1; cn=ImageRenderingMode.Pixelated; _n=() => { this.Jh.forEach(((t, e) => { t.filter.copy(this.Zs); })); }; Zs=new CssFilter(this._n); $h=[]; Nh=false; start() { this.Nh = true; const t = this.$h; for (let e = 0; e < t.length; ++e) { t[e](); } t.length = 0; } onEnable() { this.ln = true; } onDisable() { this.ln = false; } un() { this.Jh.forEach(((t, e) => { const i = this.dn(e) * this.Zh * this.tn; const s = this.pn(e) * this.Zh * this.en; t.gameObject.transform.localPosition.set(i, s, 0); })); } dn(t) { return parseInt(t.substring(0, t.indexOf("_"))); } pn(t) { return parseInt(t.substring(t.indexOf("_") + 1)); } fn(t, e) { return `${t}_${e}`; } mn(t, e, i, s) { const r = i - t * this.Zh + this.Zh / 2; const h = s - e * this.Zh + this.Zh / 2; return new Vector2(r, h); } gn(t, e) { const i = this.fn(t, e); let s = this.Jh.get(i); if (s === undefined) { const r = new PrefabRef; this.gameObject.addChildFromBuilder(this.engine.instantiater.buildGameObject(`css_tilemap_renderer_${t}_${e}`, new Vector3(t * this.Zh * this.tn, e * this.Zh * this.en, 0), undefined, (new Vector3).setScalar(this.hn)).withComponent(CssCollideTilemapRenderer, (t => { if (this.nn) t.imageSources = this.nn; t.gridCellWidth = this.tn; t.gridCellHeight = this.en; t.tileResolutionX = this.sn; t.tileResolutionY = this.rn; t.rowCount = this.Zh; t.columnCount = this.Zh; t.pointerEvents = this.on; t.viewScale = this.an; t.filter.copy(this.Zs); })).getComponent(CssCollideTilemapRenderer, r)); s = r.ref; this.Jh.set(i, s); } return s; } vn(t, e) { const i = this.fn(t, e); const s = this.Jh.get(i); if (s === undefined) { return null; } return s; } drawTile(t, e, i, s) { if (!this.Nh) { this.$h.push((() => { this.drawTile(t, e, i, s); })); return; } const r = Math.floor((t + this.Zh / 2) / this.Zh); const h = Math.floor((e + this.Zh / 2) / this.Zh); const n = this.gn(r, h); const o = this.mn(r, h, t, e); const l = this.chunkSize % 2 === 0 ? 0 : -.5; const a = this.chunkSize % 2 === 0 ? 0 : .5; const c = o.x + l; const _ = this.Zh - o.y - 1 + a; n.drawTile(c, _, i, s); const u = this.fn(r, h); const d = this.Qh.get(u); if (d === undefined) { this.Qh.set(u, new Set([ `${c}_${_}` ])); } else { d.add(`${c}_${_}`); } } drawTileFromTwoDimensionalArray(t, e, i) { if (!this.Nh) { this.$h.push((() => { this.drawTileFromTwoDimensionalArray(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.drawTile(r + e, t.length - s + i - 1, t[s][r].i, t[s][r].a); } } } clearTile(t, e) { if (!this.Nh) { this.$h.push((() => { this.clearTile(t, e); })); return; } const i = Math.floor((t + this.Zh / 2) / this.Zh); const s = Math.floor((e + this.Zh / 2) / this.Zh); const r = this.vn(i, s); if (r === null) return; const h = this.mn(i, s, t, e); const n = this.chunkSize % 2 === 0 ? 0 : -.5; const o = this.chunkSize % 2 === 0 ? 0 : .5; r.clearTile(h.x + n, this.Zh - h.y - 1 + o); } addCollider(t, e) { if (!this.Nh) { this.$h.push((() => { this.addCollider(t, e); })); return; } const i = Math.floor((t + this.Zh / 2) / this.Zh); const s = Math.floor((e + this.Zh / 2) / this.Zh); const r = this.gn(i, s); const h = this.mn(i, s, t, e); const n = this.chunkSize % 2 === 0 ? 0 : -.5; const o = this.chunkSize % 2 === 0 ? 0 : .5; r.addCollider(h.x + n, this.Zh - h.y - 1 + o); } checkCollision(t, e, i, s) { if (!this.ln) return false; const r = this.transform.position; const h = Math.floor(((t - r.x) / this.tn + this.Zh / 2) / this.Zh); const n = Math.floor(((e - r.y) / this.en + this.Zh / 2) / this.Zh); const o = this.fn(h, n); const l = this.Jh.get(o); if (l === undefined) { return false; } return l.checkCollision(t, e, i, s); } get chunkSize() { return this.Zh; } set chunkSize(t) { this.Zh = t; this.un(); this.Jh.forEach(((t, e) => { t.rowCount = this.Zh; t.columnCount = this.Zh; })); } set imageSources(t) { if (!this.Nh) { this.$h.push((() => { this.imageSources = t; })); return; } this.nn = t; } get pointerEvents() { return this.on; } set pointerEvents(t) { this.on = t; this.Jh.forEach(((e, i) => { e.pointerEvents = t; })); } get gridCellWidth() { return this.tn; } set gridCellWidth(t) { if (this.tn === t) return; this.tn = t; this.un(); this.Jh.forEach(((t, e) => { t.gridCellWidth = this.tn; })); } get gridCellHeight() { return this.en; } set gridCellHeight(t) { if (this.en === t) return; this.en = t; this.un(); this.Jh.forEach(((t, e) => { t.gridCellHeight = this.en; })); } get tileResolutionX() { return this.sn; } set tileResolutionX(t) { if (this.sn === t) return; this.sn = t; this.Jh.forEach(((t, e) => { t.tileResolutionX = this.sn; })); } get tileResolutionY() { return this.rn; } set tileResolutionY(t) { if (this.rn === t) return; this.rn = t; this.Jh.forEach(((t, e) => { t.tileResolutionY = this.rn; })); } get tilemapScale() { return this.hn; } set tilemapScale(t) { if (this.hn === t) return; this.hn = t; this.Jh.forEach(((t, e) => { t.gameObject.transform.localScale.setScalar(this.hn); })); } get gridCenter() { const t = this.transform.position; const e = this.Zh % 2 === 1 ? 0 : this.tn / 2; const i = this.Zh % 2 === 1 ? 0 : this.en / 2; return new Vector2(t.x + e, t.y + i); } get gridCenterX() { const t = this.transform.position; const e = this.Zh % 2 === 1 ? 0 : this.tn / 2; return t.x + e; } get gridCenterY() { const t = this.transform.position; const e = this.Zh % 2 === 1 ? 0 : this.en / 2; return t.y + e; } get viewScale() { return this.an; } set viewScale(t) { this.an = t; this.Jh.forEach(((t, e) => { t.viewScale = this.an; })); } get filter() { return this.Zs; } get imageRenderingMode() { return this.cn; } set imageRenderingMode(t) { this.cn = t; this.Jh.forEach(((e, i) => { e.imageRenderingMode = t; })); } }