-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.js
371 lines (294 loc) · 11.6 KB
/
ship.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var ship = {
p_player:true,
p_direction: 0,
// power forward, back, left, right
p_power: [100 , -30, -0.01, 0.01],
p_spin: 0,
p_hpregenamount: 0.002,
p_hpregencumative: 0,
p_maxhpregen: 0.8,
p_money: 1000,
p_goods: 50,
p_goodplanet: 0,
p_maxhp: 100,
p_hp: 100,
p_armour: 50,
p_maxarmour: 50,
p_invunrable: 160,
//Status: 0 fine, 9 destroyed
p_status: 0,
p_fuel: 10000,
p_maxfuel:10000,
// One means completly random, any number higher adds a minimum
p_armourpower: 1,
create: function(){
var obj = Object.create(this);
//12680000
obj.physical = particle.create("ship", 10300, 9000000, 10, 10);
obj.weapon = weapon.create("machinegun", 50, 200, 500, 4000, 300, 20, 0.9);
obj.ship = obj;
return obj;
},
update: function(time){
if(this.p_invunrable > 0){this.p_invunrable--;}
this.physical.update(time);
this.p_direction += (this.p_spin * time);
if(this.p_direction > Math.PI){this.p_direction = -Math.PI;}
if(this.p_direction < -Math.PI){this.p_direction = Math.PI;}
if(this.p_hp <= 0){this.destroy();}
//If hp is less than max
if((this.p_hp + 1) < (this.p_maxhp * this.p_maxhpregen)){
//add the regen amount
this.p_hpregencumative += this.p_hpregenamount;
//If reached one add it back on
if(this.p_hpregencumative >= 1){
this.p_hp++;
this.p_hpregencumative = 0;
}
}
return this.p_status;
},
draw: function() {
//this has to be here, otherwise the shooting does not line up as all the ships have not updated
this.weapon.update();
game.getcontext().fillStyle= '#' + '900';
game.getcontext().beginPath();
//https://en.wikipedia.org/wiki/Circle#Equations
game.getcontext().moveTo(this.physical.getx() + game.screen.x + (this.physical.getradius() * Math.cos(this.p_direction)) , this.physical.gety() + game.screen.y + (this.physical.getradius() * Math.sin(this.p_direction)));
game.getcontext().lineTo(this.physical.getx() + game.screen.x + (this.physical.getradius() * Math.cos(this.p_direction + 2.4)) , this.physical.gety() + game.screen.y + (this.physical.getradius() * Math.sin(this.p_direction + 2.4)));
game.getcontext().lineTo(this.physical.getx() + game.screen.x + (this.physical.getradius() * Math.cos(this.p_direction + 3.8)) , this.physical.gety() + game.screen.y + (this.physical.getradius() * Math.sin(this.p_direction + 3.8)));
game.getcontext().closePath();
game.getcontext().fill();
this.weapon.draw(this);
},
left: function(){
if(this.p_fuel > 0){
this.spin(false);
this.removefuel(this.p_power[2]);
}
},
right: function(){
if(this.p_fuel > 0){
this.spin(true);
this.removefuel(this.p_power[3]);
}
},
up: function(){
if(this.p_fuel > 0){
this.forward(this.p_power[0]);
this.removefuel(this.p_power[0]);
if(this.p_player){
game.audio.engine.play();
game.audio.engine.volume = 0.2 * game.audio.getsoundvol();
}
}
},
down: function(){
if(this.p_fuel > 0){
this.forward(this.p_power[1]);
this.removefuel(this.p_power[1]);
}
},
spin: function(right){
var spinner = right ? this.p_power[3] : this.p_power[2];
this.p_spin += (spinner / this.physical.getmass());
if(this.p_spin > 4.5){this.p_spin = 4.5;}
if(this.p_spin < -4.5){this.p_spin = -4.5}
},
forward: function(power){
this.physical.addforce(power * Math.cos(this.p_direction) , power * Math.sin(this.p_direction));
},
collided: function(obj){
if(this.physical.havecollided(obj)){
//Does damage equal to this value
var speedhit = calculate_distance(this.physical.getxspeed(), obj.getxspeed(), this.physical.getyspeed(), obj.getyspeed());
if(speedhit > 10){
this.damage(Math.pow(speedhit, 1.3) - 19);
var musicdist = calculate_distance(this.physical.getx() , game.getplayer().physical.getx(), this.physical.gety(), game.getplayer().physical.gety());
var musicvolume = ((-(Math.pow(Math.abs(musicdist), 1.1)))/10000) +1;
if(musicvolume < 0){game.audio.slap.volume = game.audio.getsoundvol() * musicvolume * 0.2;}
game.audio.slap.volume = game.audio.slap.volume * game.audio.getsoundvol();
game.audio.slap.play();
}
this.physical.collided(obj);
}
},
getrotation: function(){
return this.p_direction;
},
shoot: function() {
this.weapon.trigger(this);
},
destroy: function() {
var musicdist = calculate_distance(this.physical.getx() , game.getplayer().physical.getx(), this.physical.gety(), game.getplayer().physical.gety());
var musicvolume = ((-(Math.pow(Math.abs(musicdist), 1.1)))/10000) +1;
if(musicvolume < 0){musicvolume = 0;}
game.audio.explosion.volume = game.audio.getsoundvol() * musicvolume;
game.audio.explosion.volume = game.audio.explosion.volume * game.audio.getsoundvol();
game.audio.explosion.play();
this.p_status = 9;
},
damage: function(damage){
if(this.p_invunrable > 0){return this.p_hp;}
var armoureffect = Math.min(((Math.random() * (1/this.p_armourpower)) + ((this.p_armourpower - 1)/this.p_armourpower)) * this.p_armour, damage);
this.p_armour -= armoureffect;
this.p_hp -= (damage - armoureffect);
return this.p_hp;
},
gethp: function(){
return this.p_hp;
},
getarmour: function(){
return this.p_armour;
},
getfuel: function(){
return this.p_fuel;
},
removefuel: function(power){
this.p_fuel -= (Math.abs(power)/100);
},
getmoney: function(){
return this.p_money;
},
addmoney: function(newmoney){
this.p_money += newmoney;
}
};
var weapon = {
p_type: 0, //Weapon Type (machinegun, laser, rocket, sniper)
p_magrounds: 0, // Rounds in magazine
p_firetime: 0, // Time betweenshots
p_ammo: 0, // Total ammo
p_reloadtime: 0, // reload time
p_maxdistance: 0, //distance rounds fires
p_power: 0, //damage
p_accuraccy: 0, //accuraccy
p_draw: 0, // internal draw varible
p_currentmag: 0, //Players mag ammo
p_currentammo: 0, // Players ammo
p_currentreloadreadytime: 0, //If time is after this then ready
p_currentshootagaintime: 0, //time till they can shoot again
p_shootpressed: 0,
create: function(type, magrounds, firetime, ammo, reloadtime, maxdistance, power, accuraccy){
var obj = Object.create(this);
obj.p_type = type;
obj.p_magrounds = obj.p_currentmag = magrounds;
obj.p_firetime = firetime;
obj.p_ammo = obj.p_currentammo = ammo;
obj.p_reloadtime = reloadtime;
obj.p_maxdistance = maxdistance;
obj.p_power = power;
obj.p_accuraccy = accuraccy;
obj.p_currentreloadreadytime = obj.p_currentshootagaintime = game.gettime();
return obj;
},
draw: function(ship){
//moved to draw so lines up
if(this.p_shootpressed != 0){
this.shoot(this.p_shootpressed);
this.p_shootpressed = 0;
}
if(this.p_draw != 0){
game.getcontext().lineWidth = 2;
game.getcontext().strokeStyle= '#' + '0f0'; // 'ddb';
game.getcontext().beginPath();
game.getcontext().moveTo(ship.physical.getx() + game.screen.x + (ship.physical.getradius() * Math.cos(ship.p_direction)) , ship.physical.gety() + game.screen.y + (ship.physical.getradius() * Math.sin(ship.p_direction)));
game.getcontext().lineTo(game.screen.x + this.p_draw.x, game.screen.y + this.p_draw.y);
game.getcontext().stroke();
this.p_draw = 0;
}
},
gettype:function(){
return this.p_type;
},
trigger:function(ship){
this.p_shootpressed = ship;
},
update:function(){
},
shoot: function(ship){
if(this.reload() != 1){return 0;} //Still reloading or now reloading
//Now checks time to shoot has passed
if(this.p_currentshootagaintime > game.gettime()){return 0;}
//console.log(this.p_currentmag + " " + this.p_currentammo);
//Adds reshoot time
this.p_currentshootagaintime = game.gettime() + this.p_firetime;
//shoot noise
var musicdist = calculate_distance(ship.physical.getx() , game.getplayer().physical.getx(), ship.physical.gety(), game.getplayer().physical.gety());
var musicvolume = ((-(Math.pow(Math.abs(musicdist), 1.1)))/10000) +1;
if(musicvolume < 0){game.audio.shoot.volume = game.audio.getsoundvol() * musicvolume * 0.2;}
game.audio.shoot.volume = game.audio.shoot.volume * game.audio.getsoundvol();
game.audio.shoot.play();
// Removes one from current mag
this.p_currentmag--;
//Needs to do do damage
var shipf = {x: ship.physical.getx() + (ship.physical.getradius() * Math.cos(ship.p_direction)), y: ship.physical.gety() + (ship.physical.getradius() * Math.sin(ship.p_direction))};
var shot = {x: ship.physical.getx() + (this.p_maxdistance * (Math.cos(ship.p_direction) + this.accuracyeffect())), y:ship.physical.gety() + (this.p_maxdistance * (Math.sin(ship.p_direction) + this.accuracyeffect()))};
this.p_draw = {x: shot.x, y:shot.y};
var closestobj = {dist: 10000000000, obj: 0};
for(var i = game.p_objects.length - 1; i >= 0; i--) {
if(typeof game.p_objects[i] === 'undefined'){continue;}
if(game.p_objects[i].physical.gettype() != "ship"){continue;}
var object = {x: game.p_objects[i].physical.getx(), y: game.p_objects[i].physical.gety()};
//Tests if the ship is within the distance the ship can shoot
if(calculate_distance(shipf.x, object.x, shipf.y, object.y) > calculate_distance(shipf.x, shot.x, shipf.y, shot.y)){continue;}
//Now tests if the ship is within the circle of the endpoint
if(calculate_distance(shipf.x, shot.x, shipf.y, shot.y) < calculate_distance(object.x, shot.x, object.y, shot.y)){continue;}
//https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line - based off
//However this does not check that its on the right side so above check avoids that
var dist = Math.abs(((((shot.y - shipf.y) * object.x) - ((shot.x - shipf.x) * object.y)) + (shot.x * shipf.y)) - (shot.y * shipf.x)) / Math.sqrt(Math.pow((shot.y - shipf.y), 2) + Math.pow((shot.x - shipf.x) , 2));
var damage = (Math.random() * (this.p_power/2)) + (this.p_power/2);
if(dist < game.p_objects[i].physical.getradius()){
var tempdist = calculate_distance(shipf.x, object.x, shipf.y, object.y);
if(tempdist < closestobj.dist){
closestobj.dist = tempdist;
closestobj.obj = game.p_objects[i];
}
}
}
if(closestobj.obj != 0){
this.p_draw = {x: closestobj.obj.physical.getx(), y:closestobj.obj.physical.gety()};
if(closestobj.obj.ship.damage(damage) < 10){
ship.addmoney(closestobj.obj.ship.physical.getmass());
}
}
},
reload: function(){
//This would mean its still reloading.
if(game.gettime() <= this.p_currentreloadreadytime){
return 0;
}
//means a reload is needed as no ammo
if(this.p_currentmag == 1){
this.p_currentreloadreadytime = game.gettime() + this.p_reloadtime;
this.p_currentmag = 0;
return 1;
}
//times past and reload can happen
if(this.p_currentmag <= 0){
var amountofrounds = Math.min(this.p_currentammo, this.p_magrounds);
this.p_currentmag = amountofrounds;
this.p_currentammo -= amountofrounds;
if(this.p_currentmag == 0){
return 0;
}
}
return 1;
},
manualreload: function(){
this.p_currentammo += this.p_currentmag;
this.p_currentmag = 0;
this.p_currentreloadreadytime = game.gettime() + this.p_reloadtime;
},
accuracyeffect: function(){
var accuracytop = (1 - this.p_accuraccy);
var accuracybottom = 0 - (1 - this.p_accuraccy);
return ((Math.random() * accuracytop * 2) + accuracybottom);
},
gettype: function(){
return this.p_type;
},
getammo: function(){
return this.p_currentmag + "/" + this.p_currentammo;
}
}