-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit.js
276 lines (259 loc) · 8.4 KB
/
unit.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
var Unit = function(game, units, spritename,
bulletsprite, numbullets, muzzlex, muzzley,
diesnd,
speed, attackstr, attacksnd, attackspeed, range, health, pos, team) {
this.sprite = game.add.sprite(pos.x, pos.y, spritename);
game.physics.arcade.enable(this.sprite);
this.sprite.anchor.x = 0.5;
this.sprite.anchor.y = 1;
this.sprite.body.width = this.sprite.width * 0.7;
this.sprite.unit = this;
units.add(this.sprite);
this.name = spritename;
this.emitter = game.add.emitter(0, 0, numbullets);
this.emitter.makeParticles(bulletsprite);
this.emitter.gravity = 0;
this.emitter.setRotation(0, 0);
this.team = team;
// Flip around if not player's units
if (team !== 'player') {
this.sprite.scale.x = -1;
}
var sounds = {
fire: game.add.audio(attacksnd),
die: game.add.audio(diesnd)
};
this.sprite.health = health;
this.healthbar = new Healthbar(game);
if (attackstr === 0) {
this.manabar = new Manabar(game);
}
this.isSelected = false;
this.frameul = game.add.sprite(0, 0, 'frame');
this.frameur = game.add.sprite(0, 0, 'frame');
this.frameur.angle = 90;
this.framedl = game.add.sprite(0, 0, 'frame');
this.framedl.angle = 270;
this.framedr = game.add.sprite(0, 0, 'frame');
this.framedr.angle = 180;
this.frameGroup = game.add.group();
this.frameGroup.add(this.frameul);
this.frameGroup.add(this.frameur);
this.frameGroup.add(this.framedl);
this.frameGroup.add(this.framedr);
this.setSelected = function(selected) {
this.frameGroup.visible = selected;
if (selected) {
this.frameGroup.alpha = 1;
}
this.isSelected = selected;
};
this.isHover = false;
this.hover = function() {
this.isHover = true;
if (!this.isSelected) {
// Show selection frame
this.frameGroup.visible = true;
this.frameGroup.alpha = 0.5;
}
};
this.forceMove = false;
this.dest = pos.x;
this.moveTo = function(x) {
this.dest = x;
if (this.target) {
this.forceMove = true;
} else {
this.forceMove = false;
}
this.target = null;
};
this.attackCounter = 0;
this.target = null;
this.attack = function(target) {
if (attackstr === 0) {
return;
}
this.target = target;
this.dest = null;
};
this.oreDeposited = 0;
this.ore = 0;
this.update = function(units, buildings, ores) {
var i;
var overlap;
if (!this.isHover && !this.isSelected) {
this.frameGroup.visible = false;
}
this.healthbar.update(this.sprite, this.sprite.health / health);
if (attackstr === 0) {
this.manabar.update(this.sprite, this.ore / 1000);
}
this.frameul.x = this.sprite.body.x;
this.frameul.y = this.sprite.body.y;
this.frameur.x = this.sprite.body.x + this.sprite.body.width;
this.frameur.y = this.sprite.body.y;
this.framedl.x = this.sprite.body.x;
this.framedl.y = this.sprite.body.y + this.sprite.body.height;
this.framedr.x = this.sprite.body.x + this.sprite.body.width;
this.framedr.y = this.sprite.body.y + this.sprite.body.height;
this.isHover = false;
if (this.team === 'player') {
// follow player orders
} else {
// Basic AI
// Move towards player's end, attack along the way
this.dest = 0;
}
// Special AI for harvesters:
// Move towards opposite end and look for ore if not full
// Return if full
if (attackstr === 0) {
if (this.ore >= 1000) {
if (this.team !== 'player') {
this.dest = game.world.bounds.width;
} else {
this.dest = 0;
}
} else {
if (this.team !== 'player') {
this.dest = 0;
} else {
this.dest = game.world.bounds.width;
}
}
// Check for ore
if (this.ore < 1000) {
if (this.target === null || !this.target.alive) {
var closestOre = -1;
for (i = 0; i < ores.length; i++) {
var ore = ores.getAt(i);
var dist = Math.abs(ore.x - this.sprite.x);
if ((closestOre === -1 || dist < closestOre) &&
ore.alive) {
closestOre = dist;
this.target = ore;
this.dest = ore.x;
}
}
}
if (this.target && this.target.alive) {
this.dest = this.target.x;
overlap = this.target.x - 10 < this.sprite.x &&
this.target.x + 10 > this.sprite.x;
if (overlap) {
this.target.damage(1);
this.ore++;
}
}
} else {
// Check for refinery
for (i = 0; i < buildings.length; i++) {
var building = buildings[i];
overlap = building.sprite.x - 10 < this.sprite.x &&
building.sprite.x + 10 > this.sprite.x;
if (overlap && building.team == this.team && building.sprite.alive &&
building.isRefinery){
this.oreDeposited = this.ore;
this.ore = 0;
break;
}
}
}
}
// Stop and attack
if (!this.forceMove && attackstr > 0) {
if (this.target === null) {
// Find a unit to attack
for (i = 0; i < units.length; i++) {
var unit = units[i];
overlap = unit.sprite.x - range * 1.3 < this.sprite.x &&
unit.sprite.x + range * 1.3 > this.sprite.x;
if (overlap && unit.team != this.team && unit.sprite.alive) {
this.attack(unit);
break;
}
}
if (this.target === null) {
// attack buildings
for (i = 0; i < buildings.length; i++) {
var building = buildings[i];
overlap = building.sprite.x - range < this.sprite.x &&
building.sprite.x + range > this.sprite.x;
if (overlap && building.team != this.team && building.sprite.alive){
this.attack(building);
break;
}
}
}
} else {
if (this.target.sprite.alive) {
overlap = this.target.sprite.x - range < this.sprite.x &&
this.target.sprite.x + range > this.sprite.x;
if (!overlap) {
this.dest = this.target.sprite.x;
} else {
this.dest = null;
// Turn to face the adversary
if (this.target.sprite.x > this.sprite.x) {
this.sprite.scale.x = 1;
} else {
this.sprite.scale.x = -1;
}
// firing
if (this.attackCounter <= 0) {
this.attackCounter = 100;
var volume = (2000 - Math.abs(this.sprite.x - game.camera.x)) / 2000;
if (volume > 0) {
sounds.fire.play('', 0, volume);
}
// Halve attack str if ineffective
if ((this.name.indexOf('marine') === 0 && this.target.name.indexOf('jeep') === 0) ||
(this.name.indexOf('jeep') === 0 && this.target.name.indexOf('tank') === 0) ||
(this.name.indexOf('tank') === 0 && this.target.name.indexOf('marine') === 0)) {
this.target.sprite.damage(attackstr / 2);
} else {
this.target.sprite.damage(attackstr);
}
this.emitter.x = this.sprite.x + muzzlex * this.sprite.scale.x;
this.emitter.y = this.sprite.y + muzzley;
var xspeed = 1500.0 * this.sprite.scale.x;
this.emitter.setXSpeed(xspeed, xspeed);
this.emitter.setYSpeed(-1, 1);
this.emitter.start(false, 180, 20, numbullets);
}
}
} else {
this.target = null;
}
}
}
this.attackCounter--;
// Move towards destination at speed
if (this.dest !== null) {
if (this.dest > this.sprite.x + speed) {
this.sprite.scale.x = 1;
this.sprite.x += speed;
} else if (this.dest < this.sprite.x - speed) {
this.sprite.scale.x = -1;
this.sprite.x -= speed;
} else {
this.forceMove = false;
}
}
};
this.kill = function() {
this.healthbar.destroy();
if (this.manabar) {
this.manabar.destroy();
}
this.frameGroup.removeAll();
this.emitter.destroy();
units.remove(this.sprite);
var volume = (2000 - Math.abs(this.sprite.x - game.camera.x)) / 2000;
if (volume > 0) {
sounds.die.play('', 0, volume);
}
};
this.setSelected(false);
};