the-world-engine
Version:
three.js based, unity like game engine for browser
76 lines (72 loc) • 1.96 kB
JavaScript
import { Vector3 } from "three/src/Three";
import { Component } from "../../hierarchy_object/Component";
import { CssSpriteRenderer } from "../render/CssSpriteRenderer";
import { Pathfinder } from "./pathfind/Pathfinder";
export class PathfindTest extends Component {
Pr=null;
Cr=null;
Er=null;
Ir=null;
Mr=[];
Sr=this.Tr.bind(this);
start() {
if (this.Pr === null) {
throw new Error("Player not set");
}
if (this.Cr === null) {
throw new Error("Collide maps not set");
}
if (this.Er === null) {
throw new Error("Grid pointer not set");
}
this.Ir = new Pathfinder(this.Cr);
this.Er.onPointerDown.addListener(this.Sr);
}
onDestroy() {
this.Er?.onPointerDown.removeListener(this.Sr);
}
Tr(t) {
const e = this.Pr.positionInGrid;
const r = t.gridPosition;
const i = this.Ir.findPath(e, r);
this.Br();
if (i) {
for (let t = 0; t < i.length; ++t) {
const e = i[t];
this.Gr(e.x * this.Cr[0].gridCellWidth, e.y * this.Cr[0].gridCellHeight);
}
}
}
Gr(t, e) {
const r = {
ref: null
};
this.gameObject.addChildFromBuilder(this.engine.instantiater.buildGameObject("debug-image", new Vector3(t, e, 1e4)).withComponent(CssSpriteRenderer, (t => t.opacity = .5)).getGameObject(r));
this.Mr.push(r.ref);
}
Br() {
const t = this.Mr;
for (let e = 0; e < t.length; ++e) {
t[e].destroy();
}
t.length = 0;
}
get player() {
return this.Pr;
}
set player(t) {
this.Pr = t;
}
get collideMaps() {
return this.Cr;
}
set collideMaps(t) {
this.Cr = t;
}
get gridPointer() {
return this.Er;
}
set gridPointer(t) {
this.Er = t;
}
}