Iavra

Iavra Single Save File

Dec 20th, 2015 (edited)
1,486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Replaces the normal save/load menus with a single save file, that gets used for all games.
  3.  * <Iavra Single Save>
  4.  * @author Iavra
  5.  *
  6.  * @param Position
  7.  * @desc Y coordinate of the save/load windows. Default: 100
  8.  * @default 100
  9.  *
  10.  * @param Width
  11.  * @desc Width of the save/load windows. Default: 600
  12.  * @default 600
  13.  *
  14.  * @param Message Save
  15.  * @desc The message to be shown, when saving a game.
  16.  * @default Save the game?
  17.  *
  18.  * @param Message Load
  19.  * @desc The message to be shown, when loading a game.
  20.  * @default Load the game?
  21.  *
  22.  * @param Message New
  23.  * @desc The message to be shown, when starting a new game and overwriting the old save.
  24.  * @default This will overwrite your old save. Continue?
  25.  *
  26.  * @param Label Yes
  27.  * @desc Text to be used for the "yes" option, when attempting to save/load a game. Default: Yes
  28.  * @default Yes
  29.  *
  30.  * @param Label No
  31.  * @desc Text to be used for the "no" option, when attempting to save/load a game. Default: No
  32.  * @default No
  33.  */
  34.  
  35. var IAVRA = IAVRA || {};
  36.  
  37. (function($) {
  38.     "use strict";
  39.  
  40.     /**
  41.      * Loads the plugin parameters independent from the plugin's actual filename.
  42.      */
  43.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Single Save>'); })[0].parameters;
  44.     var _param_position = parseInt(_params['Position']) || 0;
  45.     var _param_width = parseInt(_params['Width']) || 0;
  46.     var _text = {
  47.         messageSave: _params['Message Save'],
  48.         messageLoad: _params['Message Load'],
  49.         messageNew: _params['Message New'],
  50.         labelYes: _params['Label Yes'],
  51.         labelNo: _params['Label No']
  52.     };
  53.  
  54.     //=============================================================================
  55.     // IAVRA.SINGLESAVE
  56.     //=============================================================================
  57.  
  58.     $.SINGLESAVE = {
  59.  
  60.         /**
  61.          * Saves the game and returns true or false, depending on whether the save was a success. Can also be used for
  62.          * autosave or quicksave.
  63.          */
  64.         save: function() {
  65.             $gameSystem.onBeforeSave();
  66.             return DataManager.saveGame(1);
  67.         },
  68.  
  69.         /**
  70.          * Loads the game and returns true or false, depending on whether the load was a success. Can also be used for
  71.          * quickload.
  72.          */
  73.         load: function() {
  74.             if(DataManager.loadGame(1)) {
  75.                 $gameSystem.onAfterLoad();
  76.                 Scene_Load.prototype.reloadMapIfUpdated.call(null);
  77.                 SceneManager.goto(Scene_Map);
  78.                 if(SceneManager._scene) { SceneManager._scene.fadeOutAll(); }
  79.                 return true;
  80.             } else { return false; }
  81.         },
  82.  
  83.         /**
  84.          * Displays a simple message, when saving, loading or starting a game.
  85.          */
  86.         Window_Message: function() { this.initialize.apply(this, arguments); },
  87.  
  88.         /**
  89.          * Displays the contents of the savefile, basically like a simpler version of Window_SavefileList.
  90.          */
  91.         Window_Savefile: function() { this.initialize.apply(this, arguments); },
  92.  
  93.         /**
  94.          * Confirm dialogue, that gets used to confirm the save/load/creation of a savegame.
  95.          */
  96.         Window_Confirm: function() { this.initialize.apply(this, arguments); },
  97.  
  98.         /**
  99.          * Modified Scene_Save, that gets called when starting a new game, that will overwrite the old savefile.
  100.          */
  101.         Scene_New: function() { this.initialize.apply(this, arguments); }
  102.     };
  103.  
  104.     //=============================================================================
  105.     // IAVRA.SINGLESAVE.Window_Message
  106.     //=============================================================================
  107.  
  108.     (function($) {
  109.  
  110.         ($.prototype = Object.create(Window_Base.prototype)).constructor = $;
  111.  
  112.         /**
  113.          * This is a simple window, so we don't need any fancy initialization apart from our positioning.
  114.          */
  115.         $.prototype.initialize = function() {
  116.             var width = this.windowWidth(), height = this.windowHeight();
  117.             Window_Base.prototype.initialize.call(this, (Graphics.width - width) / 2, 0, width, height);
  118.         };
  119.  
  120.         /**
  121.          * The displayed text depends on the current scene (save/load/new game).
  122.          */
  123.         $.prototype.setText = function(text) {
  124.             this.contents.clear();
  125.             this.drawText(text, 0, 0);
  126.         };
  127.  
  128.         $.prototype.windowWidth = function() { return _param_width; };
  129.         $.prototype.windowHeight = function() { return this.fittingHeight(1); };
  130.  
  131.     })(IAVRA.SINGLESAVE.Window_Message);
  132.  
  133.     //=============================================================================
  134.     // IAVRA.SINGLESAVE.Window_Savefile
  135.     //=============================================================================
  136.  
  137.     (function($) {
  138.  
  139.         ($.prototype = Object.create(Window_Base.prototype)).constructor = $;
  140.  
  141.         /**
  142.          * See Window_SavefileList
  143.          */
  144.         $.prototype.initialize = function() {
  145.             var width = this.windowWidth(), height = this.windowHeight();
  146.             Window_Base.prototype.initialize.call(this, (Graphics.width - width) / 2, 0, width, height);
  147.             this._mode = null;
  148.         };
  149.  
  150.         /**
  151.          * This is basically Window_SavefileList for 1 elements, but without the savefileId (since it doesn't make any
  152.          * sense, if we only have 1 save, anyway).
  153.          */
  154.         $.prototype.refresh = function() {
  155.             this.contents.clear();
  156.             var valid = DataManager.isThisGameFile(1);
  157.             var info = DataManager.loadSavefileInfo(1);
  158.             var rect = new Rectangle(0, 0, this.width - this.padding * 2, this.height - this.padding * 2);
  159.             this.resetTextColor();
  160.             if (this._mode === 'load') { this.changePaintOpacity(valid); }
  161.             this.drawContents(info, rect);
  162.         };
  163.  
  164.         /**
  165.          * See Window_SavefileList
  166.          */
  167.         $.prototype.drawContents = function(info, rect) {
  168.             if(info) {
  169.                 var bottom = rect.y + rect.height;
  170.                 Window_SavefileList.prototype.drawGameTitle.call(this, info, rect.x, rect.y, rect.width);
  171.                 Window_SavefileList.prototype.drawPartyCharacters.call(this, info, rect.x + 28, bottom - 4);
  172.                 Window_SavefileList.prototype.drawPlaytime.call(this, info, rect.x, bottom - this.lineHeight(), rect.width);
  173.             }
  174.         };
  175.  
  176.         $.prototype.windowWidth = function() { return _param_width; };
  177.         $.prototype.windowHeight = function() { return this.fittingHeight(3); };
  178.         $.prototype.setMode = Window_SavefileList.prototype.setMode;
  179.  
  180.     })($.SINGLESAVE.Window_Savefile);
  181.  
  182.     //=============================================================================
  183.     // IAVRA.SINGLESAVE.Window_Confirm
  184.     //=============================================================================
  185.  
  186.     (function($) {
  187.         ($.prototype = Object.create(Window_Command.prototype)).constructor = $;
  188.  
  189.         /**
  190.          * The window starts closed, so we can have a little, fancy animation.
  191.          */
  192.         $.prototype.initialize = function() {
  193.             Window_Command.prototype.initialize.call(this);
  194.             this.openness = 0;
  195.         };
  196.  
  197.         /**
  198.          * This is a simple confirmation window, so we only have 2 commands.
  199.          */
  200.         $.prototype.makeCommandList = function() {
  201.             this.addCommand(_text.labelYes, 'ok');
  202.             this.addCommand(_text.labelNo, 'cancel');
  203.         };
  204.  
  205.     })($.SINGLESAVE.Window_Confirm);
  206.  
  207.     //=============================================================================
  208.     // IAVRA.SINGLESAVE.Scene_New
  209.     //=============================================================================
  210.  
  211.     (function($) {
  212.  
  213.         ($.prototype = Object.create(Scene_Save.prototype)).constructor = $;
  214.  
  215.         /**
  216.          * Save after creating a new game.
  217.          */
  218.         $.prototype.onSaveSuccess = function() {
  219.             SoundManager.playSave();
  220.             DataManager.setupNewGame();
  221.             this.fadeOutAll();
  222.             SceneManager.goto(Scene_Map);
  223.         };
  224.  
  225.         $.prototype.message = function() { return _text.messageNew; };
  226.  
  227.     })($.SINGLESAVE.Scene_New);
  228.  
  229.     //=============================================================================
  230.     // Scene_Title
  231.     //=============================================================================
  232.  
  233.     (function($) {
  234.  
  235.         /**
  236.          * On creating a new game, check if a savefile already exists and open the save dialogue. If not, create a new
  237.          * save and continue.
  238.          */
  239.         var alias_commandNewGame = $.prototype.commandNewGame;
  240.         $.prototype.commandNewGame = function() {
  241.             if(DataManager.isAnySavefileExists()) {
  242.                 this._commandWindow.close();
  243.                 SceneManager.push(IAVRA.SINGLESAVE.Scene_New);
  244.             } else {
  245.                 alias_commandNewGame.call(this);
  246.                 SoundManager.playSave();
  247.                 IAVRA.SINGLESAVE.save();
  248.             }
  249.         };
  250.  
  251.     })(Scene_Title);
  252.  
  253.     //=============================================================================
  254.     // Scene_File
  255.     //=============================================================================
  256.  
  257.     (function($) {
  258.  
  259.         /**
  260.          * The scene has been completely remade, since we only have 1 save slot, anyway.
  261.          */
  262.         $.prototype.create = function() {
  263.             Scene_MenuBase.prototype.create.call(this);
  264.             DataManager.loadAllSavefileImages();
  265.             this.createMessageWindow();
  266.             this.createSavefileWindow();
  267.             this.createConfirmWindow();
  268.         };
  269.  
  270.         $.prototype.start = function() {
  271.             Scene_MenuBase.prototype.start.call(this);
  272.             this._savefileWindow.refresh();
  273.             this._confirmWindow.open();
  274.         };
  275.  
  276.         $.prototype.createMessageWindow = function() {
  277.             this.addWindow(this._messageWindow = new IAVRA.SINGLESAVE.Window_Message());
  278.             this._messageWindow.setText(this.message());
  279.             this._messageWindow.y = _param_position;
  280.         };
  281.  
  282.         $.prototype.createSavefileWindow = function() {
  283.             this.addWindow(this._savefileWindow = new IAVRA.SINGLESAVE.Window_Savefile());
  284.             this._savefileWindow.setMode(this.mode());
  285.             this._savefileWindow.y = this._messageWindow.y + this._messageWindow.height;
  286.         };
  287.  
  288.         $.prototype.createConfirmWindow = function() {
  289.             this.addWindow(this._confirmWindow = new IAVRA.SINGLESAVE.Window_Confirm());
  290.             this._confirmWindow.setHandler('ok', this.onSavefileOk.bind(this));
  291.             this._confirmWindow.setHandler('cancel', this.popScene.bind(this));
  292.             this._confirmWindow.x = this._savefileWindow.x + this._savefileWindow.width - this._confirmWindow.width;
  293.             this._confirmWindow.y = this._savefileWindow.y + this._savefileWindow.height;
  294.         };
  295.  
  296.         $.prototype.savefileId = function() { return 1; };
  297.  
  298.         /**
  299.          * The displayed message depends on the active scene (save/load/new game).
  300.          */
  301.         $.prototype.message = function() { return ''; };
  302.  
  303.     })(Scene_File);
  304.  
  305.     //=============================================================================
  306.     // Scene_Save
  307.     //=============================================================================
  308.  
  309.     (function($) {
  310.  
  311.         $.prototype.onSaveFailure = function() {
  312.             SoundManager.playBuzzer();
  313.             this._confirmWindow.activate();
  314.         };
  315.  
  316.         $.prototype.message = function() { return _text.messageSave; };
  317.  
  318.     })(Scene_Save);
  319.  
  320.     //=============================================================================
  321.     // Scene_Load
  322.     //=============================================================================
  323.  
  324.     (function($) {
  325.  
  326.         $.prototype.onLoadFailure = function() {
  327.             SoundManager.playBuzzer();
  328.             this._confirmWindow.activate();
  329.         };
  330.  
  331.         $.prototype.message = function() { return _text.messageLoad; };
  332.  
  333.     })(Scene_Load);
  334.  
  335.     //=============================================================================
  336.     // DataManager
  337.     //=============================================================================
  338.  
  339.     DataManager.isAnySavefileExists = function() { return this.isThisGameFile(1); };
  340.  
  341. })(IAVRA);
Add Comment
Please, Sign In to add comment