UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

55 lines (37 loc) 1.35 kB
import { QuadTreeDatum } from "../../2d/quad-tree/QuadTreeDatum.js"; import { costByRemainingArea } from "./cost/costByRemainingArea.js"; import { cutArea } from "./cutArea.js"; import { findBestContainer } from "./findBestContainer.js"; /** * * @param {AABB2} box * @param {QuadTreeNode} free * @returns {boolean} */ export function packOneBox(box, free) { const w = box.getWidth(); const h = box.getHeight(); const container = findBestContainer(w, h, free, costByRemainingArea); if (container === null) { //couldn't find a place for box return false; } //remove container from free set container.disconnect(); //place box at bottom left of the container const y0 = container.y0; const x0 = container.x0; box.set(x0, y0, x0 + w, y0 + h); //update remaining set by removing this box area from free set cutArea(box, free); //split remaining space if (box.y1 !== container.y1) { const splitA = new QuadTreeDatum(container.x0, box.y1, container.x1, container.y1); free.insertDatum(splitA); } if (box.x1 !== container.x1) { const splitB = new QuadTreeDatum(box.x1, container.y0, container.x1, container.y1); free.insertDatum(splitB); } return true; }