-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobstacle.js
More file actions
70 lines (56 loc) · 2.09 KB
/
Copy pathobstacle.js
File metadata and controls
70 lines (56 loc) · 2.09 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
class Obstacle {
constructor(vertices, velocity) {
// array of polygon's x- and y-coordinates (edges)
this.vertices = vertices;
// min. and max. x/y coordinates that create a rectangle
this.minValues = createVector();
this.maxValues = createVector();
}
show() {
fill(153, 112, 178);
stroke(123, 91, 142);
strokeWeight(4);
beginShape();
for (let i = 0; i < this.vertices.length; i++) {
vertex(this.vertices[i].x, this.vertices[i].y);
}
// connect last vertex with first one
vertex(this.vertices[0].x, this.vertices[0].y);
endShape();
}
update() {
for (let i = 0; i < this.vertices.length; i++) {
this.vertices[i].x -= gTerrain.velocity;
}
let minX = null;
let maxX = null;
let minY = null;
let maxY = null;
// retrieve min. and max. values of polygon
for (let i = 0; i < this.vertices.length; i++) {
minX = this.vertices[i].x < minX || minX == null ? this.vertices[i].x : minX;
maxX = this.vertices[i].x > maxX || maxX == null ? this.vertices[i].x : maxX;
minY = this.vertices[i].y < minY || minY == null ? this.vertices[i].y : minY;
maxY = this.vertices[i].y > maxY || maxY == null ? this.vertices[i].y : maxY;
}
this.minValues.x = minX;
this.minValues.y = minY;
this.maxValues.x = maxX;
this.maxValues.y = maxY;
}
offscreen() {
// max. x-value of polygon
let maxX = 0;
for (let i = 0; i < this.vertices.length; i++) {
maxX = this.vertices[i].x > maxX ? this.vertices[i].x : maxX;
}
// if max. x-value less than or equal to 0, whole polygon is offscreen
return maxX <= 0;
}
collidesPoint(posX, posY) {
// checks if point inside min-max rectangle (not really precise)
if (posX > this.minValues.x && posX < this.maxValues.x && posY > this.minValues.y && posY < this.maxValues.y) {
return true;
}
}
}