-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildings.js
418 lines (352 loc) · 10.3 KB
/
buildings.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// image containing graphical assets
var graphics = new Image();
// texture dimensions within the loaded graphics image
var textureDimensions = {
tallLight:{x:0,y:0,w:100,h:250},
tallDark:{x:100,y:0,w:100,h:250},
tallRoof:{x:200,y:120,w:100,h:100},
shortLight:{x:200,y:0,w:100,h:120},
shortDark:{x:300,y:0,w:100,h:120},
shortRoof:{x:300,y:120,w:100,h:100}
};
// container for building designs
// used within the city
var buildingDesigns = {};
// the city object (has the buildings)
var city = null;
// drawing/ui elements
var canvas = null;
var ctx = null;
var stage = {w:0, h:0}; // based on canvas
var mouse = {x:0, y:0};
// runtime values
var perspective = 500; // affects "3D"-ness of buildings
var spacing = 150; // between building centers
var direction = 0; // over city
var speed = 2; // of movement over city
function init(){
graphics.onload = handleGraphicsLoaded;
graphics.src = "buildings.png";
}
function handleGraphicsLoaded(){
resourcesReady();
}
function resourcesReady(){
setupDrawingSurface();
setupBuildingDesigns();
setupCity();
setupUIHandlers();
// animate
requestAnimationFrame(render);
}
function setupDrawingSurface(){
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
// could potentially be something
// other than the canvas dimensions
stage.w = canvas.width;
stage.h = canvas.height;
}
function setupBuildingDesigns(){
// a building may reuse the same texture for different
// sides, which we're doing here. Two sides are dark
// and two sides are light.
var light = null;
var dark = null;
var roof = null;
var textures = null;
// tall (green) buildings
light = new Texture(graphics, textureDimensions.tallLight);
dark = new Texture(graphics, textureDimensions.tallDark);
roof = new Texture(graphics, textureDimensions.tallRoof);
textures = new BuildingTextures(roof, light, dark, dark, light);
// using texture sizes to also define building sizes
buildingDesigns.tall = new BuildingDesign(light.dimensions.w, light.dimensions.w, light.dimensions.h, textures);
// short (yellow) buildings
light = new Texture(graphics, textureDimensions.shortLight);
dark = new Texture(graphics, textureDimensions.shortDark);
roof = new Texture(graphics, textureDimensions.shortRoof);
textures = new BuildingTextures(roof, light, dark, dark, light);
// using texture sizes to also define building sizes
buildingDesigns.short = new BuildingDesign(light.dimensions.w, light.dimensions.w, light.dimensions.h, textures);
}
function setupCity(){
city = new City();
// area containing buildings
// based on stage size
city.generateLayout(stage.w, stage.h);
}
function setupUIHandlers(){
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("click", handleClick);
}
function handleMouseMove(event){
updateMouseFromEvent(mouse, event, canvas);
updateDirection();
}
function handleClick(event){
updateMouseFromEvent(mouse, event, canvas);
updateDirection();
}
function updateDirection(){
// direction is the angle of the
// mouse in relation to the center
// of the stage area
var dx = mouse.x - stage.w/2;
var dy = mouse.y - stage.h/2;
direction = Math.atan2(dy, dx);
}
function updateMouseFromEvent(mouse, event, elem){
if (elem == undefined){
elem = document.body;
}
var isTouch = false;
// touch screen events
if (event.touches){
if (event.touches.length){
isTouch = true;
mouse.x = parseInt(event.touches[0].pageX);
mouse.y = parseInt(event.touches[0].pageY);
}
}else{
// mouse events
mouse.x = parseInt(event.clientX);
mouse.y = parseInt(event.clientY);
}
// accounts for border
mouse.x -= elem.clientLeft;
mouse.y -= elem.clientTop;
// parent offsets
var par = elem;
while (par !== null) {
if (isTouch){
// touch events offset scrolling with pageX/Y
// so scroll offset not needed for them
mouse.x -= parseInt(par.offsetLeft);
mouse.y -= parseInt(par.offsetTop);
}else{
mouse.x += parseInt(par.scrollLeft - par.offsetLeft);
mouse.y += parseInt(par.scrollTop - par.offsetTop);
}
par = par.offsetParent || null;
}
}
function render(){
// clear canvas
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0,0,canvas.width, canvas.height);
// draw buildings within the city
city.draw();
requestAnimationFrame(render);
}
// For bug causing cos/sin failure for 0 values
function Math_cos(angle){
return angle !== 0 ? Math.cos(angle) : 1;
}
function Math_sin(angle){
return angle !== 0 ? Math.sin(angle) : 0;
}
// classes
/**
* City creates and animates buildings on the screen.
*/
function City(){
this.buildings = [];
}
/**
* Generates buildings in the city.
*/
City.prototype.generateLayout = function(w, h){
var x = 0;
var y = 0;
var xn = 1 + Math.ceil(w/spacing);
var yn = 1 + Math.ceil(h/spacing);
var design = null;
for (x=0; x<xn; x++){
for (y=0; y<yn; y++){
// random design
design = Math.random() > 0.5 ? buildingDesigns.short : buildingDesigns.tall;
this.buildings.push(new Building(x*spacing, y*spacing, design));
}
}
}
/**
* Updates building locations and draws
* them to the screen.
*/
City.prototype.draw = function(){
var building = null;
this.buildings.sort(this.sortBuildingsMethod);
// move buildings based on direction
var dx = speed * Math_cos(direction);
var dy = speed * Math_sin(direction);
// values for wrapping buildings around
// the screen for an infinite landscape
var spanW = spacing*Math.ceil(1 + stage.w/spacing);
var spanH = spacing*Math.ceil(1 + stage.h/spacing);
var extentL = -stage.w/2 - spacing;
var extentR = extentL + spanW;
var extentT = -stage.h/2 - spacing;
var extentB = extentT + spanH;
var i = 0;
var n = this.buildings.length;
for (i=0; i<n; i++){
building = this.buildings[i];
// move
building.x -= dx;
building.y -= dy;
// wrap around if outside borders
if (building.x > extentR){
building.x -= spanW;
}else if (building.x < extentL){
building.x += spanW;
}
if (building.y > extentB){
building.y -= spanH;
}else if (building.y < extentT){
building.y += spanH;
}
// draw into canvas around center point
building.draw(stage.w/2, stage.h/2);
}
}
/**
* Sort method used to determine the order
* in which buildings are drawn.
*/
City.prototype.sortBuildingsMethod = function(a, b){
var da = Math.abs(a.x) + Math.abs(a.y);
var db = Math.abs(b.x) + Math.abs(b.y)
if (da < db){
return 1;
}else if (da > db){
return -1;
}else{
return 0;
}
}
/**
* A Building is a structure within a city
* based on a BuildingDesign.
*/
function Building(x, y, design){
this.x = x;
this.y = y;
this.design = design;
}
/**
* Draws the building on the screen.
*/
Building.prototype.draw = function(offsetX, offsetY){
// the smaller the perspective value
// the greater the perspective effect
var perspX = this.x/perspective;
var perspY = this.y/perspective;
// building locations are actually based
// on the top-left of their base, not
// their center so as we offset them from
// the center of the stage, they still
// seem a little off visually.
var x = this.x + offsetX;
var y = this.y + offsetY;
var w = this.design.w;
var h = this.design.h;
var tall = this.design.tall;
var tw = 0; // texture width/height
var th = 0;
var texture = null;
// only 3 sides of a building are ever seen at
// one time so only north or south, or east
// or west are drawn with the roof, never both
// north and south, or east and west
// east or west sides
if (perspX < 0){
texture = this.design.textures.east;
tw = texture.dimensions.w;
th = texture.dimensions.h;
texture.draw(0, -h/tw, -perspX*tall/th, -perspY*tall/th, x+w + tall*perspX, y+h + tall*perspY);
}else{
texture = this.design.textures.west;
tw = texture.dimensions.w;
th = texture.dimensions.h;
texture.draw(0, h/tw, -perspX*tall/th, -perspY*tall/th, x + tall*perspX, y + tall*perspY);
}
// north or south sides
if (perspY < 0){
texture = this.design.textures.south;
tw = texture.dimensions.w;
th = texture.dimensions.h;
texture.draw(w/tw, 0, -perspX*tall/th, -perspY*tall/th, x + perspX*tall, y+h + tall*perspY);
}else{
texture = this.design.textures.north;
tw = texture.dimensions.w;
th = texture.dimensions.h;
texture.draw(-w/tw, 0, -perspX*tall/th, -perspY*tall/th, x+w + perspX*tall, y + tall*perspY);
}
// top (roof)
texture = this.design.textures.roof;
tw = texture.dimensions.w;
th = texture.dimensions.h;
texture.draw(w/tw, 0, 0, h/th, x + tall*perspX, y + tall*perspY);
}
/**
* Defines the "blueprint" of a building. Many
* Building instances may use the same design.
*/
function BuildingDesign(w, h, tall, textures){
this.w = w;
this.h = h;
this.tall = tall;
this.textures = textures;
}
/**
* Contains the textures used for the various
* faces of a building.
*/
function BuildingTextures(roof, north, east, south, west){
this.roof = roof;
this.north = north;
this.east = east || this.north;
this.south = south || this.north;
this.west = west || this.east;
}
/**
* A graphical element within an Image which
* contains many texture assets.
*/
function Texture(image, dimensions){
this.image = image;
this.dimensions = dimensions;
}
Texture.prototype.draw = function(a,b,c,d,x,y){
ctx.setTransform(a,b,c,d,x,y);
ctx.drawImage(this.image,
this.dimensions.x, this.dimensions.y, this.dimensions.w, this.dimensions.h,
0, 0, this.dimensions.w, this.dimensions.h);
};
/**
* requestAnimationFrame polyfill by Erik Möller
* http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
*/
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelRequestAnimationFrame = window[vendors[x]+
'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}())