Iavra

Iavra Respawn

Jan 9th, 2016 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc A simple respawn mechanic, that revives the player and teleports them to a specific point after death.
  3.  * <Iavra Respawn>
  4.  * @author Iavra
  5.  *
  6.  * @param Variable Id
  7.  * @desc Id of the variable to be used to store the lives of the player. If it is 0 or less, the player can't respawn.
  8.  * @default 1
  9.  *
  10.  * @param Notetag
  11.  * @desc Tag to be used in map descriptions, that defines the respawn point for the player.
  12.  * @default respawn
  13.  *
  14.  * @param Window Position
  15.  * @desc Y coordinate of the command window displayed during the gameover scene. It will be horizontally centered.
  16.  * @default 400
  17.  *
  18.  * @param Label Retry
  19.  * @desc Text to be displayed for the "Retry" option. {retries} will be replaced with the number or retries available.
  20.  * @default Retry ({retries} left)
  21.  *
  22.  * @param Label Title
  23.  * @desc Text to be displayed for the "To Title" option.
  24.  * @default To Title
  25.  *
  26.  * @help
  27.  * On gameover, a window will be displayed, that lets the player decide between going back to title and respawning.
  28.  * Respawn is only available, if the number of retries is higher than 0. The current number of retries is stored in a
  29.  * variable indicated by the parameter "Variable Id".
  30.  *
  31.  * On respawn, all actors are healed to full and the player is transfered to a location indicated by a notetag in the
  32.  * map notebox. The tag can be used with 2 or 3 parameters:
  33.  *
  34.  * <respawn mapId x y>     Teleports the player to the given position on the given map.
  35.  * <respawn x y>           Teleports the player to the given position on the current map.
  36.  *
  37.  * You can register custom callbacks to be executed on respawn with the following function:
  38.  *
  39.  * IAVRA.RESPAWN.registerCallback(fn);
  40.  */
  41.  
  42. var IAVRA = IAVRA || {};
  43.  
  44. (function($) {
  45.     "use strict";
  46.  
  47.     /**
  48.      * Retrieve plugin parameters independent from the plugin's actual filename.
  49.      */
  50.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Respawn>'); })[0].parameters;
  51.     var _param_variableId = parseInt(_params['Variable Id']) || 0;
  52.     var _param_notetag = _params['Notetag'];
  53.     var _param_position = parseInt(_params['Window Position']) || 0;
  54.     var _param_labelRetry = _params['Label Retry'];
  55.     var _param_labelTitle = _params['Label Title'];
  56.  
  57.     /**
  58.      * Regex used to read notetags from map notes. Will only recognize the first occurence of the note.
  59.      */
  60.     var _regex = new RegExp('<' + _param_notetag + '[ ]+(\\d+)[ ]+(\\d+)(?:[ ]+(\\d+))?>');
  61.  
  62.     /**
  63.      * Returns the current number of respawns available.
  64.      */
  65.     var _numRetries = function() { return Math.max($gameVariables.value(_param_variableId), 0); };
  66.  
  67.     /**
  68.      * On retry, reduce the number of respawns available and execute all registered callbacks.
  69.      */
  70.     var _retry = function() {
  71.         $gameVariables.setValue(_param_variableId, Math.max($gameVariables.value(_param_variableId) - 1, 0));
  72.         $.RESPAWN.callbacks.forEach(function(callback) { callback(); });
  73.     };
  74.  
  75.     //=============================================================================
  76.     // IAVRA.RESPAWN
  77.     //=============================================================================
  78.  
  79.     $.RESPAWN = {
  80.         /**
  81.          * Stores all callbacks, that should be executed on respawn.
  82.          */
  83.         callbacks: [],
  84.         /**
  85.          * Registers a new callbacks, to be executed on respawn.
  86.          */
  87.         registerCallback: function(c) { this.callbacks.push(c); },
  88.         /**
  89.          * Removes a registered callbacks.
  90.          */
  91.         removeCallback: function(c) { var i = this.callbacks.indexOf(c); if(i > -1) { this.callbacks.splice(i, 1); } },
  92.         /**
  93.          * The window, that gets displayed during Scene_Gameover to select between respawn and going back to title.
  94.          */
  95.         Window_Retry: function() { this.initialize.apply(this, arguments); }
  96.     };
  97.  
  98.     //=============================================================================
  99.     // IAVRA.RESPAWN.Window_Retry
  100.     //=============================================================================
  101.  
  102.     (function($) {
  103.  
  104.         ($.prototype = Object.create(Window_Command.prototype)).constructor = $;
  105.  
  106.         /**
  107.          * The window is horizontally centered and starts closed, so it can be opened when starting the scene.
  108.          */
  109.         $.prototype.initialize = function() {
  110.             Window_Command.prototype.initialize.call(this, 0, _param_position);
  111.             this.x = (Graphics.boxWidth - this.width) / 2;
  112.             this.openness = 0;
  113.         };
  114.  
  115.         /**
  116.          * The windows has 2 commands, one of which gets disabled if no retries are available to the player.
  117.          */
  118.         $.prototype.makeCommandList = function() {
  119.             this.addCommand(_param_labelRetry.replace('{retries}', _numRetries()), 'retry', _numRetries() > 0);
  120.             this.addCommand(_param_labelTitle, 'title', true);
  121.         };
  122.  
  123.     })($.RESPAWN.Window_Retry);
  124.  
  125.     //=============================================================================
  126.     // Scene_Gameover
  127.     //=============================================================================
  128.  
  129.     (function($) {
  130.  
  131.         /**
  132.          * Add our window to Scene_Gameover and set the command handlers.
  133.          */
  134.         var alias_create = $.prototype.create;
  135.         $.prototype.create = function() {
  136.             alias_create.call(this);
  137.             this.addChild(this._iavra_retry_commandWindow = new IAVRA.RESPAWN.Window_Retry());
  138.             this._iavra_retry_commandWindow.setHandler('retry', _retry.bind(this));
  139.             this._iavra_retry_commandWindow.setHandler('title', this.gotoTitle.bind(this));
  140.         };
  141.  
  142.         /**
  143.          * On start, open the window for a little extra effect.
  144.          */
  145.         var alias_start = $.prototype.start;
  146.         $.prototype.start = function() {
  147.             alias_start.call(this);
  148.             this._iavra_retry_commandWindow.open();
  149.         };
  150.  
  151.         /**
  152.          * If we wouldn't overwrite this, the scene would end as soon, as the user presses a button.
  153.          */
  154.         $.prototype.isTriggered = function() { return false; };
  155.  
  156.     })(Scene_Gameover);
  157.  
  158.     //=============================================================================
  159.     // register basic callbacks
  160.     //=============================================================================
  161.  
  162.     /**
  163.      * On retry, recover all actors to full.
  164.      */
  165.     $.RESPAWN.registerCallback(function() { $gameParty.members().forEach(function(actor) { actor.recoverAll(); }); });
  166.  
  167.     /**
  168.      * On retry, teleport the player to the given location, as indicated by the map notetag. If no tag is present, the
  169.      * current player location is used, instead.
  170.      */
  171.     $.RESPAWN.registerCallback(function() {
  172.         var match = !$dataMap ? null : _regex.exec($dataMap.note);
  173.         if(match) {
  174.             if(match[3] !== undefined) { $gamePlayer.reserveTransfer(match[1]|0, match[2]|0, match[3]|0, null, 0); }
  175.             else { $gamePlayer.reserveTransfer($gameMap.mapId(), match[1]|0, match[2]|0, null, 0); }
  176.         };
  177.         SceneManager.goto(Scene_Map);
  178.     });
  179.  
  180. })(IAVRA);
Add Comment
Please, Sign In to add comment