Source: Sprite_Battler.js

Sprite_Battler.js

//-----------------------------------------------------------------------------
// Sprite_Battler
//
// The superclass of Sprite_Actor and Sprite_Enemy.
/**
 * The superclass of Sprite_Actor and Sprite_Enemy.
 *
 * @class
 * @extends Sprite_Clickable
 * @param {Game_Battler} battler - The battler object
 */
function Sprite_Battler() {
    this.initialize(...arguments);
}

Sprite_Battler.prototype = Object.create(Sprite_Clickable.prototype);
Sprite_Battler.prototype.constructor = Sprite_Battler;

Sprite_Battler.prototype.initialize = function(battler) {
    Sprite_Clickable.prototype.initialize.call(this);
    this.initMembers();
    this.setBattler(battler);
};

/**
 * Initialize sprite variables
 */
Sprite_Battler.prototype.initMembers = function() {
    this.anchor.x = 0.5;
    this.anchor.y = 1;
    this._battler = null;
    this._damages = [];
    this._homeX = 0;
    this._homeY = 0;
    this._offsetX = 0;
    this._offsetY = 0;
    this._targetOffsetX = NaN;
    this._targetOffsetY = NaN;
    this._movementDuration = 0;
    this._selectionEffectCount = 0;
};

/**
 * Set the battler to a new battler
 *
 * @param {Game_Battler} battler - The new battler for the sprite
 */
Sprite_Battler.prototype.setBattler = function(battler) {
    this._battler = battler;
};

/**
 * Check if the current battler is the same as the passed battler
 *
 * @param {Game_Battler} battler - The battler to check
 * @return {boolean} True if the battler matches the current battler
 */
Sprite_Battler.prototype.checkBattler = function(battler) {
    return this._battler === battler;
};

/**
 * Get the main battler sprite
 * 
 * @return {Sprite_Battler} The main battler sprite
 */
Sprite_Battler.prototype.mainSprite = function() {
    return this;
};

/**
 * Set the sprite's home
 * 
 * @param {number} x - The X value for the sprite's home
 * @param {number} y - The Y value for the sprite's home
 */
Sprite_Battler.prototype.setHome = function(x, y) {
    this._homeX = x;
    this._homeY = y;
    this.updatePosition();
};

Sprite_Battler.prototype.update = function() {
    Sprite_Clickable.prototype.update.call(this);
    if (this._battler) {
        this.updateMain();
        this.updateDamagePopup();
        this.updateSelectionEffect();
        this.updateVisibility();
    } else {
        this.bitmap = null;
    }
};

Sprite_Battler.prototype.updateVisibility = function() {
    Sprite_Clickable.prototype.updateVisibility.call(this);
    if (!this._battler || !this._battler.isSpriteVisible()) {
        this.visible = false;
    }
};

/**
 * Update the main elements of the sprite
 */
Sprite_Battler.prototype.updateMain = function() {
    if (this._battler.isSpriteVisible()) {
        this.updateBitmap();
        this.updateFrame();
    }
    this.updateMove();
    this.updatePosition();
};

/**
 * Update the sprite's bitmap
 */
Sprite_Battler.prototype.updateBitmap = function() {
    //
};

/**
 * Update the sprite's frame
 */
Sprite_Battler.prototype.updateFrame = function() {
    //
};

/**
 * Update sprite movement
 */
Sprite_Battler.prototype.updateMove = function() {
    if (this._movementDuration > 0) {
        const d = this._movementDuration;
        this._offsetX = (this._offsetX * (d - 1) + this._targetOffsetX) / d;
        this._offsetY = (this._offsetY * (d - 1) + this._targetOffsetY) / d;
        this._movementDuration--;
        if (this._movementDuration === 0) {
            this.onMoveEnd();
        }
    }
};

/**
 * Update the sprite's position
 */
Sprite_Battler.prototype.updatePosition = function() {
    this.x = this._homeX + this._offsetX;
    this.y = this._homeY + this._offsetY;
};

/**
 * Update the sprite's damage popups
 */
Sprite_Battler.prototype.updateDamagePopup = function() {
    this.setupDamagePopup();
    if (this._damages.length > 0) {
        for (const damage of this._damages) {
            damage.update();
        }
        if (!this._damages[0].isPlaying()) {
            this.destroyDamageSprite(this._damages[0]);
        }
    }
};

/**
 * Update the selection effect for the sprite
 */
Sprite_Battler.prototype.updateSelectionEffect = function() {
    const target = this.mainSprite();
    if (this._battler.isSelected()) {
        this._selectionEffectCount++;
        if (this._selectionEffectCount % 30 < 15) {
            target.setBlendColor([255, 255, 255, 64]);
        } else {
            target.setBlendColor([0, 0, 0, 0]);
        }
    } else if (this._selectionEffectCount > 0) {
        this._selectionEffectCount = 0;
        target.setBlendColor([0, 0, 0, 0]);
    }
};

/**
 * Sets up a damage popup sprite
 */
Sprite_Battler.prototype.setupDamagePopup = function() {
    if (this._battler.isDamagePopupRequested()) {
        if (this._battler.isSpriteVisible()) {
            this.createDamageSprite();
        }
        this._battler.clearDamagePopup();
        this._battler.clearResult();
    }
};

/**
 * Creates a damage popup sprite
 */
Sprite_Battler.prototype.createDamageSprite = function() {
    const last = this._damages[this._damages.length - 1];
    const sprite = new Sprite_Damage();
    if (last) {
        sprite.x = last.x + 8;
        sprite.y = last.y - 16;
    } else {
        sprite.x = this.x + this.damageOffsetX();
        sprite.y = this.y + this.damageOffsetY();
    }
    sprite.setup(this._battler);
    this._damages.push(sprite);
    this.parent.addChild(sprite);
};

/**
 * Destroy a damage popup sprite
 *
 * @param {Sprite_Damage} sprite - The damage sprite to destroy
 */
Sprite_Battler.prototype.destroyDamageSprite = function(sprite) {
    this.parent.removeChild(sprite);
    this._damages.remove(sprite);
    sprite.destroy();
};

/**
 * Get the damage offset x value
 * 
 * @return {number} The damage offset x value
 */
Sprite_Battler.prototype.damageOffsetX = function() {
    return 0;
};

/**
 * Get the damage offset y value
 * 
 * @return {number} The damage offset y value
 */
Sprite_Battler.prototype.damageOffsetY = function() {
    return 0;
};

/**
 * Starts a movement for the sprite
 * 
 * @param {number} x - The target x offset for the sprite
 * @param {number} y - The target y offset for the sprite
 * @param {number} duration - The duration of the movement in frames
 */
Sprite_Battler.prototype.startMove = function(x, y, duration) {
    if (this._targetOffsetX !== x || this._targetOffsetY !== y) {
        this._targetOffsetX = x;
        this._targetOffsetY = y;
        this._movementDuration = duration;
        if (duration === 0) {
            this._offsetX = x;
            this._offsetY = y;
        }
    }
};

/**
 * Processing for when movement ends
 */
Sprite_Battler.prototype.onMoveEnd = function() {
    //
};

/**
 * Check if the sprite is effecting
 *
 * @return {boolean} True if effecting. By default this will always return false when called from Sprite_Battler
 */
Sprite_Battler.prototype.isEffecting = function() {
    return false;
};

/**
 * Check if the sprite is moving
 *
 * @return {boolean} True if moving.
 */
Sprite_Battler.prototype.isMoving = function() {
    return this._movementDuration > 0;
};

/**
 * Check if the sprite is in its home position
 *
 * @return {boolean} True if the sprite is in its home position
 */
Sprite_Battler.prototype.inHomePosition = function() {
    return this._offsetX === 0 && this._offsetY === 0;
};

/**
 * Processing when the mouse enters the sprite
 */
Sprite_Battler.prototype.onMouseEnter = function() {
    $gameTemp.setTouchState(this._battler, "select");
};

/**
 * Processing when the sprite is pressed
 */
Sprite_Battler.prototype.onPress = function() {
    $gameTemp.setTouchState(this._battler, "select");
};

/**
 * Processing when the sprite is clicked
 */
Sprite_Battler.prototype.onClick = function() {
    $gameTemp.setTouchState(this._battler, "click");
};