-
Notifications
You must be signed in to change notification settings - Fork 0
/
city.js
366 lines (336 loc) · 14.5 KB
/
city.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//import {createLight} from './createLight.js';
//x is at left of grid
//y is at bottom of grid
window.addEventListener('load', function () {
var docHeight = document.body.clientHeight;
var docWidth = document.body.clientWidth;
let margin = 5;
let bottomAreaHeight = 70;
const numberOfAttemptedLights = 1000;
const minLightSize = 1;
const maxLightSize = 3;
const percentOfLightsThatAreStatic = 15;//0-100
//Builds batch 1
const numberOfBuildings = 30;
const minBldgWidthPercent = 1;
const maxBldgWidthPercent = 5;
const minBldgHeightPercent = 40;
const maxBldgHeightPercent = 85;
//Builds batch 2
const numberOfBuildings2 = 15;
const minBldgWidthPercent2 = 8;
const maxBldgWidthPercent2 = 15;
const minBldgHeightPercent2 = 10;
const maxBldgHeightPercent2 = 40;
const numberOfArtifactsToAttempt = 15;
//Set bottom element
let bottom = document.getElementById('bottom');
bottom.style.width = docWidth + "px";
bottom.style.height = bottomAreaHeight + "px";
// Generate the buildings and the grid that represents where the buildings are in a 2D array
const maxX = docWidth;
const maxY = docHeight - bottomAreaHeight;
const grid = createArray(maxX, maxY);
for(let x = 0; x < grid.length; x++){
for(let y = 0; y < grid[x].length; y++){
grid[x][y] = false;
}
}
generateBuildings(grid, margin, maxY, maxX, numberOfBuildings, maxLightSize,
minBldgWidthPercent, maxBldgWidthPercent, minBldgHeightPercent, maxBldgHeightPercent);
generateBuildings(grid, margin, maxY, maxX, numberOfBuildings2, maxLightSize,
minBldgWidthPercent2, maxBldgWidthPercent2, minBldgHeightPercent2, maxBldgHeightPercent2);
// Identify areas to add artifacts to the buildings' sides and roofs.
const minHorSurfaceLength = 20;
const minVerSurfaceLength = 80;
const surfaces = identifyArtifactPlacements(grid, minHorSurfaceLength, minVerSurfaceLength);
placeArtifacts(surfaces, numberOfArtifactsToAttempt, grid, minHorSurfaceLength, minVerSurfaceLength);
// Create lights that only go into that 2D array
const colorScheme = generateColorScheme();
for(let x = 0; x < numberOfAttemptedLights; x ++){
let remainder = x % 100;
const lightIsStatic = remainder < percentOfLightsThatAreStatic;
createLight(margin, docHeight - bottomAreaHeight, docWidth, grid,
minLightSize, maxLightSize, lightIsStatic,
colorScheme, 4);
}
// Apply bg color
let everything = document.getElementById('everything');
everything.style.height = docHeight;
everything.classList.add(colorScheme.bgColor);
// Apply sun color and size
let sun = document.getElementById('sun');
if(sun !== undefined){
let sunSizePercent = generateRand(45, 80);
let sunSize = docWidth * sunSizePercent / 100;
let sunMarginPercent = generateRand(10,40);
let sunMargin = docHeight * sunMarginPercent / 100;
sun.style.width = sunSize;
sun.style.height = sunSize;
sun.style.borderRadius = sunSize + "px";
sun.style.transform = `translateY(${sunMargin}px)`;
sun.style.backgroundColor = colorScheme.sunColor;
}
// If in debug mode, print the color scheme number
//console.log("colorScheme: " + colorScheme.colorScheme);
})
function identifyArtifactPlacements(grid, minHorSurfaceLength, minVerSurfaceLength)
{
// Start on the left side. Find the rooftop there by starting at the bottom-left-most pixel
// and going up, 1 pixel at a time, till finding the roof of the left-most building.
// NOTE: this assumes that there is a left-most building.
let coord = {
x: 0,
y: 0
};
for(let y = 0; y < grid[0].length; y++){
let pixel = grid[0][y];
if(pixel === false){
coord = {
x: 0,
y: y
};
break;
}
}
// Go until reaching a corner. Then record the surface, turn the corner, and repeat.
let direction = "right";//can be "up", "down", or "right".
let surfaces = {
horizontalSurfaces: [],
verticalSurfaces: []
};
traverseCityScape(grid, surfaces, direction, coord);
// Discard any surfaces less than a certain length.
let newHorizontalSurfaces = [];
surfaces.horizontalSurfaces.forEach(s => {
if(s.length >= minHorSurfaceLength)
newHorizontalSurfaces.push(s);
});
let newVerticalSurfaces = [];
surfaces.verticalSurfaces.forEach(s => {
if(Math.abs(s.length) >= minVerSurfaceLength)
newVerticalSurfaces.push(s);
});
return {
horizontalSurfaces: newHorizontalSurfaces,
verticalSurfaces: newVerticalSurfaces,
};
}
// Recursive function to piece together all the surfaces of the buildings.
// Go until reaching a corner. Then record the surface, turn the corner, and repeat.
// direction: can be "up", "down", or "right".
function traverseCityScape(grid, surfaces, direction, coord){
// Go until reaching a corner.
let foundEndOfSegment = false;
let y = undefined;
let nextCoord = undefined;
switch(direction){
case "right":
let x = coord.x;
while(!foundEndOfSegment){
if(x === grid.length){
// We have reached the end of the screen. Return.
return;
}
nextCoord = grid[x + 1][coord.y];
let coordBeneathNextCoord = grid[x + 1][coord.y - 1];
if(nextCoord === true){
// We have reached a new building that goes taller than this one.
// Therefore we need to record this segment, and turn the corner up.
foundEndOfSegment = true;
surfaces.horizontalSurfaces.push({
startCoord: { x: coord.x, y: coord.y },
length: 1 + x - coord.x
});
traverseCityScape(grid, surfaces, "up", { x: x, y: coord.y });
}
else if(coordBeneathNextCoord === false){
// We have reached the end of this building's roof.
// Therefore we need to record this segment, and turn the corner down.
foundEndOfSegment = true;
surfaces.horizontalSurfaces.push({
startCoord: { x: coord.x, y: coord.y },
length: 1 + x - coord.x
});
traverseCityScape(grid, surfaces, "down", { x: x + 1, y: coord.y - 1 });
}
else{
// The next coord is empty and the coord beneath it is full. So we are still
// on the roof of this building. Therefore continue.
x++;
}
}
break;
case "up":
y = coord.y;
while(!foundEndOfSegment){
nextCoord = grid[coord.x][y + 1];
let coordRightOfNextCoord = grid[coord.x + 1][y + 1];
if(coordRightOfNextCoord === false){
// We have reached the top of this building.
// Therefore we need to record this segment, and turn the corner right.
foundEndOfSegment = true;
surfaces.verticalSurfaces.push({
startCoord: { x: coord.x, y: coord.y },//the y is wrong. should be 89, but is 335.
length: 1 + y - coord.y
});
traverseCityScape(grid, surfaces, "right", { x: coord.x + 1, y: y + 1 });
}
else{
// The coord right of next coord is full, so that means the building is continuing up.
// Therefore continue.
// Note: this assumes that no building reaches the very top of the screen.
y++;
}
}
break;
case "down":
y = coord.y;
while(!foundEndOfSegment){
if(y === 0){
// We have reached the bottom of the building at ground level.
// Therefore record this segment, and then skim along the ground to the next building,
// and pick up going up there.
foundEndOfSegment = true;
surfaces.verticalSurfaces.push({
startCoord: { x: coord.x, y: coord.y },
length: -1 * (1 + coord.y - y) // in this case, negative to indicate down direction
});
let groundX = coord.x + 1;
let groundHasEnded = false;
let screenHasEnded = false;
while(!groundHasEnded){
if(groundX === grid.length)
{
// We have reached the end of the screen.
groundHasEnded = true;
screenHasEnded = true;
}
else{
nextCoordRight = grid[groundX][0];
if(nextCoordRight === true)
// We have reached a building.
// Therefore we are done being at ground level, and can go up the new building.
groundHasEnded = true;
else
groundX++;
}
}
if(screenHasEnded)
return;
else
traverseCityScape(grid, surfaces, "up", { x: groundX - 1, y: 0 });
}
else{
nextCoord = grid[coord.x][y - 1];
if(nextCoord === true){
// We have reached the corner of a new building meeting this building's right side.
// Therefore we need to record this segment, and turn the corner right.
foundEndOfSegment = true;
surfaces.verticalSurfaces.push({
startCoord: { x: coord.x, y: coord.y },
length: y - 1 - coord.y
});
traverseCityScape(grid, surfaces, "right", { x: coord.x, y: y });
}
else{
// The next coordinate is empty. We have not reached the bottom of this building.
// Therefore continue.
y--;
}
}
}
break;
}
}
function generateBuildings(grid, margin, maxY, maxX, numberOfBuildings, maxLightSize,
minBldgWidthPercent, maxBldgWidthPercent, minBldgHeightPercent, maxBldgHeightPercent)
{
let buildings = [];
//Each building has a height and a width.
//The height can't be more than a certain percentage of the total. Same for width.
for(let i = 0; i < numberOfBuildings; i++)
{
//Pick a height and width.
let heightPercentage = generateRand(minBldgHeightPercent, maxBldgHeightPercent);
let height = maxY * heightPercentage / 100;
let widthPercentage = generateRand(minBldgWidthPercent, maxBldgWidthPercent);
let width = maxX * widthPercentage / 100;
//Now try to fit that into the grid.
const numberOfTries = 5;
const xMargin = 20;
let wasSuccessful = false;
for(let j = 0; j < numberOfTries; j++)
{
if(wasSuccessful === false){
//Pick an x coordinate at random from which to start the building.
//There's no point in picking one right at the beginning or end, and it can't
//be so close to the end that the building overflows off the edge of the screen.
let xCoord = 0;//The very first building will be all the way on the left.
if(i > 0 && i < numberOfBuildings - 1)//buildings in middle are picked random.
xCoord = generateRand(xMargin, maxX - xMargin - width);
if(i === numberOfBuildings - 1)//last building is close to the end
xCoord = Math.round(maxX - xMargin - width - 1);
//Determine if the building will validly fit given that xCoord.
wasSuccessful = true;//TODO
//If it fits, then end loop and create building.
if(wasSuccessful){
let building = {
Height: height,
Width: width,
XCoord: xCoord,
};
buildings.push(building);
createBuildingDiv(building, margin, maxX, maxY, maxLightSize);
addToGrid(building, grid);
//End loop as we have now been successful.
break;
}
}
}
}
}
function createBuildingDiv(building, margin, maxX, maxY, maxLightSize){
let cityscape = document.getElementById('cityscape');
let buildingDiv = document.createElement('div');
buildingDiv.classList.add("building");
buildingDiv.style.width = building.Width + maxLightSize + "px";
buildingDiv.style.height = building.Height + maxLightSize + "px";
buildingDiv.style.left = building.XCoord;
buildingDiv.style.top = maxY - building.Height;
cityscape.appendChild(buildingDiv);
}
function addToGrid(building, grid)
{
//the grid is x long to represent the x pixels wide.
//each element in the grid is an array y long to represent the y pixels high.
//Start at the x coordinate.
for(let x = building.XCoord; x < building.XCoord + building.Width + 1; x++)
{
//This is a vertical strip of the building, 1 pixel wide, and y pixels high.
for(let y = 0; y < building.Height + 1; y++)
{
// This is a single pixel in the strip. Add it to the grid.
grid[x][y] = true;
}
}
}
function pickRandomColor(color1, color2, color3, color4, NUMBER_OF_COLORS){
let num = generateRand(0, NUMBER_OF_COLORS);
let color = '';
switch(num){
case 1:
color = color1;
break;
case 2:
color = color2;
break;
case 3:
color = color3;
break;
case 4:
color = color4;
break;
}
return color;
}