-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpowerups.js
82 lines (64 loc) · 1.83 KB
/
powerups.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
// In every powerup*() function, `obj` is Hero.
function powerupGlitchPlus(obj) {
obj.glitchUp(random(10, 20));
}
function powerupSpeedUp(obj) {
obj.speed = Math.floor(obj.defaultSpeed * 1.6);
gameTimers.setTimeout('powerupSU', function () {
obj.speed = obj.defaultSpeed;
}, 7000);
}
function powerupDoubleDamage(obj) {
obj.damageFactor = 2;
gameTimers.setTimeout('powerupDD', function () {
obj.damageFactor = 1;
}, 7000);
}
function Powerup(x, y) {
var self = this,
effectArray,
sprite;
self.id = getUniqueID();
self.creationTime = currentTime;
// Choose a powerup randomly.
effectArray = Powerup.EFFECTS[random(0, Powerup.EFFECTS.length - 1)];
self.effect = effectArray[0];
sprite = effectArray[1];
self.animation = new Animation(sprite);
self.x = x;
self.y = y;
self.w = sprite.w * PIXEL_SIZE;
self.h = sprite.h * PIXEL_SIZE;
self.hp = 3;
self.gp = 0; // To not hit Hero.
level.units[self.id] = self;
}
Powerup.prototype.tick = function () {
if (currentTime - this.creationTime >= 5000) {
this.absorb(); // Remove after 5 seconds.
}
};
// Powerup can be destroyed.
Powerup.prototype.hit = function () {
this.hp--;
if (!this.hp) this.absorb();
};
// Avoid visual bumping, just destroy instead.
Powerup.prototype.bump = function (obj) {
this.absorb(obj);
};
Powerup.prototype.absorb = function (obj) {
var self = this;
obj == hero && self.effect(obj);
gameTimers.setTimeout(function () {
delete level.units[self.id];
}, 0);
};
Powerup.prototype.getAnimationFrame = function () {
return this.animation.getFrame(isOdd ? 0 : 1);
};
Powerup.EFFECTS = [
[powerupGlitchPlus, SPRITES.POWERUP_GP],
[powerupSpeedUp, SPRITES.POWERUP_SU],
[powerupDoubleDamage, SPRITES.POWERUP_DD]
];