Guest User

Untitled

a guest
Mar 26th, 2016
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvStamina.js
  3. // Version: 1.06
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.06 Basic dashing stamina script.
  7.    <SilverStamina>
  8.  * @author Silver
  9.  *
  10.  * @param -- General --
  11.  *
  12.  * @param Show Stamina Window
  13.  * @desc true/false
  14.  * @default true
  15.  *
  16.  * @param -- Positioning & Size --
  17.  *
  18.  * @param Window X
  19.  * @desc X-location of stamina window. If window-alignment is set to Right, this will act as an offset value instead
  20.  * @default 10
  21.  *
  22.  * @param Window Y
  23.  * @desc Y-location of stamina window. If window-alignment is set to Top, this will act as an offset value instead
  24.  * @default 10
  25.  *
  26.  * @param Window Width
  27.  * @desc width of the stamina window
  28.  * @default 170
  29.  *
  30.  * @param Window Height
  31.  * @desc height of the stamina window
  32.  * @default 72
  33.  *
  34.  * @param Window Horizontal Alignment
  35.  * @desc Left/Right
  36.  * @default Left
  37.  *
  38.  * @param Window Vertical Alignment
  39.  * @desc Top/Bottom
  40.  * @default Top
  41.  *
  42.  * @param Stamina Gauge Rectangle
  43.  * @desc The gauge rectangle. Format: x y width height
  44.  * @default 0 -20 132 24
  45.  *
  46.  * @param -- Stamina Pool --
  47.  *
  48.  * @param Stamina Decrease Mode
  49.  * @desc Use "Default" to decrease while moving. Use "Tile" to decrease stamina whenever the player enters a new tile.
  50.  * @default Default
  51.  *
  52.  * @param Stamina Decrease
  53.  * @desc Amount of stamina subtracted per update (use a positive number)
  54.  * @default 1
  55.  *
  56.  * @param Stamina Max
  57.  * @desc Maximum amount of stamina
  58.  * @default 300
  59.  *
  60.  * @param Stamina Recovery Delay
  61.  * @desc delay in update-calls before recovering stamina when not dashing
  62.  * @default 180
  63.  *
  64.  * @param Stamina Recovery Rate
  65.  * @desc How fast stamina is recovered (only when recovering)
  66.  * @default 0.3
  67.  *
  68.  * @param Stamina AutoDash Threshold
  69.  * @desc Do not automatically dash again before stamina is above this threshold (%) when recovering stamina. (0-100)
  70.  * @default 40
  71.  *
  72.  * @param Stamina Regen Formula
  73.  * @desc The formula for how fast to regenerate stamina over longer period of time. "Base" is the recovery rate. Case sensitive!
  74.  * @default base + Math.sqrt(x/50);
  75.  *
  76.  * @param -- Visuals --
  77.  *
  78.  * @param Stamina Gauge Color 1
  79.  * @desc Bar gradient color1 (start of bar) in hex
  80.  * @default #009900
  81.  *
  82.  * @param Stamina Gauge Color 2
  83.  * @desc Bar gradient color2 (end of bar) in hex
  84.  * @default #CC0000
  85.  *
  86.  * @param Draw Stamina Value
  87.  * @desc Draw text in stamina bar? Accepted values: absolute/percentage/both/none
  88.  * @default percentage
  89.  *
  90.  * @param Font Size
  91.  * @desc Size for stamina value
  92.  * @default 20
  93.  *
  94.  * @param Auto Hide Stamina Window
  95.  * @desc Automatically hide the stamina window if it's at max stamina for a specific period of time? true/false
  96.  * @default true
  97.  *
  98.  * @param -- Window Prefix Text--
  99.  *
  100.  * @param Window Text
  101.  * @desc Use :none to disable this
  102.  * @default :none
  103.  *
  104.  * @param Window Text Offset Y
  105.  * @desc y-coordinate for the text
  106.  * @default 19
  107.  *
  108.  * @param Window Text Spacing X
  109.  * @desc amount of room in coordinates between text and gauge
  110.  * @default 4
  111.  *
  112.  * @param -- Window --
  113.  *
  114.  * @param Hide Stamina Window Delay
  115.  * @desc After how many updates the stamina window should hide itself (if it remains at max stamina)
  116.  * @default 160
  117.  *
  118.  * @param Stamina Window Opacity
  119.  * @desc Stamina window opacity. Set to 0 to hide the window (will still show the bar)
  120.  * @default 255
  121.  *
  122.  * @param Window Slideout Direction
  123.  * @desc What direction to slide the stamina window out to. NoSlide/Left/Top/Right/Bottom
  124.  * @default Left
  125.  *
  126.  * @param Window Slideout Speed
  127.  * @desc How fast the window slides in&out
  128.  * @default 2
  129.  *
  130.  * @param -- Advanced --
  131.  *
  132.  * @param Disable Stamina Consumption GameSwitch
  133.  * @desc The gameswitch to use to disable stamina consumption (or -1 to use none). ON = disabled
  134.  * @default -1
  135.  *
  136.  * @param Window Z-Index
  137.  * @desc Window Z-Index. Value must be > 0.
  138.  * @default 1
  139.  *
  140.  * @param Plugin Command Identifier
  141.  * @desc Do not change if you do not know what this is!
  142.  * @default stamina
  143.  *
  144.  * @help
  145.  * -------------------------------------
  146.  * Plugin Commands (not case sensitive):
  147.  * -------------------------------------
  148.  *
  149.  * Stamina Refill
  150.  * Instantly refills all of your stamina.
  151.  *
  152.  * Stamina Set <value>
  153.  * Instantly sets your stamina to the specified percentage (0-100).
  154.  * Example to set your stamina bar to 64: Stamina Set 64
  155.  *
  156.  * Stamina SetVar <variableIndex>
  157.  * Instantly sets your stamina to the specified percentage (0-100).
  158.  * Example to set the stamina using game-variable #10: Stamina SetVar 10
  159.  *
  160.  * Stamina SetEval <eval>
  161.  * Instantly sets your stamina to the specified percentage (0-100).
  162.  * Example: Example Stamina SetEval ($gameVariables.value(10) + 1) / 2
  163.  *
  164.  * Stamina Deplete
  165.  * Instantly sets your stamina to 0.
  166.  *
  167.  * Stamina ShowWindow
  168.  * Shows the stamina window. Does not work if you disabled the stamina window.
  169.  *
  170.  * Stamina HideWindow
  171.  * Hides the stamina window. Does not work if you disabled the stamina window.
  172.  * Also does not work if the stamina is currently regenerating.
  173.  *
  174.  * Stamina RefillHide
  175.  * Instantly refills all of your stamina and also hides the stamina window.
  176.  *
  177.  * Stamina SetMax <value>
  178.  * Sets a new max-stamina value.
  179.  *
  180.  * Stamina SetMaxVar <variableIndex>
  181.  * Sets a new max-stamina value.
  182.  * Example to set the max-stamina using game-variable #10: Stamina SetMaxVar 10
  183.  *
  184.  * Stamina SetMaxEval <eval>
  185.  * Sets a new max-stamina value. But this time it evaluates the code you enter as <eva>.
  186.  * Example: Stamina SetMaxEval ($gameVariables.value(10) + 1) / 2
  187.  * The above example assigns a new max-stamina using the game-variable 10 and then adding a 1 and then dividing it all by 2.
  188.  *
  189.  * Stamina IncreaseMax <value>
  190.  * Increases max stamina by the specified value. You can also use negative values.
  191.  *
  192.  * Stamina EnableDashing <true/false/toggle>
  193.  * Allows or prohibits dashing on the map.
  194.  * Examples:
  195.  * Stamina EnableDashing true
  196.  * Stamina EnableDashing false
  197.  * Stamina EnableDashing toggle
  198.  * -------------------------------------
  199.  * Map Notetags
  200.  * -------------------------------------
  201.  * <dstam_disable>
  202.  * Prevents stamina consumption on this map. Not that if the "Disable Stamina Consumption GameSwitch"
  203.  * is turned ON then you consume no stamina anyway.
  204.  *
  205.  * <disable_dashing>
  206.  * Prevents dashing on the map (with or without stamina).
  207.  *
  208.  * -------------------------------------
  209.  * Item Notetags (not case sensitive)
  210.  * -------------------------------------
  211.  * <dash_stamina:command (value)>
  212.  * Available commands:
  213.  *  - Add (may have a negative value)
  214.  *  - Refill
  215.  *  - Deplete
  216.  *  - IncreaseMax (may have a negative value)
  217.  * Examples:
  218.  * <dash_stamina:Add 10>
  219.  * <dash_stamina:Add -10>
  220.  * <dash_stamina:Refill>
  221.  * <dash_stamina:Deplete>
  222.  * <dash_stamina:IncreaseMax 1500>
  223.  * Note that those only work from the menu, not from battle
  224.  *
  225.  *--------------------------------------
  226.  * Version History:
  227.  *--------------------------------------
  228.  * v1.06 (26 March 2016)
  229.  * - Added new plugin commands: SetVar, SetEval, SetMaxEval & SetMaxVar.
  230.  * - The plugin command "Stamina EnableDashing" now accepts a third value: toggle.
  231.  *
  232.  * v1.05 (21 March 2016)
  233.  * - Added a new parameter "Stamina Decrease Mode". Now stamina can also be decreased on a per-tile-basis instead of only on a per-update-cycle-basis.
  234.  * - Added new functions and a new alias to accomodate the above new feature.
  235.  *
  236.  * v1.04 (03 February 2016)
  237.  * - Fixed a crash when transferring between maps using an autorun event with manual fading in or out (autorun stops execution of other events and some scripts like this one, which caused the crash).
  238.  *
  239.  * v1.03 (01 January 2016)
  240.  * - Used my new coding standards & refactored.
  241.  * - Switched to the Imported variable.
  242.  * - Fixed an accidental duplicate alias.
  243.  * - Fixed a bug in Scene_Map.prototype.updateMain() (which just so happened to cause no side-effects).
  244.  *
  245.  * v1.02 (12 December 2015)
  246.  * - Dashing for the current map can now be entirely disabled with a map-notetag and switched with a plugin command at any time.
  247.  * - Created a simple plugin addon to store the player-stamina in a global game-variable.
  248.  * - Enabled custom text to be drawn in front of the Stamina Window.
  249.  * - New feature: items can replenish, lower, refill and deplete stamina (only in the menu, not in battle).
  250.  *
  251.  * v1.01 (1 December 2015)
  252.  * - Removed strict-mode because... Possible bug in RPG Maker...
  253.  * - Refactored and fixed semicolons and missing var-keywords.
  254.  *
  255.  * v1.00 (26 November 2015)
  256.  * - First release.
  257.  * Alpha (November 2015)
  258.  * - First alpha release.
  259.  *
  260.  */
  261. // Imported
  262. var Imported = Imported || {};
  263. Imported.Silv_DashStamina = 1.06;
  264.  
  265. // #Parameters
  266. var Silv = Silv || {};
  267. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilverStamina>'); })[0].parameters;
  268. Silv.DashStamina = Silv.DashStamina || {};
  269. // Non-parameters
  270. Silv.DashStamina.Window = null;
  271. Silv.DashStamina.ScreenIsFading = false;
  272. Silv.DashStamina.DashingDisabled = false;
  273. // General
  274. Silv.DashStamina.ShowWindow = Silv.Parameters['Show Stamina Window'].toLowerCase() === 'true';
  275. // Positioning & Size
  276. Silv.DashStamina.Window_X = parseInt(Silv.Parameters['Window X']);
  277. Silv.DashStamina.Window_Y = parseInt(Silv.Parameters['Window Y']);
  278. Silv.DashStamina.WindowWidth = parseInt(Silv.Parameters['Window Width']);
  279. Silv.DashStamina.WindowHeight = parseInt(Silv.Parameters['Window Height']);
  280. Silv.DashStamina.WindowHorizontalAlignment = (Silv.Parameters['Window Horizontal Alignment']).toLowerCase();
  281. Silv.DashStamina.WindowVerticalAlignment = (Silv.Parameters['Window Vertical Alignment']).toLowerCase();
  282. Silv.DashStamina.StaminaGaugeRectangle = {x: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[0]), y: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[1]), width: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[2]), height: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[3])};
  283. // Stamina Pool
  284. Silv.DashStamina.DecreaseMode = Silv.Parameters['Stamina Decrease Mode'].toLowerCase();
  285. Silv.DashStamina.StaminaDecrease = parseInt(Silv.Parameters['Stamina Decrease']);
  286. Silv.DashStamina.StaminaMax = parseInt(Silv.Parameters['Stamina Max']);
  287. Silv.DashStamina.StaminaRecoveryDelay = parseInt(Silv.Parameters['Stamina Recovery Delay']);
  288. Silv.DashStamina.StaminaRecoveryRate = parseInt(Silv.Parameters['Stamina Recovery Rate']);
  289. Silv.DashStamina.StaminaAutoDashThreshold = parseInt(Silv.Parameters['Stamina AutoDash Threshold']);
  290. Silv.DashStamina.RegenFormula = Silv.Parameters['Stamina Regen Formula'];
  291. // Visuals
  292. Silv.DashStamina.StaminaGaugeColor1 = (Silv.Parameters['Stamina Gauge Color 1']).toUpperCase();
  293. Silv.DashStamina.StaminaGaugeColor2 = (Silv.Parameters['Stamina Gauge Color 2']).toUpperCase();
  294. Silv.DashStamina.DrawStaminaValue = Silv.Parameters['Draw Stamina Value'].toLowerCase();
  295. Silv.DashStamina.FontSize = parseInt(Silv.Parameters['Font Size']);
  296. // Window Prefix Text
  297. Silv.DashStamina.WindowText = Silv.Parameters['Window Text'];
  298. Silv.DashStamina.WindowTextOffsetY = parseInt(Silv.Parameters['Window Text Offset Y']);
  299. Silv.DashStamina.WindowTextGaugeSpacingX = parseInt(Silv.Parameters['Window Text Spacing X']);
  300. // Window
  301. Silv.DashStamina.AutoHideStaminaWindow = Silv.Parameters['Auto Hide Stamina Window'].toLowerCase() === 'true';
  302. Silv.DashStamina.HideStaminaWindowDelay = parseInt(Silv.Parameters['Hide Stamina Window Delay']);
  303. Silv.DashStamina.WindowOpacity = parseInt(Silv.Parameters['Stamina Window Opacity']);
  304. Silv.DashStamina.WindowSlideOutDir = Silv.Parameters['Window Slideout Direction'].toLowerCase();
  305. Silv.DashStamina.WindowSlideOutSpeed = parseFloat(Silv.Parameters['Window Slideout Speed']);
  306. // Advanced
  307. Silv.DashStamina.DisableGameSwitch = parseInt(Silv.Parameters['Disable Stamina Consumption GameSwitch']);
  308. Silv.DashStamina.Window_Z = parseInt(Silv.Parameters['Window Z-Index']);
  309. Silv.DashStamina.PluginCmdId = Silv.Parameters['Plugin Command Identifier'];
  310.  
  311. // Alias
  312. Silv.Alias = Silv.Alias || {};
  313. if (!Silv.AddAlias)
  314. {
  315.     Silv.AddAlias = function(alias, original_method)
  316.     {
  317.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  318.         Silv.Alias[alias] = original_method;
  319.     };
  320. }
  321.  
  322. // Sanity checks
  323. (function()
  324. {
  325.     if ((Silv.DashStamina.DecreaseMode !== 'default') && (Silv.DashStamina.DecreaseMode !== 'tile')) { throw new Error('Invalid parameter-value for "Decrease Mode": ' + Silv.DashStamina.DecreaseMode + '.'); }
  326. })();
  327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  328. (function()
  329. {
  330. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. // Utilities
  332. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  333.  
  334. // Usage: alert( hexToRgb("#0033ff").g ); // "51";
  335. function hexToRgb(hex)
  336. {
  337.     var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  338.     return result ? {
  339.         r: parseInt(result[1], 16),
  340.         g: parseInt(result[2], 16),
  341.         b: parseInt(result[3], 16)
  342.     } : null;
  343. }
  344.  
  345. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  346. // Game Player
  347. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  348. Game_Player.prototype.hasStaminaConsumption = function()
  349. {
  350.     if ($gameSwitches.value(Silv.DashStamina.DisableGameSwitch)) { return false; }
  351.     if (!this.mapConsumeStamina) { return false; }
  352.     return true;
  353. };
  354.  
  355. // Alias for whenever the player entered a new tile
  356. Silv.AddAlias('dashStamina_Game_Player_updateMove', Game_Player.prototype.updateMove);
  357. Game_Player.prototype.updateMove = function()
  358. {
  359.     Silv.Alias.dashStamina_Game_Player_updateMove.apply(this, arguments);
  360.     if ((Silv.DashStamina.DecreaseMode === 'tile') && !this.isMoving() && this.isDashing() && this.hasStaminaConsumption())
  361.     {
  362.         // Consume stamina for the tile-decrease-mode
  363.         this.dashStamina -= Silv.DashStamina.StaminaDecrease;
  364.         this.postDecreaseStaminaHandling();
  365.     }
  366. };
  367.  
  368. Game_Player.prototype.isConsumingStamina = function()
  369. {
  370.     return (this.isDashing() && this.isMoving() && this.hasStaminaConsumption());
  371. };
  372.  
  373. // Call this method after decreasing the stamina
  374. Game_Player.prototype.postDecreaseStaminaHandling = function()
  375. {
  376.     if (this.dashStamina < 0) { this.dashStamina = 0; }
  377.     this.isRecoveringStamina = false;
  378.     if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  379. };
  380.  
  381. // #Update stamina amount
  382. Game_Player.prototype.updateStamina = function()
  383. {
  384.     var isConsumingStamina = this.isConsumingStamina();
  385.     if (isConsumingStamina)
  386.     {
  387.         if (Silv.DashStamina.DecreaseMode === 'default') { this.dashStamina -= Silv.DashStamina.StaminaDecrease; } // Only consume stamina here if the decrease-mode is set to default
  388.         this.postDecreaseStaminaHandling();
  389.     }
  390.     else // not currently consuming stamina
  391.     {      
  392.         if (this.isRecoveringStamina)
  393.         {
  394.             this.staminaRecoveryTimeCnt++;
  395.             if (this.dashStamina < this.dashStaminaMax) // Recover the stamina
  396.             {
  397.                 // Recover stamina
  398.                 this.dashStamina += this.calculateStaminaRegen(Silv.DashStamina.StaminaRecoveryRate);
  399.                 Silv.DashStamina.clampMaxStamina();
  400.                
  401.                 if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  402.             }
  403.             else // Already at max stamina, find out when to hide the window if applicable and do so if possible
  404.             {
  405.                 // If the stamina window is used, attempt to autohide it if applicable
  406.                 if (Silv.DashStamina.ShowWindow && Silv.DashStamina.AutoHideStaminaWindow)
  407.                 {
  408.                     this.hideStaminaWindowDelayCnt += 1;
  409.                     if (this.hideStaminaWindowDelayCnt >= Silv.DashStamina.HideStaminaWindowDelay)
  410.                     {
  411.                         Silv.DashStamina.Window.hideMe();
  412.                         this.hideStaminaWindowDelayCnt = 0;
  413.                     }
  414.                 }
  415.                    
  416.             }
  417.         }
  418.         else // not currently recovering stamina, so find out when to start recovering it and do so if required
  419.         {
  420.             this.staminaRecoveryTimeCnt = 0;
  421.             this.staminaRecoveryDelayCnt += 1;
  422.             if (this.staminaRecoveryDelayCnt >= Silv.DashStamina.StaminaRecoveryDelay)
  423.             {
  424.                 this.staminaRecoveryDelayCnt = 0;
  425.                 this.isRecoveringStamina = true;
  426.             }
  427.         }
  428.     }
  429.  
  430.     // Stamina Percentage
  431.     this.dashStaminaPerc = this.dashStamina / parseFloat(this.dashStaminaMax);
  432.  
  433.     // Threshold
  434.     if (!isConsumingStamina)
  435.     {
  436.         this.requiresThresholdAmount = this.dashStaminaPerc * 100 < Silv.DashStamina.StaminaAutoDashThreshold;
  437.     }
  438. };
  439.  
  440. Game_Player.prototype.calculateStaminaRegen = function()
  441. {
  442.     var base = Silv.DashStamina.StaminaRecoveryRate;
  443.     var x = this.staminaRecoveryTimeCnt;
  444.     return eval(Silv.DashStamina.RegenFormula);
  445. };
  446.  
  447. // #Initialize
  448. Silv.AddAlias('dashStamina_Game_Player_initialize', Game_Player.prototype.initialize);
  449. Game_Player.prototype.initialize = function()
  450. {
  451.     Silv.Alias.dashStamina_Game_Player_initialize.apply(this, arguments);
  452.  
  453.     this.dashStamina = this.dashStaminaMax = Silv.DashStamina.StaminaMax;
  454.     this.staminaRecoveryDelayCnt = 0; // counter for when to start recovering stamina
  455.     this.staminaRecoveryTimeCnt = 0; // counter for how long the player has been recovering stamina (in frames)
  456.     this.isRecoveringStamina = false;
  457.     this.dashStaminaPerc = 1.0;
  458.     this.hideStaminaWindowDelayCnt = 0;
  459.     this.requiresThresholdAmount = false;
  460.     this.wasDashing = false;
  461. };
  462.  
  463. Game_Player.prototype.dashingAllowed = function()
  464. {
  465.     if (this.dashStamina === 0 ||
  466.        (!this.wasDashing && this.requiresThresholdAmount)) // Do not allow to dash if the player was not dashing the previous frame AND if the threshold was passed.
  467.     {
  468.         return false;
  469.     }
  470.    
  471.     return true;
  472. };
  473.  
  474. Silv.AddAlias('dashStamina_Game_Player_updateDashing', Game_Player.prototype.updateDashing);
  475. Game_Player.prototype.updateDashing = function()
  476. {
  477.     Silv.Alias.dashStamina_Game_Player_updateDashing.apply(this, arguments);
  478.    
  479.     if (Silv.DashStamina.DashingDisabled)
  480.     {
  481.         this._dashing = false;
  482.         return;
  483.     }
  484.    
  485.     if (!this.dashingAllowed()) { this._dashing = false; }
  486.     this.wasDashing = this._dashing;
  487. };
  488.  
  489. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  490. // Game Screen
  491. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. // Do not just show the minimap on top of a faded-out screen.
  493. Silv.AddAlias('dashStamina_Game_Screen_updateFadeOut', Game_Screen.prototype.updateFadeOut);
  494. Game_Screen.prototype.updateFadeOut = function()
  495. {
  496.     Silv.Alias.dashStamina_Game_Screen_updateFadeOut.apply(this, arguments);
  497.    
  498.     if (this._brightness < 255) // (this._fadeOutDuration > 0)
  499.     {
  500.         if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  501.         Silv.DashStamina.ScreenIsFading = true;
  502.     }
  503.     else
  504.     {
  505.         Silv.DashStamina.ScreenIsFading = false;
  506.     }
  507. };
  508.  
  509. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  510. // Scene Base
  511. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  512. Silv.AddAlias('dashStamina_Scene_Base_startFadeIn', Scene_Base.prototype.startFadeIn);
  513. Scene_Base.prototype.startFadeIn = function()
  514. {
  515.     Silv.Alias.dashStamina_Scene_Base_startFadeIn.apply(this, arguments);
  516.     if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  517. };
  518.  
  519. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  520. // Scene Map
  521. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  522. // Hook into the main loop
  523. Silv.AddAlias('dashStamina_Scene_Map_updateMain', Scene_Map.prototype.updateMain);
  524. Scene_Map.prototype.updateMain = function()
  525. {
  526.     Silv.Alias.dashStamina_Scene_Map_updateMain.apply(this, arguments);
  527.     $gamePlayer.updateStamina();
  528.     if (Silv.DashStamina.ShowWindow) { Silv.DashStamina.Window.update(); }
  529. };
  530.  
  531. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  532. // Game Map
  533. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  534. // Disable Dashing?
  535. Silv.AddAlias('dashStamina_Game_Map_setup', Game_Map.prototype.setup);
  536. Game_Map.prototype.setup = function(mapId)
  537. {
  538.     Silv.Alias.dashStamina_Game_Map_setup.apply(this, arguments);
  539.     Silv.DashStamina.DashingDisabled = ('disable_dashing' in $dataMap.meta);
  540. };
  541.  
  542. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  543. // Stamina Regen/Deplete #Items
  544. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  545. Silv.AddAlias('dashStamina_Scene_ItemBase_useItem', Scene_ItemBase.prototype.useItem);
  546. Scene_ItemBase.prototype.useItem = function()
  547. {
  548.     var item = this.item();
  549.     if ('dash_stamina' in item.meta)
  550.     {
  551.         var args = item.meta.dash_stamina.split(' ');
  552.         switch(args[0].toLowerCase())
  553.         {
  554.             case 'add':
  555.                 if (args.length < 2) { throw 'Item-add-command is missing the value-argument.'; }
  556.                 $gamePlayer.dashStamina += parseInt(args[1]);
  557.                 if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  558.                 else if ($gamePlayer.dashStamina < 0) { $gamePlayer.dashStamina = 0; }
  559.                 break;
  560.             case 'refill':
  561.                 $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  562.                 break;
  563.             case 'deplete':
  564.                 $gamePlayer.dashStamina = 0;
  565.                 break;
  566.             case 'increasemax':
  567.                 if (args.length < 2) { throw 'Item-increasemax-command is missing the value-argument.'; }
  568.                 $gamePlayer.dashStaminaMax += parseInt(args[1]);
  569.                 if ($gamePlayer.dashStaminaMax < 1 ) { $gamePlayer.dashStaminaMax = 1; }
  570.                 break;
  571.             default:
  572.                 throw 'Unknown dash_stamina itemnotetag: ' + item.meta;
  573.         }
  574.        
  575.     }
  576.    
  577.     Silv.Alias.dashStamina_Scene_ItemBase_useItem.apply(this, arguments);
  578. };
  579.  
  580. Silv.AddAlias('dashStamina_Game_Action_testApply', Game_Action.prototype.testApply);
  581. Game_Action.prototype.testApply = function(target)
  582. {
  583.     if ('dash_stamina' in this.item().meta)
  584.     {
  585.         return true;
  586.     }
  587.     else
  588.     {
  589.         return Silv.Alias.dashStamina_Game_Action_testApply.apply(this, arguments);
  590.     }
  591. };
  592. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  593. // DashStamina #Window
  594. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  595. function Window_DashStamina() { this.initialize.apply(this, arguments); }
  596. Window_DashStamina.prototype = Object.create(Window_Base.prototype);
  597. Window_DashStamina.prototype.constructor = Window_DashStamina;
  598. // Font Size
  599. Window_DashStamina.prototype.standardFontSize = function() { return Silv.DashStamina.FontSize; };
  600.  
  601. // #Initialize
  602. Window_DashStamina.prototype.initialize = function(x, y, width, height)
  603. {
  604.     Window_Base.prototype.initialize.call(this, x, y, width, height);
  605.     this._helpWindow = null;
  606.     this._handlers = {};
  607.     this._touching = false;
  608.     this.deactivate();
  609.    
  610.     this.opacity = Silv.DashStamina.WindowOpacity;
  611.     this.slideDirection = {x: 0, y: 0 };
  612.     this.originalWinLoc = {x: 0, y: 0 };
  613.     this.sliding = 'none';
  614.     this.isFullySlidedOut = false;
  615.     this.isFullySlidedIn = true;
  616.     this.originalWinLoc.x = x; // for some reason "this." is not allowed here
  617.     this.originalWinLoc.y = y; // for some reason "this." is not allowed here
  618.    
  619.     if (Silv.DashStamina.WindowText != ':none')
  620.     {
  621.         this.windowText = Silv.DashStamina.WindowText;
  622.         this.windowTextWidth = this.contents.measureTextWidth(this.windowText);
  623.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  624.     }
  625.     else
  626.     {
  627.         this.windowText = null;
  628.         this.windowTextWidth = 0;
  629.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  630.     }
  631.    
  632.     this.update();
  633. };
  634.  
  635. // Update
  636. Window_DashStamina.prototype.update = function()
  637. {
  638.     if (Silv.DashStamina.ScreenIsFading)
  639.     {
  640.         this.visible = false;
  641.     }
  642.     else
  643.     {
  644.         Window_Base.prototype.update.call(this);
  645.         this.drawStaminaWindow(0, 0, Silv.DashStamina.StaminaGaugeRectangle.width);
  646.         this.updateSliding();
  647.     }
  648. };
  649.  
  650. //----------------------------------------------------------------------------------------------------
  651. // #Drawing
  652. //----------------------------------------------------------------------------------------------------
  653. Window_DashStamina.prototype.drawStaminaGauge = function(x, y, width, height, rate)
  654. {
  655.     var fillW = Math.floor(width * rate);
  656.     var gaugeY = y + this.lineHeight() - 8;
  657.     this.contents.fillRect(x, gaugeY, width, height, this.gaugeBackColor());
  658.     this.contents.fillRect(x, gaugeY, fillW, height, this.CalculateBarColour());
  659. };
  660.  
  661. Window_DashStamina.prototype.drawStaminaWindow = function(x, y, width)
  662. {
  663.     this.contents.clear();
  664.    
  665.     // Draw text before the gauge
  666.     if (this.windowText !== null)
  667.     {
  668.         this.contents.drawText(this.windowText, 0, Silv.DashStamina.WindowTextOffsetY, this.windowTextWidth, 0);
  669.     }
  670.  
  671.     // Draw gauge
  672.     this.drawStaminaGauge(Silv.DashStamina.StaminaGaugeRectangle.x + this.windowTextWidth + this.windowTextGaugeSpacingX, Silv.DashStamina.StaminaGaugeRectangle.y, Silv.DashStamina.StaminaGaugeRectangle.width, Silv.DashStamina.StaminaGaugeRectangle.height, $gamePlayer.dashStaminaPerc);
  673.     // Draw text on top of the gauge
  674.     var text;
  675.     switch(Silv.DashStamina.DrawStaminaValue) // allowed values: absolute/percentage/both/none
  676.     {
  677.         case 'absolute':
  678.             text = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax);
  679.             break;
  680.         case 'percentage':
  681.             text = Math.round($gamePlayer.dashStaminaPerc * 100) + '%';
  682.             break;
  683.         case 'both':
  684.             text = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax) + ' (' + Math.round($gamePlayer.dashStaminaPerc * 100) + '%)';
  685.             break;
  686.         case 'none':
  687.             return;
  688.         default:
  689.             throw 'ERROR: drawStaminaWindow missing case-statement or incorrect Silv.DashStamina.DrawStaminaValue value. Value: ' + Silv.DashStamina.DrawStaminaValue;
  690.     }
  691.    
  692.     this.resetTextColor();
  693.     this.drawText(text, x + this.windowTextWidth + this.windowTextGaugeSpacingX, y + 1, width, 'center');
  694. };
  695.  
  696. // Calculate what colour the gauge should be between the two colors depending on the percentage value of the current-stamina value.
  697. Window_DashStamina.prototype.CalculateBarColour = function()
  698. {
  699.     var c1 = hexToRgb(Silv.DashStamina.StaminaGaugeColor1);
  700.     var c2 = hexToRgb(Silv.DashStamina.StaminaGaugeColor2);
  701.        
  702.     var ratio = $gamePlayer.dashStaminaPerc;
  703.     var hex = function(x) {
  704.         x = x.toString(16);
  705.         return (x.length === 1) ? '0' + x : x;
  706.     };
  707.  
  708.     var r = Math.ceil(c1.r * ratio + c2.r * (1-ratio));
  709.     var g = Math.ceil(c1.g * ratio + c2.g * (1-ratio));
  710.     var b = Math.ceil(c1.b * ratio + c2.b * (1-ratio));
  711.  
  712.     var middle = '#' + hex(r) + hex(g) + hex(b);
  713.     return middle;
  714. };
  715.  
  716. //----------------------------------------------------------------------------------------------------
  717. // #Show & #Hide
  718. //----------------------------------------------------------------------------------------------------
  719. Window_DashStamina.prototype.showMe = function()
  720. {
  721.     //if (this.visible) { return; };
  722.     if (this.isFullySlidedIn)
  723.     {
  724.         this.visible = true;
  725.         return;
  726.     }
  727.    
  728.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide' || this.visible === false) { this.visible = true; }
  729.     else
  730.     {
  731.         this.sliding = 'in';
  732.         switch (Silv.DashStamina.WindowSlideOutDir)
  733.         {
  734.             case 'top':
  735.                 this.slideDirection.x = 0;
  736.                 this.slideDirection.y = 1;
  737.                 break;
  738.             case 'left':
  739.                 this.slideDirection.x = 1;
  740.                 this.slideDirection.y = 0;
  741.                 break;
  742.             case 'right':
  743.                 this.slideDirection.x = -1;
  744.                 this.slideDirection.y = 0;
  745.                 break;
  746.             case 'bottom':
  747.                 this.slideDirection.x = 0;
  748.                 this.slideDirection.y = -1;
  749.                 break;
  750.             default:
  751.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  752.         }
  753.     }
  754. };
  755.  
  756. Window_DashStamina.prototype.hideMe = function()
  757. {
  758.     if (!this.visible || this.isFullySlidedOut) { return; }
  759.    
  760.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide') { this.visible = false; }
  761.     else
  762.     {
  763.         this.sliding = 'out';
  764.         switch (Silv.DashStamina.WindowSlideOutDir)
  765.         {
  766.             case 'top':
  767.                 this.slideDirection.x = 0;
  768.                 this.slideDirection.y = -1;
  769.                 break;
  770.             case 'left':
  771.                 this.slideDirection.x = -1;
  772.                 this.slideDirection.y = 0;
  773.                 break;
  774.             case 'right':
  775.                 this.slideDirection.x = 1;
  776.                 this.slideDirection.y = 0;
  777.                 break;
  778.             case 'bottom':
  779.                 this.slideDirection.x = 0;
  780.                 this.slideDirection.y = 1;
  781.                 break;
  782.             default:
  783.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  784.         }
  785.     }
  786. };
  787.  
  788. //----------------------------------------------------------------------------------------------------
  789. // #Sliding of the Window
  790. //----------------------------------------------------------------------------------------------------
  791. Window_DashStamina.prototype.handleSlidingEnd = function()
  792. {
  793.     if (this.sliding === 'in')
  794.     {
  795.         // Stop sliding in
  796.         if (this.slideDirection.x  === 1 && this.x > this.originalWinLoc.x ||
  797.             this.slideDirection.x  === -1 && this.x < this.originalWinLoc.x ||
  798.             this.slideDirection.y  === 1 && this.y > this.originalWinLoc.y ||
  799.             this.slideDirection.y  === -1 && this.y < this.originalWinLoc.y)
  800.         {
  801.             this.sliding = 'none';
  802.             this.isFullySlidedIn = true;
  803.         }
  804.     }
  805.     else
  806.     {
  807.         // Stop sliding out
  808.         if (this.x < -this.width || this.x > Graphics._width + this.width ||
  809.             this.y < -this.height || this.x > Graphics._height + this.height)
  810.         {
  811.             this.sliding = 'none';
  812.             this.isFullySlidedOut = true;
  813.         }
  814.     }
  815. };
  816.  
  817. Window_DashStamina.prototype.updateSliding = function()
  818. {
  819.     if (this.sliding === 'none') { return; }
  820.     this.x += this.slideDirection.x * Silv.DashStamina.WindowSlideOutSpeed;
  821.     this.y += this.slideDirection.y * Silv.DashStamina.WindowSlideOutSpeed;
  822.    
  823.     this.isFullySlidedOut = false;
  824.     this.isFullySlidedIn = false;
  825.    
  826.     this.handleSlidingEnd();
  827. };
  828.  
  829. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  830. // #Create Stamina Window
  831. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  832. Scene_Map.prototype.createDashWindow = function()
  833. {
  834.     // Dispose the old window, if any
  835.     if (Silv.DashStamina.Window !== null) { this.removeWindow(Silv.DashStamina.Window); }
  836.    
  837.     // Does the map consume stamina?
  838.     $gamePlayer.mapConsumeStamina = !('dstam_disable' in $dataMap.meta);
  839.     if(Silv.DashStamina.ShowWindow)
  840.     {
  841.         var x = 0;
  842.         if (Silv.DashStamina.WindowHorizontalAlignment === 'right') { x = Graphics.width - Silv.DashStamina.WindowWidth; }
  843.         var y = 0;
  844.         if (Silv.DashStamina.WindowVerticalAlignment === 'bottom') { y = Graphics.height - Silv.DashStamina.WindowHeight; }
  845.        
  846.         Silv.DashStamina.Window = new Window_DashStamina(x + Silv.DashStamina.Window_X, y + Silv.DashStamina.Window_Y, Silv.DashStamina.WindowWidth, Silv.DashStamina.WindowHeight);
  847.        
  848.         this.addChild(Silv.DashStamina.Window, Silv.DashStamina.Window_Z);
  849.         if (Silv.DashStamina.AutoHideStaminaWindow) { Silv.DashStamina.Window.visible = false; }
  850.     }
  851. };
  852.  
  853. // Omg why does RPG Maker not have this method by default...
  854. Scene_Base.prototype.removeWindow = function(window)
  855. {
  856.     var index = this.children.indexOf(window);
  857.     if (index > -1) { this.children.splice(index, 1); }
  858. };
  859.  
  860. Silv.AddAlias('dashStamina_Scene_Map_createDisplayObjects', Scene_Map.prototype.createDisplayObjects);
  861. Scene_Map.prototype.createDisplayObjects = function()
  862. {
  863.     Silv.Alias.dashStamina_Scene_Map_createDisplayObjects.apply(this, arguments);
  864.     this.createDashWindow();
  865. };
  866.  
  867. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  868. // Saving & Loading
  869. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  870. Silv.AddAlias('dashStamina_DataManager_makeSaveContents', DataManager.makeSaveContents);
  871. DataManager.makeSaveContents = function()
  872. {
  873.     contents = Silv.Alias.dashStamina_DataManager_makeSaveContents.apply(this, arguments);
  874.     contents.dashStamina = $gamePlayer.dashStamina;
  875.     contents.dashStaminaMax = $gamePlayer.dashStaminaMax;
  876.     return contents;
  877. };
  878.  
  879. Silv.AddAlias('dashStamina_DataManager_extractSaveContents', DataManager.extractSaveContents);
  880. DataManager.extractSaveContents = function(contents)
  881. {
  882.     Silv.Alias.dashStamina_DataManager_extractSaveContents.apply(this, arguments);
  883.     $gamePlayer.dashStamina = contents.dashStamina;
  884.     $gamePlayer.dashStaminaMax = contents.dashStaminaMax;
  885. };
  886.  
  887. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  888. // Plugin Command
  889. // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
  890. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  891. Silv.AddAlias('dashStamina_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
  892. Game_Interpreter.prototype.pluginCommand = function(command, args)
  893. {
  894.     Silv.Alias.dashStamina_Game_Interpreter_pluginCommand.apply(this, arguments);
  895.     if (command.toLowerCase() === Silv.DashStamina.PluginCmdId) { Silv.DashStamina.PluginCommand(command, args); }
  896. };
  897.  
  898. // Concatenate the 'arguments' starting at a specific index because they are all part of an eval.
  899. Silv.DashStamina.concatArgs = function(args, startIdx)
  900. {
  901.     var evalstr = '';
  902.     for (var argIdx = startIdx; argIdx < args.length; argIdx++)
  903.     {
  904.         evalstr += args[argIdx] + ' ';
  905.     }
  906.     return evalstr;
  907. };
  908.  
  909. // newValue must be between 0 - 100.
  910. Silv.DashStamina.assignNewStaminaValue = function(newValue)
  911. {
  912.     var perc = Math.max(0, Math.min(100, newValue)); // clamp value between 0-100
  913.     $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax * (perc / 100.0);
  914. };
  915.  
  916. Silv.DashStamina.clampMaxStamina = function()
  917. {
  918.     if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  919. };
  920.  
  921. Silv.DashStamina.PluginCommand = function(cmd, args)
  922. {
  923.     switch(args[0].toLowerCase())
  924.     {
  925.         case 'refill':
  926.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  927.             break;
  928.         case 'deplete':
  929.             $gamePlayer.dashStamina = 0;
  930.             break;
  931.         case 'set':
  932.             Silv.DashStamina.assignNewStaminaValue(parseInt(args[1]));
  933.         break;
  934.         case 'setvar':
  935.             Silv.DashStamina.assignNewStaminaValue(parseInt($gameVariables.value(args[1])));
  936.         break;
  937.         case 'seteval':
  938.             var perc = parseInt(eval(Silv.DashStamina.concatArgs(args, 1)));
  939.             if (isNaN(perc)) { throw new Error('Plugin command: "Stamina SetEval" evaled to a NaN value: ' + perc + '.'); }
  940.             Silv.DashStamina.assignNewStaminaValue(perc);
  941.         break;
  942.         case 'showwindow':
  943.             if (Silv.DashStamina.Window !== null)
  944.             {
  945.                 $gamePlayer.hideStaminaWindowDelayCnt = 0;
  946.                 Silv.DashStamina.Window.visible = true;
  947.             }
  948.             break;
  949.         case 'refillhide':
  950.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  951.             // NO break-statement here! We want to hide the window as well!
  952.         case 'hidewindow':
  953.             if (Silv.DashStamina.Window !== null)
  954.             {
  955.                 $gamePlayer.hideStaminaWindowDelayCnt = Silv.DashStamina.HideStaminaWindowDelay;
  956.                 Silv.DashStamina.Window.visible = false;
  957.             }
  958.             break;
  959.         case 'setmax':
  960.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(args[1]));
  961.             Silv.DashStamina.clampMaxStamina();
  962.             break;
  963.         case 'setmaxeval':
  964.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(eval(Silv.DashStamina.concatArgs(args, 1))));
  965.             Silv.DashStamina.clampMaxStamina();
  966.             break;
  967.         case 'setmaxvar':
  968.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt($gameVariables.value(args[1])));
  969.             Silv.DashStamina.clampMaxStamina();
  970.             break;
  971.         case 'increasemax':
  972.             $gamePlayer.dashStaminaMax += parseInt(args[1]);
  973.             if ($gamePlayer.dashStaminaMax < 1) { $gamePlayer.dashStaminaMax = 1; }
  974.             if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  975.             break;
  976.         case 'enabledashing':
  977.             switch(args[1].toLowerCase())
  978.             {
  979.                 case 'true':
  980.                     Silv.DashStamina.DashingDisabled = false;
  981.                     break;
  982.                 case 'false':
  983.                     Silv.DashStamina.DashingDisabled = true;
  984.                     break;
  985.                 case 'toggle':
  986.                     Silv.DashStamina.DashingDisabled = !Silv.DashStamina.DashingDisabled;
  987.                     break;
  988.                 default:
  989.                     throw new Error('"Stamina EnableDashing" received an unknown argument: ' + args[1] + '. Expected: True, False or Toggle.');
  990.             }
  991.             break;
  992.         default:
  993.             throw 'Stamina PluginCommand invalid command: ' + args[0];
  994.     }
  995. };
  996.  
  997. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  998. })();
  999. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1000. // This is the end of this awesome script!
  1001. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RAW Paste Data