-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildbutton.js
65 lines (62 loc) · 2.33 KB
/
buildbutton.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
var BuildButton = function(game, x, y, sprite, building, credits, buildIndex) {
this.building = building;
this.buildIndex = buildIndex;
this.build = function() {
if (this.building.canBuild(credits.credits, this.buildIndex)) {
this.building.build(this.buildIndex);
credits.addCredits(-this.building.units[this.buildIndex].cost);
}
};
this.button = game.add.button(0, 0, sprite, this.build, this);
this.button.fixedToCamera = true;
this.button.cameraOffset.x = x;
this.button.cameraOffset.y = y;
this.unitname = '';
this.tooltip = null;
this.showTooltip = false;
this.onOver = function() {
this.showTooltip = true;
this.tooltip.content = this.unitname + ' $' + this.building.units[this.buildIndex].cost;
};
this.onOut = function() {
this.showTooltip = false;
this.tooltip.content = '';
};
this.setTooltip = function(tooltip, unitname) {
this.unitname = unitname;
this.tooltip = tooltip;
this.button.onInputOver.add(this.onOver, this);
this.button.onInputOut.add(this.onOut, this);
};
this.shade = game.add.sprite(0, 0, 'shade');
this.shade.fixedToCamera = true;
this.shade.kill();
this.update = function() {
if (!this.building.sprite.alive) {
// Dead building; permanently shade
if (!this.shade.alive) {
this.shade.revive(1);
this.shade.cameraOffset.x = this.button.cameraOffset.x;
this.shade.cameraOffset.y = this.button.cameraOffset.y;
}
this.shade.width = this.button.width;
this.shade.height = this.button.height;
} else if (this.building.units[this.buildIndex].isBuilding) {
// draw a shade over the button
if (!this.shade.alive) {
this.shade.revive(1);
this.shade.cameraOffset.x = this.button.cameraOffset.x;
this.shade.cameraOffset.y = this.button.cameraOffset.y;
this.shade.width = this.button.width;
}
this.shade.height = (this.building.units[this.buildIndex].buildTime - this.building.units[this.buildIndex].buildCounter) / this.building.units[this.buildIndex].buildTime * this.button.height;
} else if (this.shade.alive) {
this.shade.kill();
}
// Update tooltip location
if (this.showTooltip) {
this.tooltip.x = game.input.mousePointer.worldX + 16;
this.tooltip.y = game.input.mousePointer.worldY + 16;
}
};
};