-
Notifications
You must be signed in to change notification settings - Fork 1
/
water-bar.js
70 lines (62 loc) · 2.16 KB
/
water-bar.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
'use strict';
class WaterBar {
constructor (scale, posX, posY, defaultColor) {
this.maxValue = 100;
this.curValue = this.maxValue;
this.maskOffsetStart = 23;
this.maskOffsetEnd = -30;
this.mask = new PIXI.Graphics();
this.backgroundSprite = game.add.sprite(posX, posY, 'life_bar_background', null, waveAttack.uiGroup);
this.backgroundSprite.scale.setTo(scale, scale);
this.backgroundSprite.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
this.middleSprite = game.add.sprite(posX, posY, 'life_bar_middle', null, waveAttack.uiGroup);
this.middleSprite.scale.setTo(scale, scale);
this.middleSprite.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
this.middleSprite.animations.add('default');
this.middleSprite.animations.play('default', 15, true);
this.middleSprite.tint = defaultColor;
this.calcMask();
this.borderSprite = game.add.sprite(posX, posY, 'life_bar_border', null, waveAttack.uiGroup);
this.borderSprite.scale.setTo(scale, scale);
this.borderSprite.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
}
calcMask () {
var perc = this.curValue / this.maxValue;
this.mask.position.x = 0;
this.mask.position.y = 0;
this.mask.clear();
this.mask.beginFill(0, 1);
this.mask.moveTo(this.maskOffsetStart, 0);
this.mask.lineTo(this.maskOffsetStart + (this.middleSprite.width + this.maskOffsetEnd) * perc, 0);
this.mask.lineTo(this.maskOffsetStart + (this.middleSprite.width + this.maskOffsetEnd) * perc, this.middleSprite.height);
this.mask.lineTo(this.maskOffsetStart, this.middleSprite.height);
this.mask.lineTo(this.maskOffsetStart, 0);
this.mask.endFill();
this.middleSprite.mask = this.mask;
}
set tint (value) {
this.middleSprite.tint = value;
}
removeWater (value) {
this.curValue -= value;
if (this.curValue < 0)
this.curValue = 0;
}
addWater (value) {
this.curValue += value;
if (this.curValue > this.maxValue)
this.curValue = this.maxValue;
}
update (deltaTime) {
if (waveAttack.waveUp) {
this.removeWater(5 * deltaTime);
} else {
this.removeWater(1 * deltaTime);
}
this.calcMask();
if (this.curValue == 0)
{
waveAttack.gameState = GameState.GAMEOVER;
}
}
}