Iavra

Iavra Battle Retry

Dec 11th, 2015 (edited)
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Creates an invisible savegame before every fight and optionally reloads it after a defeat.
  3.  * <Iavra Battle Retry>
  4.  * @author Iavra
  5.  *
  6.  * @param Label Retry
  7.  * @desc Label to be used for the "retry" option of the command window. Default: Retry
  8.  * @default Retry
  9.  *
  10.  * @param Label Title
  11.  * @desc Label to be used for the "title" option of the command window. Default: To Title
  12.  * @default To Title
  13.  *
  14.  * @param Window Position
  15.  * @desc Y coordinate of the command window, that gets displayed during the gameover scene after a battle. Default: 400
  16.  * @default 400
  17.  *
  18.  * @param Disable Switch
  19.  * @desc Id of an optional switch, that can be set ON to (temporarily) disable battle retry. Default: 0
  20.  * @default 0
  21.  *
  22.  * @help
  23.  * Note: Please place this below every other plugin, that deals with the battle system.
  24.  *
  25.  * If the plugin is active and the switch defined under "Disable Switch" is set to OFF, every lost fight (that will
  26.  * redirect to the gameover screen) offers the option to retry the battle.
  27.  */
  28.  
  29. var Imported = Imported || {};
  30. Imported.iavra_quickselect = true;
  31.  
  32. //=============================================================================
  33. // namespace IAVRA
  34. //=============================================================================
  35.  
  36. var IAVRA = IAVRA || {};
  37.  
  38. (function() {
  39.     "use strict";
  40.  
  41.     /**
  42.      * Retrieve plugin parameters independant from the plugin's actual filename.
  43.      */
  44.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Battle Retry>'); })[0].parameters;
  45.     var _param_labelRetry = _params['Label Retry'];
  46.     var _param_labelTitle = _params['Label Title'];
  47.     var _param_position = parseInt(_params['Window Position']) || 0;
  48.     var _param_disableSwitch = parseInt(_params['Disable Switch']) || 0;
  49.    
  50.     /**
  51.      * Contains the saved data.
  52.      */
  53.     var _data;
  54.    
  55.     /**
  56.      * Creates a new save, which is stored in a variable, instead of being written to a file.
  57.      */
  58.     var _save = function() {
  59.         var contents = DataManager.makeSaveContents();
  60.         contents.troop = $gameTroop;
  61.         _data = JsonEx.stringify(contents);
  62.     }
  63.  
  64.     /**
  65.      * Loads the current save from the variable.
  66.      */
  67.     var _load = function() {
  68.         var contents = JsonEx.parse(_data)
  69.         DataManager.extractSaveContents(contents);
  70.         $gameTroop = contents.troop;
  71.     };
  72.  
  73.     //=============================================================================
  74.     // module IAVRA.BATTLERETRY
  75.     //=============================================================================
  76.    
  77.     IAVRA.BATTLERETRY = {
  78.         Scene_Gameover: function() { this.initialize.apply(this, arguments); },
  79.         Window_Command: function() { this.initialize.apply(this, arguments); }
  80.     };
  81.    
  82.     //=============================================================================
  83.     // class IAVRA.BATTLERETRY.Scene_Gameover
  84.     //=============================================================================
  85.    
  86.     (function($) {
  87.         ($.prototype = Object.create(Scene_Gameover.prototype)).constructor = $;
  88.        
  89.         /**
  90.          * Unlike the normal gameover scene, we display a command window, that can be used to retry the battle.
  91.          */
  92.         $.prototype.create = function() {
  93.             Scene_Gameover.prototype.create.call(this);
  94.             this.createCommandWindow();
  95.         };
  96.        
  97.         /**
  98.          * On start, open the command window for a small visual touch.
  99.          */
  100.         $.prototype.start = function() {
  101.             Scene_Gameover.prototype.start.call(this);
  102.             this._commandWindow.open();
  103.         }
  104.        
  105.         /**
  106.          * Add 2 command handler to either redirect to the title screen or back to the battle.
  107.          */
  108.         $.prototype.createCommandWindow = function() {
  109.             this.addChild(this._commandWindow = new IAVRA.BATTLERETRY.Window_Command());
  110.             this._commandWindow.setHandler('retry', this.retryBattle.bind(this));
  111.             this._commandWindow.setHandler('title', this.gotoTitle.bind(this));
  112.         };
  113.        
  114.         /**
  115.          * On retry, load the stored savegame and go to the battle scene.
  116.          */
  117.         $.prototype.retryBattle = function() {
  118.             _load();
  119.             SceneManager.goto(Scene_Battle);
  120.         };
  121.        
  122.         /**
  123.          * We have a window, so we override the default behaviour of Scene_Gameover, that redirects to the title scene,
  124.          * when the "ok" button is pressed.
  125.          */
  126.         $.prototype.isTriggered = function() { return false; }
  127.        
  128.     })(IAVRA.BATTLERETRY.Scene_Gameover);
  129.    
  130.     //=============================================================================
  131.     // class IAVRA.BATTLEENTRY.Window_Retry
  132.     //=============================================================================
  133.    
  134.     (function($) {
  135.         ($.prototype = Object.create(Window_Command.prototype)).constructor = $;
  136.  
  137.         /**
  138.          * Horizontally center the window and start closed, to it can be opened by the scene, when it starts.
  139.          */
  140.         $.prototype.initialize = function() {
  141.             Window_Command.prototype.initialize.call(this, 0, _param_position);
  142.             this.x = (Graphics.boxWidth - this.width) / 2;
  143.             this.openness = 0;
  144.         };
  145.  
  146.         /**
  147.          * Add 2 commands, to either retry the battle or to go to the title screen (normal gameover behaviour).
  148.          */
  149.         $.prototype.makeCommandList = function() {
  150.             this.addCommand(_param_labelRetry, 'retry', true);
  151.             this.addCommand(_param_labelTitle, 'title', true);
  152.         };
  153.    
  154.     })(IAVRA.BATTLERETRY.Window_Command);
  155.    
  156.     //=============================================================================
  157.     // module BattleManager
  158.     //=============================================================================
  159.    
  160.     (function($) {
  161.        
  162.         /**
  163.          * On battle start, store the current game state.
  164.          */
  165.         var alias_startBattle = $.startBattle;
  166.         $.startBattle = function() {
  167.             _save();
  168.             alias_startBattle.call(this);
  169.         };
  170.        
  171.         /**
  172.          * Redirect to our own gameover scene if:
  173.          * - this is not a battle test.
  174.          * - the whole party is dead (aka defeat).
  175.          * - the battle would actually redirect to the gameover screen afterwards (isn't meant to be lost).
  176.          * - the disable switch isn't set to ON.
  177.          */
  178.         var alias_updateBattleEnd = $.updateBattleEnd;
  179.         $.updateBattleEnd = function() {
  180.             if(!this.isBattleTest() && $gameParty.isAllDead() &&
  181.                !this._canLose && !$gameSwitches.value(_param_disableSwitch)) {
  182.                 SceneManager.goto(IAVRA.BATTLERETRY.Scene_Gameover);
  183.             } else { alias_updateBattleEnd.call(this); }
  184.         };
  185.        
  186.     })(BattleManager);
  187.    
  188. })();
Add Comment
Please, Sign In to add comment