-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSprite_Damage.js
119 lines (104 loc) · 3.28 KB
/
Sprite_Damage.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
//-----------------------------------------------------------------------------
// Sprite_Damage
//
// The sprite for displaying a popup damage.
function Sprite_Damage() {
this.initialize.apply(this, arguments);
}
Sprite_Damage.prototype = Object.create(Sprite.prototype);
Sprite_Damage.prototype.constructor = Sprite_Damage;
Sprite_Damage.prototype.initialize = function() {
Sprite.prototype.initialize.call(this);
this._duration = 90;
this._flashColor = [0, 0, 0, 0];
this._flashDuration = 0;
this._damageBitmap = ImageManager.loadSystem('Damage');
};
Sprite_Damage.prototype.setup = function(target) {
var result = target.result();
if (result.missed || result.evaded) {
this.createMiss();
} else if (result.hpAffected) {
this.createDigits(0, result.hpDamage);
} else if (target.isAlive() && result.mpDamage !== 0) {
this.createDigits(2, result.mpDamage);
}
if (result.critical) {
this.setupCriticalEffect();
}
};
Sprite_Damage.prototype.setupCriticalEffect = function() {
this._flashColor = [255, 0, 0, 160];
this._flashDuration = 60;
};
Sprite_Damage.prototype.digitWidth = function() {
return this._damageBitmap ? this._damageBitmap.width / 10 : 0;
};
Sprite_Damage.prototype.digitHeight = function() {
return this._damageBitmap ? this._damageBitmap.height / 5 : 0;
};
Sprite_Damage.prototype.createMiss = function() {
var w = this.digitWidth();
var h = this.digitHeight();
var sprite = this.createChildSprite();
sprite.setFrame(0, 4 * h, 4 * w, h);
sprite.dy = 0;
};
Sprite_Damage.prototype.createDigits = function(baseRow, value) {
var string = Math.abs(value).toString();
var row = baseRow + (value < 0 ? 1 : 0);
var w = this.digitWidth();
var h = this.digitHeight();
for (var i = 0; i < string.length; i++) {
var sprite = this.createChildSprite();
var n = Number(string[i]);
sprite.setFrame(n * w, row * h, w, h);
sprite.x = (i - (string.length - 1) / 2) * w;
sprite.dy = -i;
}
};
Sprite_Damage.prototype.createChildSprite = function() {
var sprite = new Sprite();
sprite.bitmap = this._damageBitmap;
sprite.anchor.x = 0.5;
sprite.anchor.y = 1;
sprite.y = -40;
sprite.ry = sprite.y;
this.addChild(sprite);
return sprite;
};
Sprite_Damage.prototype.update = function() {
Sprite.prototype.update.call(this);
if (this._duration > 0) {
this._duration--;
for (var i = 0; i < this.children.length; i++) {
this.updateChild(this.children[i]);
}
}
this.updateFlash();
this.updateOpacity();
};
Sprite_Damage.prototype.updateChild = function(sprite) {
sprite.dy += 0.5;
sprite.ry += sprite.dy;
if (sprite.ry >= 0) {
sprite.ry = 0;
sprite.dy *= -0.6;
}
sprite.y = Math.round(sprite.ry);
sprite.setBlendColor(this._flashColor);
};
Sprite_Damage.prototype.updateFlash = function() {
if (this._flashDuration > 0) {
var d = this._flashDuration--;
this._flashColor[3] *= (d - 1) / d;
}
};
Sprite_Damage.prototype.updateOpacity = function() {
if (this._duration < 10) {
this.opacity = 255 * this._duration / 10;
}
};
Sprite_Damage.prototype.isPlaying = function() {
return this._duration > 0;
};