UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

69 lines (68 loc) 2.52 kB
import { CssTilemapRenderer } from "../render/CssTilemapRenderer"; export class CssCollideTilemapRenderer extends CssTilemapRenderer { ha=new Map; ln=false; onEnable() { super.onEnable(); this.ln = true; } onDisable() { super.onDisable(); this.ln = false; } drawTile(t, s, i, e) { super.drawTile(t, s, i, e); const h = Math.ceil(t - this.columnCount / 2); const r = Math.ceil(this.rowCount - s - this.rowCount / 2) - 1; this.ha.set(`${h}_${r}`, true); } drawTileFromTwoDimensionalArray(t, s, i) { super.drawTileFromTwoDimensionalArray(t, s, i); for (let e = 0; e < t.length; e++) { for (let h = 0; h < t[e].length; h++) { if (t[e][h] !== null) { const t = Math.ceil(this.transform.localPosition.x / this.gridCellWidth + (h + s) - this.columnCount / 2); const r = Math.ceil(this.transform.localPosition.y / this.gridCellHeight + (this.rowCount - (e + i)) - this.rowCount / 2) - 1; this.ha.set(`${t}_${r}`, true); } } } } clearTile(t, s) { super.clearTile(t, s); const i = Math.ceil(t - this.columnCount / 2); const e = Math.ceil(this.rowCount - s - this.rowCount / 2) - 1; this.ha.delete(`${i}_${e}`); } addCollider(t, s) { const i = Math.ceil(t - this.columnCount / 2); const e = Math.ceil(this.rowCount - s - this.rowCount / 2) - 1; this.ha.set(`${i}_${e}`, true); } checkCollision(t, s, i, e) { if (!this.ln) return false; const h = this.transform.position; t -= h.x; s -= h.y; if (this.columnCount % 2 === 0) { t -= this.gridCellWidth; } if (this.rowCount % 2 === 0) { s -= this.gridCellHeight; } if (this.rowCount % 2 === 0) s += this.gridCellHeight / 2; if (this.columnCount % 2 === 0) t += this.gridCellWidth / 2; const r = Math.floor(t / this.gridCellWidth); const a = Math.floor((t + i) / this.gridCellWidth); const l = Math.floor(s / this.gridCellHeight); const n = Math.floor((s + e) / this.gridCellHeight); for (let t = l; t <= n; t++) { for (let s = r; s <= a; s++) { if (this.ha.get(`${s}_${t}`) === true) { return true; } } } return false; } }