the-world-engine
Version:
three.js based, unity like game engine for browser
277 lines (271 loc) • 7.77 kB
JavaScript
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 { CssTilemapRenderer } from "../render/CssTilemapRenderer";
import { CssFilter } from "../render/filter/CssFilter";
export class CssTilemapChunkRenderer 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;
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]();
}
this.$h.length = 0;
}
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(CssTilemapRenderer, (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(CssTilemapRenderer, 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 a = this.chunkSize % 2 === 0 ? 0 : -.5;
const l = this.chunkSize % 2 === 0 ? 0 : .5;
const _ = o.x + a;
const c = this.Zh - o.y - 1 + l;
n.drawTile(_, c, i, s);
const p = this.fn(r, h);
const u = this.Qh.get(p);
if (u === undefined) {
this.Qh.set(p, new Set([ `${_}_${c}` ]));
} else {
u.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;
const a = h.x + n;
const l = this.Zh - h.y - 1 + o;
r.clearTile(a, l);
const _ = this.fn(i, s);
const c = this.Qh.get(_);
if (c !== undefined) {
c.delete(`${a}_${l}`);
if (c.size === 0) {
this.Qh.delete(_);
this.Jh.delete(_);
r.destroy();
}
}
}
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;
this.Jh.forEach(((e, i) => {
e.imageSources = 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.Zh % 2 === 1 ? 0 : this.tn / 2;
const e = this.Zh % 2 === 1 ? 0 : this.en / 2;
return new Vector2(this.transform.localPosition.x + t, this.transform.localPosition.y + e);
}
get gridCenterX() {
const t = this.Zh % 2 === 1 ? 0 : this.tn / 2;
return this.transform.localPosition.x + t;
}
get gridCenterY() {
const t = this.Zh % 2 === 1 ? 0 : this.en / 2;
return this.transform.localPosition.y + t;
}
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;
}));
}
}