MCPcopy Create free account
hub / github.com/melonjs/melonJS / insert

Method insert

packages/melonjs/src/physics/broadphase/quadtree.ts:332–377  ·  view source on GitHub ↗

* Insert the given object into the node. If the node * exceeds the capacity, it will split and add all * objects to their corresponding subnodes. * @param item - object to be added

(item: QuadTreeItem)

Source from the content-addressed store, hash-verified

330 * @param item - object to be added
331 */
332 insert(item: QuadTreeItem) {
333 // Subtree count: this insert adds one object SOMEWHERE in
334 // this subtree (either to `this.objects` or, by recursion,
335 // to a descendant's subtree). Bumping at every level entered
336 // gives every ancestor a correct running total.
337 this._subtreeCount++;
338
339 //if we have subnodes ...
340 if (this.nodes.length > 0) {
341 const index = this.getIndex(item);
342
343 if (index !== -1) {
344 this.nodes[index].insert(item);
345 return;
346 }
347 }
348
349 this.objects.push(item);
350
351 if (
352 this.objects.length > this.max_objects &&
353 this.level < this.max_levels
354 ) {
355 //split if we don't already have subnodes
356 if (this.nodes.length === 0) {
357 this.split();
358 }
359
360 //add all objects to their corresponding subnodes
361 let writeIdx = 0;
362 for (let i = 0, len = this.objects.length; i < len; i++) {
363 const subIndex = this.getIndex(this.objects[i]);
364 if (subIndex !== -1) {
365 // Redistribution: the item is being MOVED from
366 // `this.objects` into a subnode. The total in this
367 // subtree is unchanged, so we DON'T touch
368 // `this._subtreeCount`. The subnode's `insert`
369 // will increment its own count for the moved item.
370 this.nodes[subIndex].insert(this.objects[i]);
371 } else {
372 this.objects[writeIdx++] = this.objects[i];
373 }
374 }
375 this.objects.length = writeIdx;
376 }
377 }
378
379 /**
380 * Recursively remove the given container and its descendants from

Callers 1

insertContainerMethod · 0.95

Calls 4

getIndexMethod · 0.95
splitMethod · 0.95
insertMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected