-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.js
111 lines (102 loc) · 3.02 KB
/
obstacle.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const COLOR_AMOUNT = 10;
const RIGHT_DIR = 0;
const LEFT_DIR = 1;
const UP_DIR = 2;
const DOWN_DIR = 3;
const THICKNESS = 10;
class Obsticle {
constructor(dir, gapSize) {
this.dir = dir;
// The distance between the two rects.
this.gapSize = gapSize;
// Some random color.
let hue = int((random(COLOR_AMOUNT) * 360) / COLOR_AMOUNT);
let colorFill = color("hsl(" + hue + ", 100%, 15.3%)");
let colorBorder = color("hsla(" + hue + ", 100%, 55.3%, 0.52)");
this.colorFill = colorFill;
this.colorBorder = colorBorder;
switch (dir) {
// pos is 1 frame left to the screen.
case RIGHT_DIR:
this.pos = createVector(-2, 0);
this.vel = createVector(2, 0);
break;
// pos is 1 frame right to the screen.
case LEFT_DIR:
this.pos = createVector(width + 2, 0);
this.vel = createVector(-2, 0);
break;
// pos is 1 frame down to the screen.
case UP_DIR:
this.pos = createVector(0, height + 2);
this.vel = createVector(0, -2);
break;
// pos is 1 frame up to the screen.
case DOWN_DIR:
this.pos = createVector(0, -2);
this.vel = createVector(0, 2);
break;
}
if (dir === UP_DIR || dir === DOWN_DIR) {
// calculates the width of the rect.
this.xrange = random(0, width - this.gapSize);
this.yrange = THICKNESS;
// calculates the width of the rect by the other rect.
this.whatleftx = width - (this.xrange + this.gapSize);
this.whatlefty = this.yrange;
} else {
// calculates the width of the rect.
this.xrange = THICKNESS;
this.yrange = random(0, height - this.gapSize);
// calculates the width of the rect by the other rect.
this.whatleftx = this.xrange;
this.whatlefty = height - (this.yrange + this.gapSize);
}
}
update() {
this.pos.add(this.vel);
}
show() {
fill(this.colorFill);
stroke(this.colorBorder);
strokeWeight(4);
if (this.dir === UP_DIR || this.dir === DOWN_DIR) {
this.rect2posx = this.xrange + this.gapSize;
this.rect2posy = this.pos.y;
} else {
this.rect2posx = this.pos.x;
this.rect2posy = this.yrange + this.gapSize;
}
rect(this.pos.x, this.pos.y, this.xrange, this.yrange);
rect(this.rect2posx, this.rect2posy, this.whatleftx, this.whatlefty);
}
isOutOfScreen() {
switch (this.dir) {
// pos is 1 frame left to the screen.
case RIGHT_DIR:
if (this.pos.x > width + THICKNESS) {
return true;
}
break;
// pos is 1 frame right to the screen.
case LEFT_DIR:
if (this.pos.x < -THICKNESS) {
return true;
}
break;
// pos is 1 frame down to the screen.
case UP_DIR:
if (this.pos.y < -THICKNESS) {
return true;
}
break;
// pos is 1 frame up to the screen.
case DOWN_DIR:
if (this.pos.y > height + THICKNESS) {
return true;
}
break;
}
return false;
}
}