Source: Scene_Base.js

Scene_Base.js

//-----------------------------------------------------------------------------
// Scene_Base
//
// The superclass of all scenes within the game.
/**
 * The superclass of all scenes within the game.
 *
 * @class
 * @extends Stage
 */
function Scene_Base() {
    this.initialize(...arguments);
}

Scene_Base.prototype = Object.create(Stage.prototype);
Scene_Base.prototype.constructor = Scene_Base;

/**
 * Initializes the scene
 */
Scene_Base.prototype.initialize = function() {
    Stage.prototype.initialize.call(this);
    this._started = false;
    this._active = false;
    this._fadeSign = 0;
    this._fadeDuration = 0;
    this._fadeWhite = 0;
    this._fadeOpacity = 0;
    this.createColorFilter();
};

/**
 * Creates the scene
 */
Scene_Base.prototype.create = function() {
    //
};

/**
 * Check if scene is active
 *
 * @return {boolean} True if the scene is active
 */
Scene_Base.prototype.isActive = function() {
    return this._active;
};

/**
 * Check if scene is ready
 *
 * @return {boolean} True if the scene is ready
 */
Scene_Base.prototype.isReady = function() {
    return (
        ImageManager.isReady() &&
        EffectManager.isReady() &&
        FontManager.isReady()
    );
};

/**
 * Starts the scene
 */
Scene_Base.prototype.start = function() {
    this._started = true;
    this._active = true;
};

/**
 * Updates the scene
 */
Scene_Base.prototype.update = function() {
    this.updateFade();
    this.updateColorFilter();
    this.updateChildren();
    AudioManager.checkErrors();
};

/**
 * Stops the scene
 */
Scene_Base.prototype.stop = function() {
    this._active = false;
};

/**
 * Check if scene is started
 *
 * @return {boolean} True if the scene is started
 */
Scene_Base.prototype.isStarted = function() {
    return this._started;
};

/**
 * Check if scene is busy
 *
 * @return {boolean} True if the scene is busy
 */
Scene_Base.prototype.isBusy = function() {
    return this.isFading();
};

/**
 * Check if scene is currently fading the screen
 *
 * @return {boolean} True if the scene is fading
 */
Scene_Base.prototype.isFading = function() {
    return this._fadeDuration > 0;
};

/**
 * Terminates the scene
 */
Scene_Base.prototype.terminate = function() {
    //
};

/**
 * Create the scene's {@link WindowLayer}
 */
Scene_Base.prototype.createWindowLayer = function() {
    this._windowLayer = new WindowLayer();
    this._windowLayer.x = (Graphics.width - Graphics.boxWidth) / 2;
    this._windowLayer.y = (Graphics.height - Graphics.boxHeight) / 2;
    this.addChild(this._windowLayer);
};

/**
 * Adds a window to the scene's {@link WindowLayer}
 *
 * @param {Window} window - The window to add as a child
 */
Scene_Base.prototype.addWindow = function(window) {
    this._windowLayer.addChild(window);
};

/**
 * Start to fade the scene in
 *
 * @param {number} duration - Frames to fade in the scene
 * @param {boolean} white - If the fade color is white or not
 */
Scene_Base.prototype.startFadeIn = function(duration, white) {
    this._fadeSign = 1;
    this._fadeDuration = duration || 30;
    this._fadeWhite = white;
    this._fadeOpacity = 255;
    this.updateColorFilter();
};

/**
 * Start to fade the scene out
 *
 * @param {number} duration - Frames to fade out the scene
 * @param {boolean} white - If the fade color is white or not
 */
Scene_Base.prototype.startFadeOut = function(duration, white) {
    this._fadeSign = -1;
    this._fadeDuration = duration || 30;
    this._fadeWhite = white;
    this._fadeOpacity = 0;
    this.updateColorFilter();
};

/**
 * Creates the scene's {@link ColorFilter}
 */
Scene_Base.prototype.createColorFilter = function() {
    this._colorFilter = new ColorFilter();
    this.filters = [this._colorFilter];
};

/**
 * Updates the scene's {@link ColorFilter}
 */
Scene_Base.prototype.updateColorFilter = function() {
    const c = this._fadeWhite ? 255 : 0;
    const blendColor = [c, c, c, this._fadeOpacity];
    this._colorFilter.setBlendColor(blendColor);
};

/**
 * Updates the scene's fade
 */
Scene_Base.prototype.updateFade = function() {
    if (this._fadeDuration > 0) {
        const d = this._fadeDuration;
        if (this._fadeSign > 0) {
            this._fadeOpacity -= this._fadeOpacity / d;
        } else {
            this._fadeOpacity += (255 - this._fadeOpacity) / d;
        }
        this._fadeDuration--;
    }
};

/**
 * Updates the scene's children
 */
Scene_Base.prototype.updateChildren = function() {
    for (const child of this.children) {
        if (child.update) {
            child.update();
        }
    }
};

/**
 * Pops the scene to return to the previous scene
 */
Scene_Base.prototype.popScene = function() {
    SceneManager.pop();
};

/**
 * Check if there is a gameover, and go to Scene_Gameover if so
 */
Scene_Base.prototype.checkGameover = function() {
    if ($gameParty.isAllDead()) {
        SceneManager.goto(Scene_Gameover);
    }
};

/**
 * Fades out the scene and audio
 */
Scene_Base.prototype.fadeOutAll = function() {
    const time = this.slowFadeSpeed() / 60;
    AudioManager.fadeOutBgm(time);
    AudioManager.fadeOutBgs(time);
    AudioManager.fadeOutMe(time);
    this.startFadeOut(this.slowFadeSpeed());
};

/**
 * Default speed for a normal fade
 * 
 * @return {number} Amount of frames to fade
 */
Scene_Base.prototype.fadeSpeed = function() {
    return 24;
};

/**
 * Default speed for a slow fade
 * 
 * @return {number} Amount of frames to fade
 */
Scene_Base.prototype.slowFadeSpeed = function() {
    return this.fadeSpeed() * 2;
};

/**
 * Scales a given sprite to fit in the game window
 * 
 * @param {Sprite} sprite - The sprite to scale
 */
Scene_Base.prototype.scaleSprite = function(sprite) {
    const ratioX = Graphics.width / sprite.bitmap.width;
    const ratioY = Graphics.height / sprite.bitmap.height;
    const scale = Math.max(ratioX, ratioY, 1.0);
    sprite.scale.x = scale;
    sprite.scale.y = scale;
};

/**
 * Centers a given sprite within the game window
 * 
 * @param {Sprite} sprite - The sprite to center
 */
Scene_Base.prototype.centerSprite = function(sprite) {
    sprite.x = Graphics.width / 2;
    sprite.y = Graphics.height / 2;
    sprite.anchor.x = 0.5;
    sprite.anchor.y = 0.5;
};

/**
 * Check if the help window appears on bottom
 * 
 * @return {boolean} True if bottom help mode is enabled
 */
Scene_Base.prototype.isBottomHelpMode = function() {
    return true;
};

/**
 * Check if touch UI buttons appear on bottom
 * 
 * @return {boolean} True if bottom button mode is enabled
 */
Scene_Base.prototype.isBottomButtonMode = function() {
    return false;
};

/**
 * Check if right input mode is enabled
 * 
 * @return {boolean} True if right input mode is enabled
 */
Scene_Base.prototype.isRightInputMode = function() {
    return true;
};

/**
 * Get the main command width
 * 
 * @return {number} Width of a main command
 */
Scene_Base.prototype.mainCommandWidth = function() {
    return 240;
};

/**
 * Gets the top of the touch UI button area in the scene
 * 
 * @return {number} Y Pixel for the top coordinate of the button area
 */
Scene_Base.prototype.buttonAreaTop = function() {
    if (this.isBottomButtonMode()) {
        return Graphics.boxHeight - this.buttonAreaHeight();
    } else {
        return 0;
    }
};

/**
 * Gets the bottom of the touch UI button area in the scene
 * 
 * @return {number} Y Pixel for the cottom coordinate of the button area
 */
Scene_Base.prototype.buttonAreaBottom = function() {
    return this.buttonAreaTop() + this.buttonAreaHeight();
};

/**
 * Gets the height of the touch UI button area
 * 
 * @return {number} Height in pixels of the button area
 */
Scene_Base.prototype.buttonAreaHeight = function() {
    return 52;
};

/**
 * Gets the Y pixel of the buttons
 * 
 * @return {number} Y Pixel for the top coordinate of the buttons
 */
Scene_Base.prototype.buttonY = function() {
    const offsetY = Math.floor((this.buttonAreaHeight() - 48) / 2);
    return this.buttonAreaTop() + offsetY;
};

/**
 * Calculates how many pixels tall a window is
 * 
 * @param {number} numLines - Amount of text lines in the window
 * @param {boolean} selectable - If the window is selectable (or inherits from Window_Selectable) or not
 * @return {number} Amount of pixels tall the window is
 */
Scene_Base.prototype.calcWindowHeight = function(numLines, selectable) {
    if (selectable) {
        return Window_Selectable.prototype.fittingHeight(numLines);
    } else {
        return Window_Base.prototype.fittingHeight(numLines);
    }
};

/**
 * Requests an autosave
 */
Scene_Base.prototype.requestAutosave = function() {
    if (this.isAutosaveEnabled()) {
        this.executeAutosave();
    }
};

/**
 * Check if autosaving is enabled
 * 
 * @return {boolean} True if the game can autosave
 */
Scene_Base.prototype.isAutosaveEnabled = function() {
    return (
        !DataManager.isBattleTest() &&
        !DataManager.isEventTest() &&
        $gameSystem.isAutosaveEnabled() &&
        $gameSystem.isSaveEnabled()
    );
};

/**
 * Try to autosave the game
 */
Scene_Base.prototype.executeAutosave = function() {
    $gameSystem.onBeforeSave();
    DataManager.saveGame(0)
        .then(() => this.onAutosaveSuccess())
        .catch(() => this.onAutosaveFailure());
};

/**
 * Processing for when an autosave attempt was successful
 */
Scene_Base.prototype.onAutosaveSuccess = function() {
    //
};

/**
 * Processing for when an autosave attempt failed
 */
Scene_Base.prototype.onAutosaveFailure = function() {
    //
};