-
Notifications
You must be signed in to change notification settings - Fork 0
/
skyline.js
105 lines (92 loc) · 2.34 KB
/
skyline.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
class Skyline {
constructor(w, h) {
this.w = w;
this.h = h;
this.buildings = this.buildSkyline();
}
buildSkyline(n) {
let buildings = [];
let origin = 0;
while (origin < this.w) {
let newBuilding = new Building(origin, this.w, this.h);
buildings.push(newBuilding);
origin += newBuilding.w;
}
return buildings;
}
addTallest(xPosition) {
let tallestBuilding = new Skyscraper(xPosition, this.w, this.h);
this.buildings.push(tallestBuilding);
}
render() {
for (let b of this.buildings) {
b.show();
}
}
}
class Building {
constructor(x0, envWidth, envHeight, colour = color(31, 31, 31)) {
this.x0 = x0;
this.w = random(0.02, 0.1) * envWidth;
this.h = random(0.2, 0.6) * envHeight;
this.colour = colour;
this.windows = new Windows(this.x0, this.w, this.h, random(0.05, 0.1));
}
show() {
stroke(this.colour);
fill(this.colour);
rectMode(CORNER);
rect(this.x0, 800 - this.h, this.w, this.h);
// this.windows.show();
}
}
class Skyscraper extends Building {
constructor(x0, envWidth, envHeight, colour = color(31, 31, 31)) {
super(x0, envWidth, envHeight, colour = color(31, 31, 31));
//Update two attributes...
this.x0 -= this.w / 2;
this.h = 0.85 * envHeight;
this.windows = new Windows(this.x0, this.w, this.h, random(0.05, 0.1));
}
show() {
super.show();
//Show spire...
noStroke();
fill(this.colour);
rectMode(CORNER);
rect(this.x0 + this.w / 2 - 5, 595, 7, 50);
}
}
class Windows {
constructor(x0, buildingWidth, buildingHeight, probLightOn) {
this.x0 = x0;
this.buildingWidth = buildingWidth;
this.buildingHeight = buildingHeight;
this.probLightOn = probLightOn;
this.windows = [];
this.createWindows();
}
createWindows() {
for (let i = this.x0; i < this.buildingWidth + this.x0; i += 5) {
for (let j = 800 - (this.buildingHeight * 0.9); j < 800; j += 5) {
this.windows.push(new Window(this.probLightOn, i, j));
}
}
}
show() {
for (let window of this.windows) {
if (window.hasLightOn) {
noStroke();
fill(255, 251, 230);
square(window.x, window.y, 2);
}
}
}
}
class Window {
constructor(probLightOn, x, y) {
this.hasLightOn = random() < probLightOn;
this.x = x;
this.y = y;
}
}