This repository was archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplosion.js
More file actions
59 lines (57 loc) · 1.9 KB
/
explosion.js
File metadata and controls
59 lines (57 loc) · 1.9 KB
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
var Explosive = function(damage, expansionRate, maxRadius){
this.damage=damage;
this.expansionRate=expansionRate;
this.maxRadius=maxRadius;
}
var Explosion = function(x, y, damageMultiplier=2, expansionRate = 5,
maxRadius = 50, startRadius = 1,
color="#FF0000"){
this.color = color;
this.type = 'e'; //explosion
this.hitbox = new Circle(x, y, startRadius,
0, 0, 0, 0); //radius starts at 1 typically
this.hitbox.mass=100000;
this.expansionRate = expansionRate;
this.maxRadius = maxRadius;
this.duration= maxRadius / expansionRate; // in game frames
this.finished = false;
this.damageMultiplier=damageMultiplier;
this.expandRadius = function(){
if (this.hitbox.radius < this.maxRadius){
this.hitbox.radius += this.expansionRate;
}
}
this.decreaseDuration= function(){
this.duration--;
if (this.duration == 0){
this.finished = true;
}
}
this.draw = function(){
ctx.beginPath();
ctx.fillStyle=this.color;
ctx.arc(this.hitbox.position.x, this.hitbox.position.y,
this.hitbox.radius, 0, 2 *Math.PI);
ctx.fill();
}
this.fetchDamage = function(affectedHitbox){
var damage = this.hitbox.radius - distance(affectedHitbox.center,
this.hitbox.position);
damage *= this.damageMultiplier;
damage = Math.round(Math.abs(damage));
return damage;
}
}
function finishExplosions(explosions){
for (var i = 0; i < explosions.length; i++){
if (explosions[i].finished){
explosions.splice(i, 1);
}
}
}
function updateExplosions(explosions){
for (var i = 0; i < explosions.length; i++){
explosions[i].expandRadius();
explosions[i].decreaseDuration();
}
}