-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplanet.js
80 lines (68 loc) · 1.72 KB
/
planet.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
/* exported planet */
/* global bigExplosion,smallExplosion, ressources,moon,drawAt,UI,engine,drawnEntity,mergeObject */
"use strict";
var planet = function (settings) {
var ini = {
type:'planet',
radius:32,
mass:100,
hpMax:100,
hp:100,
fireRate:0.5,
coolDown:0,
missileRadius:200,
frames:ressources.planet
};
settings = mergeObject(settings,ini);
var planet = drawnEntity(settings);
planet.moons = [];
var hPBarResolution = 4; // to counter scaling effect
//*
var superUpdate = planet.update;
planet.update = function(dt) {
superUpdate.call(this,dt);
this.coolDown = Math.max(0,this.coolDown-dt);
};
//*/
planet.draw = function(ctx) {
var self = this;
drawAt(ctx,this.position,0,function(ctx) {
ctx.globalCompositeOperation = 'lighter'
UI.drawHPBar(ctx,self.hpBar,hPBarResolution);
});
drawAt(ctx,this.position,this.angle,function(ctx) {
self.animation.draw(ctx,
-self.radius,-self.radius,self.radius*2,self.radius*2);
});
};
planet.addMoon = function(settings) {
return moon(settings,this);
};
planet.updateHPBar = function() {UI.circleHPBar(this,hPBarResolution);};
planet.updateHPBar();
planet.hit = function(damage) {
this.hp -= damage;
engine.finance.hit(damage);
this.updateHPBar();
if(this.hp <= 0) {
playSoundInstance(sounds['lose']);
this.destroy();
}
};
var superDestroy = planet.destroy;
planet.destroy = function() {
bigExplosion({
layerName : this.layerName,
position:this.position
});
// destroy all the moons appended to the planet
while(planet.moons.length>0) {
planet.moons.pop().destroy();
}
engine.killable.erase(this);
superDestroy.call(this);
engine.lose();
};
engine.killable.push(planet);
return planet;
};