-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
301 lines (250 loc) · 6.58 KB
/
objects.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
function extend(ChildClass, ParentClass) {
var parent = new ParentClass();
ChildClass.prototype = parent;
ChildClass.prototype.super = parent.constructor;
ChildClass.prototype.constructor = ChildClass;
}
function collision(obj1, obj2) {
return distance_between(obj1, obj2) < (obj1.radius + obj2.radius);
}
function distance_between(obj1, obj2) {
return Math.sqrt(Math.pow(obj1.x_coord - obj2.x_coord, 2) + Math.pow(obj1.y_coord - obj2.y_coord, 2));
}
/*Mass Object - Because everything has mass*/
function Mass(radius, x, y, x_speed, y_speed) {
this.radius = radius;
this.x_coord = x || 0;
this.y_coord = y || 0;
this.x_speed = x_speed || 0;
this.y_speed = y_speed || 0;
}
/*Ship Object*/
function Ship(x, y, speed, radius, weapon_power) {
this.speed = 200 || speed;
this.super(radius, x, y, this.speed, 0);
this.right_thruster = false;
this.left_thruster = false;
this.radius = radius || 0;
this.weapon_reload_time = 0.8; // seconds
this.time_until_reloaded = this.weapon_reload_time;
this.alive = true;
this.time_dead = 0;
}
extend(Ship, Mass);
Ship.prototype.draw = function(c) {
c.save();
var img = new Image();
if (this.alive) {
img.src = "./assets/images/tank.png";
}
else
img.src = "./assets/images/tank_destroyed.png";
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}
Ship.prototype.projectile = function(elapsed) {
var p = new Projectile(10, this.x_coord, this.y_coord, this.radius, -1, true);
this.time_until_reloaded = this.weapon_reload_time;
return p;
}
Ship.prototype.update = function(elapsed, c) {
if (this.alive == false) {
this.time_until_reloaded = 0.8;
return;
}
if(this.time_until_reloaded > 0) {
this.time_until_reloaded -= Math.min(elapsed, this.time_until_reloaded);
}
if (this.right_thruster) {
if (this.x_coord + 24 + this.radius >= c.canvas.width) {
return;
}
this.x_coord = this.x_coord + (this.x_speed * elapsed);
}
else if (this.left_thruster) {
if (this.x_coord - 24 <= 0) {
return;
}
this.x_coord = this.x_coord - (this.x_speed * elapsed);
}
}
/*Project Object*/
function Projectile(radius, x, y, ship_radius, direction, tb) {
this.speed = 800;
this.width = 2;
this.height = 10;
this.direction = direction || -1;
this.tank_bullet = tb || false;
this.super(this.height / 2, x, y, this.speed, ship_radius);
}
extend(Projectile, Mass);
Projectile.prototype.update = function(elapsed, c) {
this.y_coord = this.y_coord + (this.speed * elapsed) * this.direction;
}
Projectile.prototype.draw = function(c, guide) {
c.save();
c.translate(this.x_coord - 1.2, this.y_coord + 15);
draw_projectile(c, 13, this.width, this.height);
c.restore();
}
/*Alien Object*/
function Alien(x, y, type, speed) {
var radius = 0;
switch (type)
{
case 0:
radius = 8;
break;
case 1:
radius = 11;
break;
case 2:
radius = 12;
break;
case 3:
radius = 24;
break;
}
this.speed = speed || 20;
this.can_shoot = false;
this.type = type || 0;
this.state = 0;
this.speed_increase = 5;
this.time_state = 0;
this.time_dead = 0;
this.alien_dir = 1;
this.retaliate_rate = 50;
this.time_until_shoot = Math.floor((Math.random() * 12) + 7);
this.last_in_row = false;
this.super(radius, x, y, this.speed, 0);
}
extend(Alien, Mass);
Alien.prototype.draw = function(c) {
c.save();
var img = new Image();
img.src = "./assets/images/alien_" + this.type + "_a" + this.state + ".png";
if (this.state > 1) {
img.src = "./assets/images/alien_destroyed.png";
}
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}
Alien.prototype.projectile = function(elapsed) {
var p = new Projectile(10, this.x_coord, this.y_coord + 20, this.radius, 1);
return p;
}
Alien.prototype.update = function(elapsed, c) {
this.time_state += elapsed * (this.speed / 10);
if (this.time_state > 1.1) {
if (this.state == 0) {
this.state = 1;
}
else if (this.state == 1) {
this.state = 0;
}
this.time_state = 0;
}
if (elapsed > 1)
return;
this.x_coord = this.x_coord + (this.speed * elapsed) * this.alien_dir;
}
/*Red Spaceship object*/
function SpaceShip(x, y, speed, direction) {
this.speed = speed || 20;
this.alive = true;
this.time_dead = 0;
this.speed_increase = 1;
this.ship_dir = direction || 1;
this.super(24, x, y, this.speed, 0);
}
extend(SpaceShip, Mass);
SpaceShip.prototype.draw = function(c) {
c.save();
var img = new Image();
img.src = "./assets/images/space_ship.png";
if (!this.alive) {
img.src = "./assets/images/alien_destroyed.png";
}
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}
SpaceShip.prototype.update = function(elapsed, c) {
if (elapsed > 1)
return;
this.x_coord = this.x_coord + (this.speed * elapsed) * this.ship_dir;
}
/*Left Barrier Object*/
function BarrierLeft(x, y) {
this.hits = 1;
this.radius = 0;
this.super(7, x, y, this.speed, 0);
}
extend(BarrierLeft, Mass);
BarrierLeft.prototype.draw = function(c) {
c.save();
var img = new Image();
if (this.hits < 3) {
img.src = "./assets/images/barrier_l_0.png"; //1, 2 = not damaged
}
else if (this.hits == 3 || this.hits == 4) { // 3, 4 = slightly damaged
img.src = "./assets/images/barrier_l_1.png";
}
else if (this.hits == 5 || this.hits == 6) { // 5, 6 = heavily damaged
img.src = "./assets/images/barrier_l_2.png";
}
else if (this.hits > 6) {
img.src = "";
}
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}
/*Middle Barrier Object*/
function BarrierMiddle(x, y) {
this.hits = 1;
this.radius = 0;
this.super(7, x, y, this.speed, 0);
}
extend(BarrierMiddle, Mass);
BarrierMiddle.prototype.draw = function(c) {
c.save();
var img = new Image();
if (this.hits < 3) {
img.src = "./assets/images/barrier_m_0.png";
}
else if (this.hits == 3 || this.hits == 4) {
img.src = "./assets/images/barrier_m_1.png";
}
else if (this.hits == 5 || this.hits == 6) {
img.src = "./assets/images/barrier_m_2.png";
}
else if (this.hits > 6) {
img.src = "";
}
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}
/*Right Barrier Object*/
function BarrierRight(x, y) {
this.hits = 1;
this.radius = 0;
this.super(7, x, y, this.speed, 0);
}
extend(BarrierRight, Mass);
BarrierRight.prototype.draw = function(c) {
c.save();
var img = new Image();
if (this.hits < 3) {
img.src = "./assets/images/barrier_r_0.png";
}
else if (this.hits == 3 || this.hits == 4) {
img.src = "./assets/images/barrier_r_1.png";
}
else if (this.hits == 5 || this.hits == 6) {
img.src = "./assets/images/barrier_r_2.png";
}
else if (this.hits > 6) {
img.src = "";
}
c.drawImage(img, this.x_coord, this.y_coord);
c.restore();
}