Source: Sprite_Gauge.js

Sprite_Gauge.js

//-----------------------------------------------------------------------------
// Sprite_Gauge
//
// The sprite for displaying a status gauge.
/**
 * The sprite for displaying a status gauge.
 *
 * @class
 * @extends Sprite
 */
function Sprite_Gauge() {
    this.initialize(...arguments);
}

Sprite_Gauge.prototype = Object.create(Sprite.prototype);
Sprite_Gauge.prototype.constructor = Sprite_Gauge;

Sprite_Gauge.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this.initMembers();
    this.createBitmap();
};

/**
 * Initialize sprite variables
 */
Sprite_Gauge.prototype.initMembers = function() {
    this._battler = null;
    this._statusType = "";
    this._value = NaN;
    this._maxValue = NaN;
    this._targetValue = NaN;
    this._targetMaxValue = NaN;
    this._duration = 0;
    this._flashingCount = 0;
};

Sprite_Gauge.prototype.destroy = function(options) {
    this.bitmap.destroy();
    Sprite.prototype.destroy.call(this, options);
};

/**
 * Creates a bitmap for the sprite
 */
Sprite_Gauge.prototype.createBitmap = function() {
    const width = this.bitmapWidth();
    const height = this.bitmapHeight();
    this.bitmap = new Bitmap(width, height);
};

/**
 * Gets the width of the bitmap
 *
 * @return {number} The bitmap width
 */
Sprite_Gauge.prototype.bitmapWidth = function() {
    return 128;
};

/**
 * Gets the height of the bitmap
 *
 * @return {number} The bitmap height
 */
Sprite_Gauge.prototype.bitmapHeight = function() {
    return 32;
};

/**
 * Gets the text height
 *
 * @return {number} The text height
 * @since Version 1.3.3
 */
Sprite_Gauge.prototype.textHeight = function() {
    return 24;
};

/**
 * Gets the height of the gauge
 *
 * @return {number} The gauge height
 */
Sprite_Gauge.prototype.gaugeHeight = function() {
    return 12;
};

/**
 * Gets X coordinate of the gauge
 *
 * @return {number} The X coordinate of the gauge
 */
Sprite_Gauge.prototype.gaugeX = function() {
    if (this._statusType === "time") {
        return 0;
    } else {
        return this.measureLabelWidth() + 6;
    }
};

/**
 * Gets the Y coordinate of the gauge label
 *
 * @return {number} The Y coordinate
 */
Sprite_Gauge.prototype.labelY = function() {
    return 3;
};

/**
 * Gets the font face for the label
 *
 * @return {string} The font face
 */
Sprite_Gauge.prototype.labelFontFace = function() {
    return $gameSystem.mainFontFace();
};

/**
 * Gets the font size for the label
 *
 * @return {number} The font size
 */
Sprite_Gauge.prototype.labelFontSize = function() {
    return $gameSystem.mainFontSize() - 2;
};

/**
 * Gets the font face for the value
 *
 * @return {string} The font face
 */
Sprite_Gauge.prototype.valueFontFace = function() {
    return $gameSystem.numberFontFace();
};

/**
 * Gets the font size for the value
 *
 * @return {number} The font size
 */
Sprite_Gauge.prototype.valueFontSize = function() {
    return $gameSystem.mainFontSize() - 6;
};

/**
 * Sets the sprite up
 *
 * @param {Game_Battler} battler - The battler object to track
 * @param {string} statusType - The type of stat to show in the gauge
 */
Sprite_Gauge.prototype.setup = function(battler, statusType) {
    this._battler = battler;
    this._statusType = statusType;
    this._value = this.currentValue();
    this._maxValue = this.currentMaxValue();
    this.updateBitmap();
};

Sprite_Gauge.prototype.update = function() {
    Sprite.prototype.update.call(this);
    this.updateBitmap();
};

/**
 * Updates the sprite's bitmap
 */
Sprite_Gauge.prototype.updateBitmap = function() {
    const value = this.currentValue();
    const maxValue = this.currentMaxValue();
    if (value !== this._targetValue || maxValue !== this._targetMaxValue) {
        this.updateTargetValue(value, maxValue);
    }
    this.updateGaugeAnimation();
    this.updateFlashing();
};

/**
 * Updates for the target value of the gauge
 *
 * @param {number} value - The current value of the gauge
 * @param {number} maxValue - The max value of the gauge
 */
Sprite_Gauge.prototype.updateTargetValue = function(value, maxValue) {
    this._targetValue = value;
    this._targetMaxValue = maxValue;
    if (isNaN(this._value)) {
        this._value = value;
        this._maxValue = maxValue;
        this.redraw();
    } else {
        this._duration = this.smoothness();
    }
};

/**
 * Get the smoothness of the effect
 *
 * @return {number} The smoothness of the effect
 */
Sprite_Gauge.prototype.smoothness = function() {
    return this._statusType === "time" ? 5 : 20;
};

/**
 * Updates the gauge animation effect
 */
Sprite_Gauge.prototype.updateGaugeAnimation = function() {
    if (this._duration > 0) {
        const d = this._duration;
        this._value = (this._value * (d - 1) + this._targetValue) / d;
        this._maxValue = (this._maxValue * (d - 1) + this._targetMaxValue) / d;
        this._duration--;
        this.redraw();
    }
};

/**
 * Updates the gauge flash effect
 */
Sprite_Gauge.prototype.updateFlashing = function() {
    if (this._statusType === "time") {
        this._flashingCount++;
        if (this._battler.isInputting()) {
            if (this._flashingCount % 30 < 15) {
                this.setBlendColor(this.flashingColor1());
            } else {
                this.setBlendColor(this.flashingColor2());
            }
        } else {
            this.setBlendColor([0, 0, 0, 0]);
        }
    }
};

/**
 * Get the color 1 flash
 *
 * @return {Array} An array representing a blend color
 */
Sprite_Gauge.prototype.flashingColor1 = function() {
    return [255, 255, 255, 64];
};

/**
 * Get the color 2 flash
 *
 * @return {Array} An array representing a blend color
 */
Sprite_Gauge.prototype.flashingColor2 = function() {
    return [0, 0, 255, 48];
};

/**
 * Check if the gauge is valid
 *
 * @return {boolean} True if the gauge is valid
 */
Sprite_Gauge.prototype.isValid = function() {
    if (this._battler) {
        if (this._statusType === "tp" && !this._battler.isPreserveTp()) {
            return $gameParty.inBattle();
        } else {
            return true;
        }
    }
    return false;
};

/**
 * Get the gauge's current value
 *
 * @return {number} The current value of the gauge
 */
Sprite_Gauge.prototype.currentValue = function() {
    if (this._battler) {
        switch (this._statusType) {
            case "hp":
                return this._battler.hp;
            case "mp":
                return this._battler.mp;
            case "tp":
                return this._battler.tp;
            case "time":
                return this._battler.tpbChargeTime();
        }
    }
    return NaN;
};

/**
 * Get the gauge's max value
 *
 * @return {number} The max value of the gauge
 */
Sprite_Gauge.prototype.currentMaxValue = function() {
    if (this._battler) {
        switch (this._statusType) {
            case "hp":
                return this._battler.mhp;
            case "mp":
                return this._battler.mmp;
            case "tp":
                return this._battler.maxTp();
            case "time":
                return 1;
        }
    }
    return NaN;
};

/**
 * Get the gauge's label
 *
 * @return {string} The label
 */
Sprite_Gauge.prototype.label = function() {
    switch (this._statusType) {
        case "hp":
            return TextManager.hpA;
        case "mp":
            return TextManager.mpA;
        case "tp":
            return TextManager.tpA;
        default:
            return "";
    }
};

/**
 * Get the gauge's back color
 *
 * @return {string} The color of the back of the gauge
 */
Sprite_Gauge.prototype.gaugeBackColor = function() {
    return ColorManager.gaugeBackColor();
};

/**
 * Get the gauge's color 1
 *
 * @return {string} The color 1 of the gauge
 */
Sprite_Gauge.prototype.gaugeColor1 = function() {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpGaugeColor1();
        case "mp":
            return ColorManager.mpGaugeColor1();
        case "tp":
            return ColorManager.tpGaugeColor1();
        case "time":
            return ColorManager.ctGaugeColor1();
        default:
            return ColorManager.normalColor();
    }
};

/**
 * Get the gauge's color 2
 *
 * @return {string} The color 2 of the gauge
 */
Sprite_Gauge.prototype.gaugeColor2 = function() {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpGaugeColor2();
        case "mp":
            return ColorManager.mpGaugeColor2();
        case "tp":
            return ColorManager.tpGaugeColor2();
        case "time":
            return ColorManager.ctGaugeColor2();
        default:
            return ColorManager.normalColor();
    }
};

/**
 * Get the gauge label's color
 *
 * @return {string} The color of the label
 */
Sprite_Gauge.prototype.labelColor = function() {
    return ColorManager.systemColor();
};

/**
 * Get the gauge label outline color
 *
 * @return {string} The color of the label outline
 */
Sprite_Gauge.prototype.labelOutlineColor = function() {
    return ColorManager.outlineColor();
};

/**
 * Get the gauge label outline width
 *
 * @return {number} The width of the label outline
 */
Sprite_Gauge.prototype.labelOutlineWidth = function() {
    return 3;
};

/**
 * Get the color for the value based on type
 *
 * @return {string} The color of the value
 */
Sprite_Gauge.prototype.valueColor = function() {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpColor(this._battler);
        case "mp":
            return ColorManager.mpColor(this._battler);
        case "tp":
            return ColorManager.tpColor(this._battler);
        default:
            return ColorManager.normalColor();
    }
};

/**
 * Get the color for the value outline
 *
 * @return {string} The color of the outline
 */
Sprite_Gauge.prototype.valueOutlineColor = function() {
    return "rgba(0, 0, 0, 1)";
};

/**
 * Get the width of the value outline
 *
 * @return {number} The width of the outline
 */
Sprite_Gauge.prototype.valueOutlineWidth = function() {
    return 2;
};

/**
 * Redraws the gauge
 */
Sprite_Gauge.prototype.redraw = function() {
    this.bitmap.clear();
    const currentValue = this.currentValue();
    if (!isNaN(currentValue)) {
        this.drawGauge();
        if (this._statusType !== "time") {
            this.drawLabel();
            if (this.isValid()) {
                this.drawValue();
            }
        }
    }
};

/**
 * Draws the gauge
 */
Sprite_Gauge.prototype.drawGauge = function() {
    const gaugeX = this.gaugeX();
    const gaugeY = this.textHeight() - this.gaugeHeight();
    const gaugewidth = this.bitmapWidth() - gaugeX;
    const gaugeHeight = this.gaugeHeight();
    this.drawGaugeRect(gaugeX, gaugeY, gaugewidth, gaugeHeight);
};

/**
 * Draws the gauge by x/y/width/height
 *
 * @param {number} x - The x coordinate of the gauge
 * @param {number} y - The y coordinate of the gauge
 * @param {number} width - The width of the gauge
 * @param {number} height - The height of the gauge
 */
Sprite_Gauge.prototype.drawGaugeRect = function(x, y, width, height) {
    const rate = this.gaugeRate();
    const fillW = Math.floor((width - 2) * rate);
    const fillH = height - 2;
    const color0 = this.gaugeBackColor();
    const color1 = this.gaugeColor1();
    const color2 = this.gaugeColor2();
    this.bitmap.fillRect(x, y, width, height, color0);
    this.bitmap.gradientFillRect(x + 1, y + 1, fillW, fillH, color1, color2);
};

/**
 * Gets the gauge rate
 *
 * @return {number} x - The rate of the gauge (how much is filled)
 */
Sprite_Gauge.prototype.gaugeRate = function() {
    if (this.isValid()) {
        const value = this._value;
        const maxValue = this._maxValue;
        return maxValue > 0 ? value / maxValue : 0;
    } else {
        return 0;
    }
};

/**
 * Draws the label
 */
Sprite_Gauge.prototype.drawLabel = function() {
    const label = this.label();
    const x = this.labelOutlineWidth() / 2;
    const y = this.labelY();
    const width = this.bitmapWidth();
    const height = this.textHeight();
    this.setupLabelFont();
    this.bitmap.paintOpacity = this.labelOpacity();
    this.bitmap.drawText(label, x, y, width, height, "left");
    this.bitmap.paintOpacity = 255;
};

/**
 * Sets up the label's font
 */
Sprite_Gauge.prototype.setupLabelFont = function() {
    this.bitmap.fontFace = this.labelFontFace();
    this.bitmap.fontSize = this.labelFontSize();
    this.bitmap.textColor = this.labelColor();
    this.bitmap.outlineColor = this.labelOutlineColor();
    this.bitmap.outlineWidth = this.labelOutlineWidth();
};

/**
 * Gets the width of the label
 *
 * @return {number} The width
 * @since Version 1.1.0
 */
Sprite_Gauge.prototype.measureLabelWidth = function() {
    this.setupLabelFont();
    const labels = [TextManager.hpA, TextManager.mpA, TextManager.tpA];
    const widths = labels.map(str => this.bitmap.measureTextWidth(str));
    return Math.ceil(Math.max(...widths));
};

/**
 * Gets the opacity of the label
 *
 * @return {number} The opacity
 */
Sprite_Gauge.prototype.labelOpacity = function() {
    return this.isValid() ? 255 : 160;
};

/**
 * Draws the gauge value
 */
Sprite_Gauge.prototype.drawValue = function() {
    const currentValue = this.currentValue();
    const width = this.bitmapWidth();
    const height = this.textHeight();
    this.setupValueFont();
    this.bitmap.drawText(currentValue, 0, 0, width, height, "right");
};

/**
 * Sets up the value font
 */
Sprite_Gauge.prototype.setupValueFont = function() {
    this.bitmap.fontFace = this.valueFontFace();
    this.bitmap.fontSize = this.valueFontSize();
    this.bitmap.textColor = this.valueColor();
    this.bitmap.outlineColor = this.valueOutlineColor();
    this.bitmap.outlineWidth = this.valueOutlineWidth();
};