-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneElement.js
More file actions
90 lines (79 loc) · 1.98 KB
/
SceneElement.js
File metadata and controls
90 lines (79 loc) · 1.98 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {EntityElement} from "./hgl/elements.js"
import {Rect, Point} from "./hgl/geometry.js"
import {ObjectElement, BlockElement, LocationElement} from "./SceneChildElements.js"
export class SceneElement extends EntityElement {
constructor() {
super();
ObjectElement.register();
BlockElement.register();
LocationElement.register();
this.querySelectorAll("x-block").forEach(e => {
e.setRectFromDataset();
});
this.querySelectorAll("x-location").forEach(e => {
e.setRectFromDataset();
});
this.querySelectorAll("x-object").forEach(e => {
e.setRectFromDataset();
});
}
onSceneLoad() {
//TODO: Clean up hack by listening to some type of layout event
window.setTimeout(e=>{this.updateSizeFromComputedSize()}, 100);
}
verifyBlockCollision(point) {
let canPass = true;
if (!this.rect.containsPoint(point)) {
canPass = false;
}
this.querySelectorAll("x-block").forEach(e => {
if (e.disabled) {
return;
}
let r = e.getRectFromCSS();
if (e.getRectFromCSS().containsPoint(point)) {
canPass = false;
}
})
return canPass;
}
checkLocationCollision(point) {
let location = null;
this.querySelectorAll("x-location").forEach(e => {
if (e.disabled) {
return;
}
let r = e.getRectFromCSS();
if (e.getRectFromCSS().containsPoint(point)) {
location = e;
}
})
return location;
}
getClosestObject(point, range) {
let closestObject = null;
let shortestDistance = 100000;
this.querySelectorAll("x-object").forEach(e => {
if (e.disabled) {
return;
}
let objectCenterPoint = e.getRectFromCSS().centerPoint;
let horizontalDistance = Math.abs(objectCenterPoint.x - point.x);
if (horizontalDistance <= range) {
if (horizontalDistance < shortestDistance) {
closestObject = e.id;
shortestDistance = horizontalDistance;
}
}
});
return closestObject;
}
tick(game) {
this.querySelectorAll("x-object").forEach(e => {
e.tick();
})
}
static selector() {
return "x-scene";
}
}