Source: Game_Picture.js

Game_Picture.js

//-----------------------------------------------------------------------------
// Game_Picture
//
// The game object class for a picture.
/**
 * The game object class for a picture.
 *
 * @class
 */
function Game_Picture() {
    this.initialize(...arguments);
}

/**
 * Initialize the picture game object
 */
Game_Picture.prototype.initialize = function() {
    this.initBasic();
    this.initTarget();
    this.initTone();
    this.initRotation();
};

/**
 * Get the picture image file name
 *
 * @return {string} The picture's image file name
 */
Game_Picture.prototype.name = function() {
    return this._name;
};

/**
 * Get the picture's origin
 *
 * @return {number} The picture's origin
 */
Game_Picture.prototype.origin = function() {
    return this._origin;
};

/**
 * Get the picture's x coordinate
 *
 * @return {number} The picture's x coordinate
 */
Game_Picture.prototype.x = function() {
    return this._x;
};

/**
 * Get the picture's y coordinate
 *
 * @return {number} The picture's y coordinate
 */
Game_Picture.prototype.y = function() {
    return this._y;
};

/**
 * Get the picture's x scale
 *
 * @return {number} The picture's x scale
 */
Game_Picture.prototype.scaleX = function() {
    return this._scaleX;
};

/**
 * Get the picture's y scale
 *
 * @return {number} The picture's y scale
 */
Game_Picture.prototype.scaleY = function() {
    return this._scaleY;
};

/**
 * Get the picture's opacity
 *
 * @return {number} The picture's opacity
 */
Game_Picture.prototype.opacity = function() {
    return this._opacity;
};

/**
 * Get the picture's blend mode
 *
 * @return {number} The picture's blend mode
 */
Game_Picture.prototype.blendMode = function() {
    return this._blendMode;
};

/**
 * Get the picture's tone
 *
 * @return {number[]|null} The picture's tone
 */
Game_Picture.prototype.tone = function() {
    return this._tone;
};

/**
 * Get the picture's angle
 *
 * @return {number} The picture's angle
 */
Game_Picture.prototype.angle = function() {
    return this._angle;
};

/**
 * Initialize basic properties of the picture object
 */
Game_Picture.prototype.initBasic = function() {
    this._name = "";
    this._origin = 0;
    this._x = 0;
    this._y = 0;
    this._scaleX = 100;
    this._scaleY = 100;
    this._opacity = 255;
    this._blendMode = 0;
};

/**
 * Initialize target properties of the picture object
 */
Game_Picture.prototype.initTarget = function() {
    this._targetX = this._x;
    this._targetY = this._y;
    this._targetScaleX = this._scaleX;
    this._targetScaleY = this._scaleY;
    this._targetOpacity = this._opacity;
    this._duration = 0;
    this._wholeDuration = 0;
    this._easingType = 0;
    this._easingExponent = 0;
};

/**
 * Initialize the picture's tone
 */
Game_Picture.prototype.initTone = function() {
    this._tone = null;
    this._toneTarget = null;
    this._toneDuration = 0;
};

/**
 * Initialize the picture's rotation
 */
Game_Picture.prototype.initRotation = function() {
    this._angle = 0;
    this._rotationSpeed = 0;
};

// prettier-ignore
/**
 * Shows the picture with options
 *
 * @param {string} name - The picture image file name
 * @param {number} origin - The picture origin
 * @param {number} x - The picture x coordinate
 * @param {number} y - The picture y coordinate
 * @param {number} scaleX - The x (horizontal) scale of the picture
 * @param {number} scaleY - The y (vertical) scale of the picture
 * @param {number} opacity - The opacity of the picture
 * @param {number} blendMode - The blend mode of the picture
 */
Game_Picture.prototype.show = function(
    name, origin, x, y, scaleX, scaleY, opacity, blendMode
) {
    this._name = name;
    this._origin = origin;
    this._x = x;
    this._y = y;
    this._scaleX = scaleX;
    this._scaleY = scaleY;
    this._opacity = opacity;
    this._blendMode = blendMode;
    this.initTarget();
    this.initTone();
    this.initRotation();
};

// prettier-ignore
/**
 * Moves the picture with options
 *
 * @param {number} origin - The picture origin
 * @param {number} x - The picture x coordinate
 * @param {number} y - The picture y coordinate
 * @param {number} scaleX - The x (horizontal) scale of the picture
 * @param {number} scaleY - The y (vertical) scale of the picture
 * @param {number} opacity - The opacity of the picture
 * @param {number} blendMode - The blend mode of the picture
 * @param {number} duration - The duration of the move
 * @param {number} easingType - The easing type of the move
 */
Game_Picture.prototype.move = function(
    origin, x, y, scaleX, scaleY, opacity, blendMode, duration, easingType
) {
    this._origin = origin;
    this._targetX = x;
    this._targetY = y;
    this._targetScaleX = scaleX;
    this._targetScaleY = scaleY;
    this._targetOpacity = opacity;
    this._blendMode = blendMode;
    this._duration = duration;
    this._wholeDuration = duration;
    this._easingType = easingType;
    this._easingExponent = 2;
};

/**
 * Rotates the picture
 *
 * @param {number} speed - The speed of the rotation
 */
Game_Picture.prototype.rotate = function(speed) {
    this._rotationSpeed = speed;
};

/**
 * Sets the picture's tint over a given duration
 *
 * @param {number[]} tone - The new tone of the picture
 * @param {number} duration - The duration over which to change the tone
 */
Game_Picture.prototype.tint = function(tone, duration) {
    if (!this._tone) {
        this._tone = [0, 0, 0, 0];
    }
    this._toneTarget = tone.clone();
    this._toneDuration = duration;
    if (this._toneDuration === 0) {
        this._tone = this._toneTarget.clone();
    }
};

/**
 * Update the picture
 */
Game_Picture.prototype.update = function() {
    this.updateMove();
    this.updateTone();
    this.updateRotation();
};

/**
 * Update moving the picture
 */
Game_Picture.prototype.updateMove = function() {
    if (this._duration > 0) {
        this._x = this.applyEasing(this._x, this._targetX);
        this._y = this.applyEasing(this._y, this._targetY);
        this._scaleX = this.applyEasing(this._scaleX, this._targetScaleX);
        this._scaleY = this.applyEasing(this._scaleY, this._targetScaleY);
        this._opacity = this.applyEasing(this._opacity, this._targetOpacity);
        this._duration--;
    }
};

/**
 * Update the picture tone
 */
Game_Picture.prototype.updateTone = function() {
    if (this._toneDuration > 0) {
        const d = this._toneDuration;
        for (let i = 0; i < 4; i++) {
            this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget[i]) / d;
        }
        this._toneDuration--;
    }
};

/**
 * Update the picture rotation
 */
Game_Picture.prototype.updateRotation = function() {
    if (this._rotationSpeed !== 0) {
        this._angle += this._rotationSpeed / 2;
    }
};

/**
 * Applies easing for picture movements
 *
 * @param {number} current - The current picture state
 * @param {number} target - The target picture state
 * @return {number} The calculated value between current and target values
 */
Game_Picture.prototype.applyEasing = function(current, target) {
    const d = this._duration;
    const wd = this._wholeDuration;
    const lt = this.calcEasing((wd - d) / wd);
    const t = this.calcEasing((wd - d + 1) / wd);
    const start = (current - target * lt) / (1 - lt);
    return start + (target - start) * t;
};

/**
 * Calculates the easing amount
 *
 * @param {number} t - The amount of easing to calculate given current state
 * @return {number} The calculated easing value
 */
Game_Picture.prototype.calcEasing = function(t) {
    const exponent = this._easingExponent;
    switch (this._easingType) {
        case 1: // Slow start
            return this.easeIn(t, exponent);
        case 2: // Slow end
            return this.easeOut(t, exponent);
        case 3: // Slow start and end
            return this.easeInOut(t, exponent);
        default:
            return t;
    }
};

/**
 * Calculates the easing amount for slow start
 *
 * @param {number} t - The amount of easing to calculate given current state
 * @param {number} exponent - The exponent to apply
 * @return {number} The calculated easing value
 */
Game_Picture.prototype.easeIn = function(t, exponent) {
    return Math.pow(t, exponent);
};

/**
 * Calculates the easing amount for slow end
 *
 * @param {number} t - The amount of easing to calculate given current state
 * @param {number} exponent - The exponent to apply
 * @return {number} The calculated easing value
 */
Game_Picture.prototype.easeOut = function(t, exponent) {
    return 1 - Math.pow(1 - t, exponent);
};

/**
 * Calculates the easing amount for slow start and end
 *
 * @param {number} t - The amount of easing to calculate given current state
 * @param {number} exponent - The exponent to apply
 * @return {number} The calculated easing value
 */
Game_Picture.prototype.easeInOut = function(t, exponent) {
    if (t < 0.5) {
        return this.easeIn(t * 2, exponent) / 2;
    } else {
        return this.easeOut(t * 2 - 1, exponent) / 2 + 0.5;
    }
};