-
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathQuadtree.js
More file actions
61 lines (57 loc) · 2.04 KB
/
Quadtree.js
File metadata and controls
61 lines (57 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright 2020 WICKLETS LLC
*
* This file is part of Wick Engine.
*
* Wick Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wick Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
*/
/* Quadtree wrapper */
// this.quadtree:
// - quadtree-lib data structure (https://github.com/elbywan/quadtree-lib#readme)
// - elements in form {x, y, width, height, uuid, inTree}
// this.dirty:
// - set of quadtree elements ({x, y, width, height, uuid, inTree})
// this.elements:
// - dictionary of elements {uuid1: element1, uuid2: element2}
// - these are the exact objects that go into this.quadtree by reference
Wick.Quadtree = class {
constructor (width, height) {
this._quadtree = new Quadtree({width: width, height: height});
this.dirty = new Set();
this.elements = {};
}
get quadtree() {
return this._quadtree;
}
clean() {
let to_remove = [];
this._quadtree.each(function(element) {
let clip = Wick.ObjectCache.getObjectByUUID(element.uuid);
if (!clip || !clip.onScreen) {
to_remove.push(element);
element.inTree = false;
}
});
for (let i = 0; i < to_remove.length; i++) {
this._quadtree.remove(to_remove[i]);
}
}
resize(width, height) {
this._quadtree.each(function(element) {
element.inTree = false;
this.dirty.add(element.uuid);
});
this._quadtree = new Quadtree({width: width, height: height});
}
}