Source: Sprite_Balloon.js

Sprite_Balloon.js

//-----------------------------------------------------------------------------
// Sprite_Balloon
//
// The sprite for displaying a balloon icon.
/**
 * The sprite for displaying a balloon icon.
 *
 * @class
 * @extends Sprite
 */
function Sprite_Balloon() {
    this.initialize(...arguments);
}

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

Sprite_Balloon.prototype.initialize = function() {
    Sprite.prototype.initialize.call(this);
    this.initMembers();
    this.loadBitmap();
};

/**
 * Initialize sprite variables
 */
Sprite_Balloon.prototype.initMembers = function() {
    this._target = null;
    this._balloonId = 0;
    this._duration = 0;
    this.anchor.x = 0.5;
    this.anchor.y = 1;
    this.z = 7;
};

/**
 * Loads the bitmap file with balloons
 */
Sprite_Balloon.prototype.loadBitmap = function() {
    this.bitmap = ImageManager.loadSystem("Balloon");
    this.setFrame(0, 0, 0, 0);
};

/**
 * Sets up the animation
 *
 * @param {Sprite} targetSprite - The sprite to show the balloon on
 * @param {balloonId} number - The id of the balloon to show
 */
Sprite_Balloon.prototype.setup = function(targetSprite, balloonId) {
    this._target = targetSprite;
    this._balloonId = balloonId;
    this._duration = 8 * this.speed() + this.waitTime();
};

Sprite_Balloon.prototype.update = function() {
    Sprite.prototype.update.call(this);
    if (this._duration > 0) {
        this._duration--;
        if (this._duration > 0) {
            this.updatePosition();
            this.updateFrame();
        }
    }
};

/**
 * Updates the position of the balloon
 */
Sprite_Balloon.prototype.updatePosition = function() {
    this.x = this._target.x;
    this.y = this._target.y - this._target.height;
};

/**
 * Updates the balloon frame
 */
Sprite_Balloon.prototype.updateFrame = function() {
    const w = 48;
    const h = 48;
    const sx = this.frameIndex() * w;
    const sy = (this._balloonId - 1) * h;
    this.setFrame(sx, sy, w, h);
};

/**
 * Get the balloon animation speed
 *
 * @return {number} The speed of the balloon animation
 */
Sprite_Balloon.prototype.speed = function() {
    return 8;
};

/**
 * Get the balloon animation wait time
 *
 * @return {number} The wait time for the balloon animation
 */
Sprite_Balloon.prototype.waitTime = function() {
    return 12;
};

/**
 * Get the balloon frame index
 *
 * @return {number} The index of the balloon's frame
 */
Sprite_Balloon.prototype.frameIndex = function() {
    const index = (this._duration - this.waitTime()) / this.speed();
    return 7 - Math.max(Math.floor(index), 0);
};

/**
 * Check if the balloon animation is playing
 *
 * @return {boolean} True if the balloon is playing
 */
Sprite_Balloon.prototype.isPlaying = function() {
    return this._duration > 0;
};