-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerMenu.js
239 lines (217 loc) · 7.21 KB
/
TowerMenu.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
/*TowerMenu sets up the towers for the menus*/
function TowerMenu() {
//Menu Locations
this.topLeft = new Array();
this.topLeft.push(new Point(870, 188));
this.topLeft.push(new Point(930, 188));
this.topLeft.push(new Point(870, 248));
this.topLeft.push(new Point(930, 248));
this.topLeft.push(new Point(870, 308));
this.topLeft.push(new Point(930, 308));
this.width = 56;
this.height = 58;
//upgradeLocations
this.upgradeTopLeft = new Array();
this.upgradeTopLeft.push(new Point(873, 482));
this.upgradeTopLeft.push(new Point(873, 522));
this.upgradeWidth = 112;
this.upgradeHeight = 38;
//create the towers
this.towers = new Array();
for (var i = 1; i <= this.topLeft.length; i++) {
this.towers.push(
TowerType.createTower(
new Point(this.topLeft[i - 1].x + 29, this.topLeft[i - 1].y + 29),
0,
i
)
);
}
this.draggingTower = false; //are we dragging a tower?
this.draggedTower = null; //the tower we are dragging
this.towerHighlighted = -1; //is a tower highlighted -1 = false
this.towerSelected = -1; //is a tower selected? -1 = false
this.upgradeHighlighted = -1; //is an upgrade highlighted? -1 = false
}
TowerMenu.prototype.drawAll = function (ctx) {
//draw the menu tower
for (var i = 1; i <= this.towers.length; i++) {
this.getHighlighted();
this.towers[i - 1].draw(ctx, true, this.towerSelected == i, false, true);
}
//draw the draggedTower
if (this.draggingTower) {
//test to see if the tower is in a safe place
var greenRadius = this.validPosition();
this.draggedTower.position.x = SuperTower.mouseX;
this.draggedTower.position.y = SuperTower.mouseY;
this.draggedTower.draw(ctx, false, true, true, greenRadius);
}
};
/*Determines whether or not a tower is highlighted*/
TowerMenu.prototype.getHighlighted = function () {
//a clicked tower is automatically highlighted
if (this.towerSelected != -1) {
this.towerHighlighted = this.towerSelected;
this.upgradeHighlighted = -1;
} else {
//test if tower is highlighted
var mouseX = SuperTower.mouseX;
var mouseY = SuperTower.mouseY;
this.towerHighlighted = -1;
for (var i = 0, n = this.topLeft.length; i < n; i++) {
if (
mouseX >= this.topLeft[i].x &&
mouseX <= this.topLeft[i].x + this.width &&
mouseY >= this.topLeft[i].y &&
mouseY <= this.topLeft[i].y + this.height
) {
this.towerHighlighted = i + 1;
}
}
//test if upgrade is highlighted
this.upgradeHighlighted = -1;
for (var i = 0, n = this.upgradeTopLeft.length; i < n; i++) {
if (
mouseX >= this.upgradeTopLeft[i].x &&
mouseX <= this.upgradeTopLeft[i].x + this.upgradeWidth &&
mouseY >= this.upgradeTopLeft[i].y &&
mouseY <= this.upgradeTopLeft[i].y + this.upgradeHeight
) {
this.upgradeHighlighted = i + 1;
}
}
}
};
/*Returns the price for the named tower (1-6)
@return the price of the tower
*/
TowerMenu.prototype.getCost = function (tower) {
return this.towers[tower - 1].cost;
};
/*Returns the price for the named tower (1-6)
@return the price of the tower
*/
TowerMenu.prototype.getName = function (tower) {
return this.towers[tower - 1].name;
};
/*Handles mouseDown */
TowerMenu.prototype.mouseDown = function () {
var mouseX = SuperTower.mouseX;
var mouseY = SuperTower.mouseY;
this.towerSelected = -1;
//Did the user click on an upgrade?
for (var i = 0, n = this.upgradeTopLeft.length; i < n; i++) {
if (
mouseX >= this.upgradeTopLeft[i].x &&
mouseX <= this.upgradeTopLeft[i].x + this.upgradeWidth &&
mouseY >= this.upgradeTopLeft[i].y &&
mouseY <= this.upgradeTopLeft[i].y + this.upgradeHeight
) {
//is a tower selected, if so send an upgrade click?
if (SuperTower.towers.selectedTower != null) {
SuperTower.towers.selectedTower.increaseUpgrade(i + 1);
}
}
}
//Did the user click on a tower?
for (var i = 0, n = this.topLeft.length; i < n; i++) {
if (
mouseX >= this.topLeft[i].x &&
mouseX <= this.topLeft[i].x + this.width &&
mouseY >= this.topLeft[i].y &&
mouseY <= this.topLeft[i].y + this.height
) {
//do they have enough money to select this tower?
if (this.towers[i].cost <= SuperTower.cash) {
this.towerSelected = i + 1;
//create a new tower to drag
this.draggedTower = TowerType.createTower(
new Point(mouseX, mouseY),
0,
i + 1
);
this.draggingTower = true;
}
}
}
};
/*Handles mouseUp */
TowerMenu.prototype.mouseUp = function () {
if (this.draggingTower != false) {
//create a new tower if the position is valid
if (this.validPosition()) {
SuperTower.towers.add(
new Point(SuperTower.mouseX, SuperTower.mouseY),
0,
this.towerSelected
);
SuperTower.cash -= this.draggedTower.cost;
}
//we are no longer dragging a tower
this.towerSelected = -1;
this.draggedTower = null;
this.draggingTower = false;
}
};
/*Checks whether or not the tower is in a valid position*/
TowerMenu.prototype.validPosition = function () {
//off screen
if (
this.draggedTower.position.x <= Background.SCREEN_X ||
this.draggedTower.position.x >=
Background.SCREEN_X + Background.SCREEN_WIDTH ||
this.draggedTower.position.y <= Background.SCREEN_Y ||
this.draggedTower.position.y >=
Background.SCREEN_Y + Background.SCREEN_HEIGHT
) {
return false;
}
//what is the closest distance to another tower?
var closestDistance =
SuperTower.stageList.stages[SuperTower.stage].paths[0].width * 2.2;
var closestDistanceSquared = closestDistance * closestDistance;
//too close to other towers
for (var i = SuperTower.towers.towers.length - 1; i >= 0; i--) {
var xDiff =
SuperTower.towers.towers[i].position.x - this.draggedTower.position.x;
var yDiff =
SuperTower.towers.towers[i].position.y - this.draggedTower.position.y;
var distanceSquared = xDiff * xDiff + yDiff * yDiff;
if (distanceSquared < closestDistanceSquared) {
return false;
}
}
//too close to a path
for (
var i = 0, n = SuperTower.stageList.stages[SuperTower.stage].paths.length;
i < n;
i++
) {
if (
SuperTower.stageList.stages[SuperTower.stage].paths[i].inPath(
this.draggedTower.position
)
) {
return false;
}
}
return true;
};
/*returns whether or not an upgrade was clicked using current mouse position
@return {Boolean} true if upgrade was clicked*/
TowerMenu.prototype.upgradeClicked = function () {
var mouseX = SuperTower.mouseX;
var mouseY = SuperTower.mouseY;
for (var i = 0, n = this.upgradeTopLeft.length; i < n; i++) {
if (
mouseX >= this.upgradeTopLeft[i].x &&
mouseX <= this.upgradeTopLeft[i].x + this.upgradeWidth &&
mouseY >= this.upgradeTopLeft[i].y &&
mouseY <= this.upgradeTopLeft[i].y + this.upgradeHeight
) {
return true;
}
}
return false;
};