@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
121 lines (92 loc) • 3.11 kB
JavaScript
import { Reference } from "../../../engine/reference/v2/Reference.js";
import { MarkerGLAttributes } from "./MarkerGLAttributes.js";
function logPatchLoadError(e) {
console.error('Failed to load patch:', e);
}
export class MarkerGL {
/**
*
* @param {MinimapMarker} marker
* @param {Transform} transform
* @param entity
* @param id
* @param {MinimapMarkersGL} manager
* @constructor
*/
constructor(marker, transform, entity, id, manager) {
/**
*
* @type {MinimapMarker}
*/
this.marker = marker;
this.transform = transform;
this.entity = entity;
this.id = id;
/**
*
* @type {Reference<AtlasPatch>}
*/
this.patch = Reference.NULL;
this.manager = manager;
this.particles = this.manager.particles;
this.active = false;
}
updatePosition() {
const position = this.transform.position;
this.particles.executeOperationWriteAttribute_Vector3(
this.id,
MarkerGLAttributes.AttributePosition,
position.x,
1,
position.z
);
this.manager.needsRender = true;
}
updateIcon(newURL, oldURL) {
const atlasManager = this.manager.atlasManager;
this.patch.release();
atlasManager
.acquire(newURL)
.then((patch) => {
this.patch = patch;
//check if marker is still active
if (!this.active) {
//marker is dead
return;
}
const patchUv = patch.getValue().uv;
this.particles.executeOperationWriteAttribute_Vector4(
this.id,
MarkerGLAttributes.AttributePatch,
patchUv.position.x,
patchUv.position.y,
patchUv.size.x,
patchUv.size.y
);
this.manager.needsRender = true;
}, logPatchLoadError);
}
startup() {
this.active = true;
this.transform.position.process(this.updatePosition, this);
this.marker.iconURL.onChanged.add(this.updateIcon, this);
this.updateIcon(this.marker.iconURL.getValue(), null);
this.particles.executeOperationWriteAttribute_Scalar(
this.id,
MarkerGLAttributes.AttributeSize,
this.marker.size.x
);
this.particles.executeOperationWriteAttribute_Scalar(
this.id,
MarkerGLAttributes.AttributeZIndex,
this.marker.zIndex
);
}
shutdown() {
this.active = false;
this.transform.position.onChanged.remove(this.updatePosition, this);
this.marker.iconURL.onChanged.remove(this.updateIcon, this);
//release reference to icon
this.patch.release();
}
}