MV Help with Image Gauge programming

Heya guys, I've been programming a custom battle HUD in javascript for a friend's RPG they're making, it's been going pretty smoothly UP UNTIL I reached the custom HP Gauge. It uses images for both the front and back of the gauge and while I can get both to render just fine, I can't figure out how to get the front part to visually drain as health goes down. I'm pretty new to javascript so I'm having trouble finding where to start on something like this, so if anyone can give me some pointers with this It'd be greatly appreciated (it would also help when I have to eventually make the custom MP bar too)!
Spoiler: HUD Example Images
1773590168661.png 1773590920285.png 1773590935214.png
I'm not an expert on this, but I believe you want to use an empty heart and a full heart, then apply a frame mask on the full heart based on current HP levels to hide whatever part you don't want to show.

You'd do something like :
hpRatio = currentHP / maxHP
visibleHeight = spriteHeight * hpRatio

for exeample :
hpRatio = 50 / 100
visibleHeight = 100 (sprite height) * 0.5

Then apply visibleHeight to a frame mask, hiding the part you don't want to show
The full and empty hearts are already separate images, I tried changing the frame of the full heart sprite but it wouldn't update.

1773593142032.png

I can try the mask method, but the question is how would I go about doing that (still new at java)?
I do have a gauge plugin which that one is able what you want to do.
but if its for battle/map hud, you can also use SRD_HudMaker that can
do what you want.

only the one I have is not public yet as I try to get the other one done first.
so I don't know if you use your own plugin or other plugin to make what you want.

for battle scene => use SRD HudMaker, its easier with it.
Been a bit, but I've done some more work on the hud with adding a backdrop and the mp bar.
I've attempted to apply masks to both bars but they don't seem to be showing up for some reason.
I've made sure the masks target the same location, width and height of the bar bitmaps but they seem to still be misplaced somehow.

1773626970947.png
1773627100298.png


I'll leave some excerpts from my hud code below to show what I mean.

Spoiler: Mask Code Excerpt 1
//creating and applying the masks
this._hpGauge = new Sprite();
this._hpGauge.bitmap = this.getGaugeFrontImage('HP', actor);

this._hpMask = new PIXI.Graphics()
.beginFill(0xFFFFFF)
.drawRect(0, 0, this._hpGauge.bitmap.width, this._hpGauge.bitmap.height)
.endFill();

this._hpGauge.mask = this._hpMask;
this._hpGauge._refresh();
this.addChildAt(this._hpGauge, 4);

this._mpGauge = new Sprite();
this._mpGauge.bitmap = this.getGaugeFrontImage('MP', actor);

this._mpMask = new PIXI.Graphics()
.beginFill(0xFFFFFF)
.drawRect(0, 0, this._mpGauge.bitmap.width, this._mpGauge.bitmap.height)
.endFill();

this._mpGauge.mask = this._mpMask;
this._mpGauge._refresh();
this.addChildAt(this._mpGauge, 4);
Spoiler: Mask Code Excerpt 2
// when the hp gauge is drawn it is move to the defined x and y coords
this._hpGauge.move(x, y);
this._hpMask.moveTo(this._hpGauge.x, this._hpGauge.x);
//imagine the same but with mp

Can anyone tell me if I'm missing something here? The front of the gauge is still supposed to be fully visible when the mask is applied and hp/mp is full.

Oh, I also forgot to mention that I don't plan to use other plugins for this since it's less limiting (and also to learn javascript a lil lmao).
Been a bit, but I've done some more work on the hud with adding a backdrop and the mp bar.
I've attempted to apply masks to both bars but they don't seem to be showing up for some reason.
I've made sure the masks target the same location, width and height of the bar bitmaps but they seem to still be misplaced somehow.

View attachment 362788 View attachment 362789

I'll leave some excerpts from my hud code below to show what I mean.

Spoiler: Mask Code Excerpt 1
//creating and applying the masks
this._hpGauge = new Sprite();
this._hpGauge.bitmap = this.getGaugeFrontImage('HP', actor);

this._hpMask = new PIXI.Graphics()
.beginFill(0xFFFFFF)
.drawRect(0, 0, this._hpGauge.bitmap.width, this._hpGauge.bitmap.height)
.endFill();

this._hpGauge.mask = this._hpMask;
this._hpGauge._refresh();
this.addChildAt(this._hpGauge, 4);

this._mpGauge = new Sprite();
this._mpGauge.bitmap = this.getGaugeFrontImage('MP', actor);

this._mpMask = new PIXI.Graphics()
.beginFill(0xFFFFFF)
.drawRect(0, 0, this._mpGauge.bitmap.width, this._mpGauge.bitmap.height)
.endFill();

this._mpGauge.mask = this._mpMask;
this._mpGauge._refresh();
this.addChildAt(this._mpGauge, 4);
Spoiler: Mask Code Excerpt 2
// when the hp gauge is drawn it is move to the defined x and y coords
this._hpGauge.move(x, y);
this._hpMask.moveTo(this._hpGauge.x, this._hpGauge.x);
//imagine the same but with mp

Can anyone tell me if I'm missing something here? The front of the gauge is still supposed to be fully visible when the mask is applied and hp/mp is full.

Oh, I also forgot to mention that I don't plan to use other plugins for this since it's less limiting (and also to learn javascript a lil lmao).

The second snippet has two times the this._hpGauge.x , should be x,y

Also, this._hpMask.moveTo(this._hpGauge.x, this._hpGauge.x);
should be something like :
this._hpMask.x = this._hpGauge.x;
this._hpMask.y = this._hpGauge.y;

Or, simply :
this._hpGauge.move(x, y);

this._hpMask.x = x;
this._hpMask.y = y;
Because you want to move the mask at the gauge's position anyway.

Also, you need to add the mask to the PIXI display tree, so before you do :
this._hpGauge.mask = this._hpMask;
you'd add :
this.addChild(this._hpMask);
(Same for MP)



I think there will still be an issue with how the mask is drawn, as you draw it from x0, y0, instead of the correct Y position, since you draw it from y0 it will probably always draw a full rectangle mask every time, showing empty heart and gauge, you need to calculate the starting Y position for the mask drawing, using more or less what I said before

hpRatio = currentHP / maxHP
visibleHeight = spriteHeight * hpRatio

example :
hpRatio = 80 / 100
visibleHeight = 100 * 0.8 (= 80)

Now you want to invert that with :
maskStart = spriteHeight - visibleHeight

still in the example :
maskStart = 100 - 80 (= 20)

Now, instead of starting to draw your mask at x0, y0, you start drawing your mask at x0, maskStart
(Apply same logic on X axis for the MP gauge)


And, unless I'm totally mistaken, this should do the trick xD
Alright, I've figured it out now! Thanks for your help!
1773679745654.png
K actually, new problem. Draining works fine, but when healing the bar doesn't go back up.
I wonder if it's because I'm moving the mask itself instead of changing its height (which doesn't seem to work for some reason?)

1773771496834.png


Spoiler: HP Gauge Code
Window_BattleStatus.prototype.drawHPGauge = function(x, y, height, rate, actor) {
var grid = 95;
var bitmap = this.getGaugeBackImage('HP', actor);
var gaugeY = y - height + 36;
var drainH = 68 * rate;

this._hpGauge.move(x, gaugeY);
this._hpMask.x = this._hpGauge.x;
//this does the actual gauge moving, only goes down for some reason?
this._hpMask.y = this._hpGauge.y - 8 - drainH;

this._gaugeContents.bitmap.blt(bitmap, 0, 0, grid, gaugeH, x, gaugeY);
};

Theoretically this should just work but it only goes down, not back up. When I check the mask y in the console, it DOES update but its not reflected on the actual mask position. Am I missing something here?
Another thing I just noticed is, if one were to change the scene mid battle (say like, "formation" in Yanflys party system) the gauges unload and when returning to the battle scene, the mask is correctly updated to the position its supposed to be in. I wonder if this'll get me closer to a solution...

EDIT: I've ALMOST found a perfect solution, as it turns out it was creating new masks everytime lol. Now i gotta figure out how to make each mask unique from each other since its only affecting the most recent actors gauges

Original thread: https://forums.rpgmakerweb.com/threads/help-with-image-gauge-programming.182884/