the-world-engine
Version:
three.js based, unity like game engine for browser
165 lines (161 loc) • 4.42 kB
JavaScript
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 GridObjectCollideMap 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;
}
const i = this.ha.get(`${t}_${e}`);
this.ha.set(`${t}_${e}`, i === undefined ? 1 : i + 1);
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 s = 0; s < t.length; s++) {
for (let r = 0; r < t[s].length; r++) {
if (t[s][r] === 1) {
this.addCollider(r + e, t.length - (s + i) - 1);
}
}
}
}
removeCollider(t, e) {
const i = this.ha.get(`${t}_${e}`);
if (i === undefined) {
return;
}
if (i === 1) {
this.ha.delete(`${t}_${e}`);
} else {
this.ha.set(`${t}_${e}`, i - 1);
}
if (this.Yr) {
this._o(t * this.gridCellWidth, e * this.gridCellHeight);
}
}
lo() {
this.ha.forEach(((t, e) => {
const [i, s] = e.split("_").map(Number);
this.Gr(i * this.gridCellWidth, s * 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, s) {
if (!this.ln) return false;
const r = this.transform.position;
t -= r.x;
e -= r.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 + s) / this.gridCellHeight);
for (let t = l; t <= n; t++) {
for (let e = h; e <= o; e++) {
const i = this.ha.get(`${e}_${t}`);
if (i !== undefined && i > 0) {
return true;
}
}
}
return false;
}
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;
}
}