Advertisement
Guest User

Untitled

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