UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

116 lines (89 loc) 2.86 kB
import { GridCellAction } from "./action/GridCellAction.js"; import { MarkerNodeMatcherAny } from "../markers/matcher/MarkerNodeMatcherAny.js"; import { assert } from "../../core/assert.js"; import { qt_match_data_by_circle } from "../../core/geom/2d/quad-tree/qt_match_data_by_circle.js"; export class GridCellActionTransformNearbyMarkers extends GridCellAction { /** * * @type {MarkerNodeMatcher} */ matcher = MarkerNodeMatcherAny.INSTANCE; /** * * @type {number} */ radius = 1; /** * * @type {MarkerNodeTransformer} */ transformer = null; /** * @private * @type {QuadTreeDatum<MarkerNode>[]} */ __leaves = []; /** * * @type {number} * @private */ __leaf_cursor = 0; /** * * @param {number} radius * @param {MarkerNodeMatcher} matcher * @param {MarkerNodeTransformer} transformer * @returns {GridCellActionTransformNearbyMarkers} */ static from(radius, matcher, transformer) { assert.greaterThanOrEqual(radius, 0, 'radius'); assert.equal(matcher.isMarkerNodeMatcher, true, 'matcher.isMarkerNodeMatcher'); assert.equal(transformer.isMarkerNodeTransformer, true, 'matcher.isMarkerNodeTransformer'); const r = new GridCellActionTransformNearbyMarkers(); r.radius = radius; r.matcher = matcher; r.transformer = transformer; return r; } initialize(data, seed) { super.initialize(data, seed); this.transformer.initialize(data, seed); } /** * * @param {QuadTreeDatum<MarkerNode>} leaf * @private */ __visitMarker(leaf) { /** * * @type {MarkerNode} */ const marker = leaf.data; const is_match = this.matcher.match(marker); if (is_match) { this.__leaves[this.__leaf_cursor] = leaf; this.__leaf_cursor++; } } execute(data, x, y, rotation) { const transformer = this.transformer; this.__leaf_cursor = qt_match_data_by_circle(this.__leaves, 0, data.markers, x, y, this.radius, this.matcher.match, this.matcher); const match_count = this.__leaf_cursor; const leaves = this.__leaves; // remove leaves for (let i = 0; i < match_count; i++) { const leaf = leaves[i]; leaf.disconnect(); } // transform and re-insert for (let i = 0; i < match_count; i++) { const leaf = leaves[i]; // transform const node = transformer.transform(leaf.data, data); // insert the node data.addMarker(node); } } }