-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuilding.js
85 lines (71 loc) · 3.03 KB
/
building.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
import bezier from './easing.js';
const houseBez = bezier(0.00, 0.75, 0.29, 1.2);
const BuildTime = 1200;
// The x,y coords are the bottom left of the building, not the top left
// This is so buildings can be made bigger or smaller without changing the y-axis
export class Building {
constructor(x, y, width, height, flip, img, imgx, imgy, imgw, imgh, delay) {
this.x = x || 0;
this.y = y || 0;
// Caked-in assumption that buildings are 2x scale of background w.r.t. pixel density!
this.width = width;
this.height = height;
this.flip = flip;
this.img = img; // may be undefined
this.imgx = imgx;
this.imgy = imgy;
this.imgw = imgw;
this.imgh = imgh;
// Animation:
this.creationTime = 0; // Not yet
this.delay = delay || 0;
}
toString() {
return `${this.x}, ${this.y}, ${this.width}, ${this.height}, false, undefined, ${this.imgx}, ${this.imgy}, ${this.imgw}, ${this.imgh}`;
// return ` { creationTime: ${this.creationTime}, finishTime: ${this.finishTime}, flip: ${this.flip}, height: ${this.height}, imgh: ${this.imgh}, imgw: ${this.imgw}, imgx: ${this.imgx}, imgy: ${this.imgy}, width: ${this.width}, x: ${this.x}, y: ${this.y} }`;
}
build() {
this.creationTime = Date.now() + this.delay;
this.finishTime = this.creationTime + BuildTime;
}
// return true if x,y is within bounds
containsPoint(x, y) {
return ((this.x <= x) && ((this.x + this.width) >= x) &&
(this.y >= y) && ((this.y - this.height) <= y));
}
// Buildings draw from their bottom center, rather than their top left corner.
// This lets us adjust height etc later while maintaining their x/y positions
draw(ctx) {
const animating = (this.creationTime !== 0);
let value = 1;
if (animating) {
const elapsedTime = Date.now() - this.creationTime;
if (elapsedTime < 0) return; // still have some delay, first
if (elapsedTime > BuildTime) this.creationTime = 0;
value = houseBez(elapsedTime / BuildTime); // Goes between 0 and (greater than bc of bezier) 1'
}
const { x, y, flip, img, width, height } = this;
ctx.translate(x, y - height);
if (flip) { ctx.translate(width, 0); ctx.scale(-1, 1); }
if (animating) {
// Starting from a fixed scale like 0.6 looks nicer than appearing from nowhere.
// const scale = 0.8 + (value * 0.2);
// if scaling: this x value during animation ensures it animates from the center, not the left side as it scales up
ctx.translate(/* (1 - scale) * (width / 2) */ 0, (height - (height * value)));
// ctx.scale(scale, scale);
}
if (this.imgx) {
ctx.drawImage(img, this.imgx, this.imgy, this.imgw, this.imgh, 0, 0, width, height);
} else {
ctx.drawImage(img, 0, 0, width, height); // NYI
}
ctx.resetTransform();
// if (animating) {
// ctx.scale(valfrac, valfrac);
// ctx.translate(0, -(h - (h * value)));
// }
// if (flip) { ctx.scale(-1, 1); ctx.translate(-w, 0); }
// ctx.translate(-x, -y);
} // end draw
} // end class
export default Building;