Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*:
- * @plugindesc A simple respawn mechanic, that revives the player and teleports them to a specific point after death.
- * <Iavra Respawn>
- * @author Iavra
- *
- * @param Variable Id
- * @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.
- * @default 1
- *
- * @param Notetag
- * @desc Tag to be used in map descriptions, that defines the respawn point for the player.
- * @default respawn
- *
- * @param Window Position
- * @desc Y coordinate of the command window displayed during the gameover scene. It will be horizontally centered.
- * @default 400
- *
- * @param Label Retry
- * @desc Text to be displayed for the "Retry" option. {retries} will be replaced with the number or retries available.
- * @default Retry ({retries} left)
- *
- * @param Label Title
- * @desc Text to be displayed for the "To Title" option.
- * @default To Title
- *
- * @help
- * On gameover, a window will be displayed, that lets the player decide between going back to title and respawning.
- * Respawn is only available, if the number of retries is higher than 0. The current number of retries is stored in a
- * variable indicated by the parameter "Variable Id".
- *
- * On respawn, all actors are healed to full and the player is transfered to a location indicated by a notetag in the
- * map notebox. The tag can be used with 2 or 3 parameters:
- *
- * <respawn mapId x y> Teleports the player to the given position on the given map.
- * <respawn x y> Teleports the player to the given position on the current map.
- *
- * You can register custom callbacks to be executed on respawn with the following function:
- *
- * IAVRA.RESPAWN.registerCallback(fn);
- */
- var IAVRA = IAVRA || {};
- (function($) {
- "use strict";
- /**
- * Retrieve plugin parameters independent from the plugin's actual filename.
- */
- var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Respawn>'); })[0].parameters;
- var _param_variableId = parseInt(_params['Variable Id']) || 0;
- var _param_notetag = _params['Notetag'];
- var _param_position = parseInt(_params['Window Position']) || 0;
- var _param_labelRetry = _params['Label Retry'];
- var _param_labelTitle = _params['Label Title'];
- /**
- * Regex used to read notetags from map notes. Will only recognize the first occurence of the note.
- */
- var _regex = new RegExp('<' + _param_notetag + '[ ]+(\\d+)[ ]+(\\d+)(?:[ ]+(\\d+))?>');
- /**
- * Returns the current number of respawns available.
- */
- var _numRetries = function() { return Math.max($gameVariables.value(_param_variableId), 0); };
- /**
- * On retry, reduce the number of respawns available and execute all registered callbacks.
- */
- var _retry = function() {
- $gameVariables.setValue(_param_variableId, Math.max($gameVariables.value(_param_variableId) - 1, 0));
- $.RESPAWN.callbacks.forEach(function(callback) { callback(); });
- };
- //=============================================================================
- // IAVRA.RESPAWN
- //=============================================================================
- $.RESPAWN = {
- /**
- * Stores all callbacks, that should be executed on respawn.
- */
- callbacks: [],
- /**
- * Registers a new callbacks, to be executed on respawn.
- */
- registerCallback: function(c) { this.callbacks.push(c); },
- /**
- * Removes a registered callbacks.
- */
- removeCallback: function(c) { var i = this.callbacks.indexOf(c); if(i > -1) { this.callbacks.splice(i, 1); } },
- /**
- * The window, that gets displayed during Scene_Gameover to select between respawn and going back to title.
- */
- Window_Retry: function() { this.initialize.apply(this, arguments); }
- };
- //=============================================================================
- // IAVRA.RESPAWN.Window_Retry
- //=============================================================================
- (function($) {
- ($.prototype = Object.create(Window_Command.prototype)).constructor = $;
- /**
- * The window is horizontally centered and starts closed, so it can be opened when starting the scene.
- */
- $.prototype.initialize = function() {
- Window_Command.prototype.initialize.call(this, 0, _param_position);
- this.x = (Graphics.boxWidth - this.width) / 2;
- this.openness = 0;
- };
- /**
- * The windows has 2 commands, one of which gets disabled if no retries are available to the player.
- */
- $.prototype.makeCommandList = function() {
- this.addCommand(_param_labelRetry.replace('{retries}', _numRetries()), 'retry', _numRetries() > 0);
- this.addCommand(_param_labelTitle, 'title', true);
- };
- })($.RESPAWN.Window_Retry);
- //=============================================================================
- // Scene_Gameover
- //=============================================================================
- (function($) {
- /**
- * Add our window to Scene_Gameover and set the command handlers.
- */
- var alias_create = $.prototype.create;
- $.prototype.create = function() {
- alias_create.call(this);
- this.addChild(this._iavra_retry_commandWindow = new IAVRA.RESPAWN.Window_Retry());
- this._iavra_retry_commandWindow.setHandler('retry', _retry.bind(this));
- this._iavra_retry_commandWindow.setHandler('title', this.gotoTitle.bind(this));
- };
- /**
- * On start, open the window for a little extra effect.
- */
- var alias_start = $.prototype.start;
- $.prototype.start = function() {
- alias_start.call(this);
- this._iavra_retry_commandWindow.open();
- };
- /**
- * If we wouldn't overwrite this, the scene would end as soon, as the user presses a button.
- */
- $.prototype.isTriggered = function() { return false; };
- })(Scene_Gameover);
- //=============================================================================
- // register basic callbacks
- //=============================================================================
- /**
- * On retry, recover all actors to full.
- */
- $.RESPAWN.registerCallback(function() { $gameParty.members().forEach(function(actor) { actor.recoverAll(); }); });
- /**
- * On retry, teleport the player to the given location, as indicated by the map notetag. If no tag is present, the
- * current player location is used, instead.
- */
- $.RESPAWN.registerCallback(function() {
- var match = !$dataMap ? null : _regex.exec($dataMap.note);
- if(match) {
- if(match[3] !== undefined) { $gamePlayer.reserveTransfer(match[1]|0, match[2]|0, match[3]|0, null, 0); }
- else { $gamePlayer.reserveTransfer($gameMap.mapId(), match[1]|0, match[2]|0, null, 0); }
- };
- SceneManager.goto(Scene_Map);
- });
- })(IAVRA);
Add Comment
Please, Sign In to add comment