| 102 | * @see World.broadphase |
| 103 | */ |
| 104 | export default class QuadTree implements Broadphase<QuadTreeItem> { |
| 105 | world: World; |
| 106 | bounds: QuadRect | Bounds; |
| 107 | max_objects: number; |
| 108 | max_levels: number; |
| 109 | level: number; |
| 110 | objects: QuadTreeItem[]; |
| 111 | nodes: QuadTree[]; |
| 112 | |
| 113 | /** |
| 114 | * Total number of objects in this subtree (this node's own |
| 115 | * `objects` plus every descendant's). Maintained by `insert` |
| 116 | * and `remove` so `isPrunable` / `hasChildren` are O(1) reads |
| 117 | * instead of O(tree-size) walks. Reset to 0 in `clear`. |
| 118 | * @ignore |
| 119 | */ |
| 120 | _subtreeCount: number; |
| 121 | |
| 122 | /** |
| 123 | * Root-only scratch array reused across `retrieve` calls to |
| 124 | * avoid allocating a fresh array per pointer event / per |
| 125 | * narrow-phase query. Only the root allocates one; recursive |
| 126 | * subnode calls receive the array via the `result` arg. |
| 127 | * @ignore |
| 128 | */ |
| 129 | _retrieveScratch: QuadTreeItem[] | null; |
| 130 | |
| 131 | /** |
| 132 | * @param world - the physic world this QuadTree belongs to |
| 133 | * @param bounds - bounds of the node |
| 134 | * @param [max_objects=4] - max objects a node can hold before splitting into 4 subnodes |
| 135 | * @param [max_levels=4] - total max levels inside root QuadTree |
| 136 | * @param [level] - depth level, required for subnodes |
| 137 | */ |
| 138 | constructor( |
| 139 | world: World, |
| 140 | bounds: QuadRect | Bounds, |
| 141 | max_objects = 4, |
| 142 | max_levels = 4, |
| 143 | level = 0, |
| 144 | ) { |
| 145 | this.world = world; |
| 146 | this.bounds = bounds; |
| 147 | |
| 148 | this.max_objects = max_objects; |
| 149 | this.max_levels = max_levels; |
| 150 | |
| 151 | this.level = level; |
| 152 | |
| 153 | this.objects = []; |
| 154 | this.nodes = []; |
| 155 | |
| 156 | this._subtreeCount = 0; |
| 157 | this._retrieveScratch = level === 0 ? [] : null; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Split the node into 4 subnodes |
nothing calls this directly
no outgoing calls
no test coverage detected