Advertisement
Guest User

Untitled

a guest
Jun 5th, 2016
1,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvStamina.js
  3. // Version: 1.10
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.10 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.  * Stamina ForceHide <true/false>
  219.  * Example:
  220.  * Stamina ForceHide true
  221.  *
  222.  * -------------------------------------
  223.  * Map Notetags
  224.  * -------------------------------------
  225.  * <dstam_disable>
  226.  * Prevents stamina consumption on this map. Not that if the "Disable Stamina Consumption GameSwitch"
  227.  * is turned ON then you consume no stamina anyway.
  228.  *
  229.  * <disable_dashing>
  230.  * Prevents dashing on the map (with or without stamina).
  231.  *
  232.  * -------------------------------------
  233.  * Item Notetags (not case sensitive)
  234.  * -------------------------------------
  235.  * <dash_stamina:command (value)>
  236.  * Available commands:
  237.  *  - Add (may have a negative value)
  238.  *  - Refill
  239.  *  - Deplete
  240.  *  - IncreaseMax (may have a negative value)
  241.  * Examples:
  242.  * <dash_stamina:Add 10>
  243.  * <dash_stamina:Add -10>
  244.  * <dash_stamina:Refill>
  245.  * <dash_stamina:Deplete>
  246.  * <dash_stamina:IncreaseMax 1500>
  247.  * Note that those only work from the menu, not from battle
  248.  *
  249.  *--------------------------------------
  250.  * Version History:
  251.  *--------------------------------------
  252.  * v1.10 (5 June 2016)
  253.  * - Added a new plugin command to enforce to hide the stamina-bar (for cut-scenes and such): Stamina ForceHide true/false.
  254.  * - Fixed a bug that wouldn't correctly render the stamina window when setting "Autohide" to false. Consuming stamina at least once also used to fix this.
  255.  *
  256.  * v1.09 (11 April 2016)
  257.  * - Refactoring & minor performance optimizations.
  258.  *
  259.  * v1.08 (3 April 2016) [Parameters Changed]
  260.  * - Added an optional SFX to the tile-mode.
  261.  * - Added two new parameters: "Default SFX Volume" and "Stamina Decrease Tiled SFX".
  262.  *
  263.  * v1.07a (1 April 2016) [Parameters Changed]
  264.  * - Added a new parameter to run common events at certain stamina values/percentages.
  265.  * - 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).
  266.  * - Added more comments to the code.
  267.  *
  268.  * v1.06 (26 March 2016)
  269.  * - Added new plugin commands: SetVar, SetEval, SetMaxEval & SetMaxVar.
  270.  * - The plugin command "Stamina EnableDashing" now accepts a third value: toggle.
  271.  *
  272.  * v1.05 (21 March 2016)
  273.  * - 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.
  274.  * - Added new functions and a new alias to accomodate the above new feature.
  275.  *
  276.  * v1.04 (03 February 2016)
  277.  * - 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).
  278.  *
  279.  * v1.03 (01 January 2016)
  280.  * - Used my new coding standards & refactored.
  281.  * - Switched to the Imported variable.
  282.  * - Fixed an accidental duplicate alias.
  283.  * - Fixed a bug in Scene_Map.prototype.updateMain() (which just so happened to cause no side-effects).
  284.  *
  285.  * v1.02 (12 December 2015)
  286.  * - Dashing for the current map can now be entirely disabled with a map-notetag and switched with a plugin command at any time.
  287.  * - Created a simple plugin addon to store the player-stamina in a global game-variable.
  288.  * - Enabled custom text to be drawn in front of the Stamina Window.
  289.  * - New feature: items can replenish, lower, refill and deplete stamina (only in the menu, not in battle).
  290.  *
  291.  * v1.01 (1 December 2015)
  292.  * - Removed strict-mode because... Possible bug in RPG Maker...
  293.  * - Refactored and fixed semicolons and missing var-keywords.
  294.  *
  295.  * v1.00 (26 November 2015)
  296.  * - First release.
  297.  * Alpha (November 2015)
  298.  * - First alpha release.
  299.  *
  300.  */
  301. // Imported
  302. var Imported = Imported || {};
  303. Imported.Silv_DashStamina = 1.10;
  304.  
  305. // #Parameters
  306. var Silv = Silv || {};
  307. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilverStamina>'); })[0].parameters;
  308. Silv.DashStamina = Silv.DashStamina || {};
  309. // Non-parameters
  310. Silv.DashStamina.Window = null;
  311. Silv.DashStamina.ScreenIsFading = false;
  312. Silv.DashStamina.DashingDisabled = false;
  313. Silv.DashStamina.CommonEvents = [];
  314. Silv.DashStamina.HasCommonEvents = false;
  315. // General
  316. Silv.DashStamina.ShowWindow = Silv.Parameters['Show Stamina Window'].toLowerCase() === 'true';
  317. Silv.DashStamina.DefaultVolume = parseInt(Silv.Parameters['Default SFX Volume']);
  318. Silv.DashStamina.TiledStaminaDecreaseSFX = Silv.Parameters['Stamina Decrease Tiled SFX'];
  319. // Positioning & Size
  320. Silv.DashStamina.Window_X = parseInt(Silv.Parameters['Window X']);
  321. Silv.DashStamina.Window_Y = parseInt(Silv.Parameters['Window Y']);
  322. Silv.DashStamina.WindowWidth = parseInt(Silv.Parameters['Window Width']);
  323. Silv.DashStamina.WindowHeight = parseInt(Silv.Parameters['Window Height']);
  324. Silv.DashStamina.WindowHorizontalAlignment = (Silv.Parameters['Window Horizontal Alignment']).toLowerCase();
  325. Silv.DashStamina.WindowVerticalAlignment = (Silv.Parameters['Window Vertical Alignment']).toLowerCase();
  326. 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])};
  327. // Stamina Pool
  328. Silv.DashStamina.DecreaseMode = Silv.Parameters['Stamina Decrease Mode'].toLowerCase();
  329. Silv.DashStamina.StaminaDecrease = parseInt(Silv.Parameters['Stamina Decrease']);
  330. Silv.DashStamina.StaminaMax = parseInt(Silv.Parameters['Stamina Max']);
  331. Silv.DashStamina.StaminaRecoveryDelay = parseInt(Silv.Parameters['Stamina Recovery Delay']);
  332. Silv.DashStamina.StaminaRecoveryRate = parseInt(Silv.Parameters['Stamina Recovery Rate']);
  333. Silv.DashStamina.StaminaAutoDashThreshold = parseInt(Silv.Parameters['Stamina AutoDash Threshold']);
  334. Silv.DashStamina.UseCustomRegenFormula = Silv.Parameters['Use Custom Stamina Regen Formula?'].toLowerCase() === 'true';
  335. Silv.DashStamina.RegenFormula = Silv.Parameters['Stamina Regen Formula'];
  336. // Visuals
  337. Silv.DashStamina.StaminaGaugeColor1 = (Silv.Parameters['Stamina Gauge Color 1']).toUpperCase();
  338. Silv.DashStamina.StaminaGaugeColor2 = (Silv.Parameters['Stamina Gauge Color 2']).toUpperCase();
  339. Silv.DashStamina.DrawStaminaValue = Silv.Parameters['Draw Stamina Value'].toLowerCase();
  340. Silv.DashStamina.FontSize = parseInt(Silv.Parameters['Font Size']);
  341. // Window Prefix Text
  342. Silv.DashStamina.WindowText = Silv.Parameters['Window Text'];
  343. Silv.DashStamina.WindowTextOffsetY = parseInt(Silv.Parameters['Window Text Offset Y']);
  344. Silv.DashStamina.WindowTextGaugeSpacingX = parseInt(Silv.Parameters['Window Text Spacing X']);
  345. // Window
  346. Silv.DashStamina.AutoHideStaminaWindow = Silv.Parameters['Auto Hide Stamina Window'].toLowerCase() === 'true';
  347. Silv.DashStamina.HideStaminaWindowDelay = parseInt(Silv.Parameters['Hide Stamina Window Delay']);
  348. Silv.DashStamina.WindowOpacity = parseInt(Silv.Parameters['Stamina Window Opacity']);
  349. Silv.DashStamina.WindowSlideOutDir = Silv.Parameters['Window Slideout Direction'].toLowerCase();
  350. Silv.DashStamina.WindowSlideOutSpeed = parseFloat(Silv.Parameters['Window Slideout Speed']);
  351. // Advanced
  352. Silv.DashStamina.CE_Delta = parseInt(Silv.Parameters['Common Event Stamina Value Activation Delta']);
  353. Silv.DashStamina.DisableGameSwitch = parseInt(Silv.Parameters['Disable Stamina Consumption GameSwitch']);
  354. Silv.DashStamina.Window_Z = parseInt(Silv.Parameters['Window Z-Index']);
  355. Silv.DashStamina.PluginCmdId = Silv.Parameters['Plugin Command Identifier'];
  356.  
  357. // Non-parameters
  358. Silv.DashStamina.ForcedHidden = false;
  359.  
  360. // Alias
  361. Silv.Alias = Silv.Alias || {};
  362. if (!Silv.AddAlias)
  363. {
  364.     Silv.AddAlias = function(alias, original_method)
  365.     {
  366.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  367.         Silv.Alias[alias] = original_method;
  368.     };
  369. }
  370.  
  371.  
  372. // Convert&store data for running common events at certain stamina percentages
  373. (function()
  374. {
  375.     var splitted = Silv.Parameters['Common Events At Stamina Values'].split(' ');
  376.     for (var ceIdx=0; ceIdx<splitted.length; ceIdx+=2)
  377.     {
  378.         var threshold = splitted[ceIdx];
  379.         var thresholdIsPerc = false;
  380.         if (~threshold.indexOf('%'))
  381.         {
  382.             thresholdIsPerc = true;
  383.             threshold = parseInt(threshold.slice(0, -1)); // Remove % character
  384.         }
  385.         else
  386.         {
  387.             threshold = parseInt(threshold);
  388.         }
  389.         var commonEventID = parseInt(splitted[ceIdx + 1]);
  390.        
  391.         Silv.DashStamina.CommonEvents[ceIdx / 2] = { threshold:threshold, thresholdIsPerc:thresholdIsPerc, commonEventID:commonEventID, lastRunStaminaAmount:null };
  392.     }
  393.     Silv.DashStamina.HasCommonEvents = (Silv.DashStamina.CommonEvents.length > 0);
  394. })();
  395.  
  396. // Sanity checks
  397. (function()
  398. {
  399.     if ((Silv.DashStamina.DecreaseMode !== 'default') && (Silv.DashStamina.DecreaseMode !== 'tile')) { throw new Error('Invalid parameter-value for "Decrease Mode": ' + Silv.DashStamina.DecreaseMode + '.'); }
  400. })();
  401. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  402. (function()
  403. {
  404. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  405. // Utilities
  406. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  407.  
  408. // Usage: alert( hexToRgb("#0033ff").g ); // "51";
  409. function hexToRgb(hex)
  410. {
  411.     var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  412.     return result ? {
  413.         r: parseInt(result[1], 16),
  414.         g: parseInt(result[2], 16),
  415.         b: parseInt(result[3], 16)
  416.     } : null;
  417. }
  418.  
  419. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  420. // Game Player
  421. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  422. //------------------------------------------------------------------------------------------------------------------------------------
  423. // Can the player possibly consume stamina?
  424. //------------------------------------------------------------------------------------------------------------------------------------
  425. Game_Player.prototype.hasStaminaConsumption = function()
  426. {
  427.     if ($gameSwitches.value(Silv.DashStamina.DisableGameSwitch)) { return false; }
  428.     if (!this.mapConsumeStamina) { return false; }
  429.     return true;
  430. };
  431.  
  432. //------------------------------------------------------------------------------------------------------------------------------------
  433. // Alias for whenever the player entered a new tile
  434. //------------------------------------------------------------------------------------------------------------------------------------
  435. Silv.AddAlias('dashStamina_Game_Player_updateMove', Game_Player.prototype.updateMove);
  436. Game_Player.prototype.updateMove = function()
  437. {
  438.     Silv.Alias.dashStamina_Game_Player_updateMove.apply(this, arguments);
  439.     if ((Silv.DashStamina.DecreaseMode === 'tile') && !this.isMoving() && this.isDashing() && this.hasStaminaConsumption())
  440.     {
  441.         // Consume stamina for the tile-decrease-mode
  442.         this.dashStamina -= Silv.DashStamina.StaminaDecrease;
  443.         if (Silv.DashStamina.TiledStaminaDecreaseSFX.toLowerCase() !== 'none') { Play_SE(Silv.DashStamina.TiledStaminaDecreaseSFX); }
  444.         this.postDecreaseStaminaHandling();
  445.     }
  446. };
  447.  
  448. //------------------------------------------------------------------------------------------------------------------------------------
  449. // Is&can the player consume stamina?
  450. //------------------------------------------------------------------------------------------------------------------------------------
  451. Game_Player.prototype.isConsumingStamina = function()
  452. {
  453.     return (this.isDashing() && this.isMoving() && this.hasStaminaConsumption());
  454. };
  455.  
  456. //------------------------------------------------------------------------------------------------------------------------------------
  457. // Call this method after decreasing the stamina
  458. //------------------------------------------------------------------------------------------------------------------------------------
  459. Game_Player.prototype.postDecreaseStaminaHandling = function()
  460. {
  461.     if (this.dashStamina < 0) { this.dashStamina = 0; }
  462.     this.isRecoveringStamina = false;
  463.     if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  464. };
  465.  
  466. //------------------------------------------------------------------------------------------------------------------------------------
  467. // Check if #Common Events (CE) need running and do so if applicable
  468. //------------------------------------------------------------------------------------------------------------------------------------
  469. Game_Player.prototype.staminaEventChecks = function()
  470. {
  471.     if (Silv.DashStamina.HasCommonEvents)
  472.     {
  473.         var staminaPercRounded = Math.round(this.dashStaminaPerc * 100);
  474.         for (var ceIdx=0; ceIdx<Silv.DashStamina.CommonEvents.length; ceIdx++)
  475.         {
  476.             if (Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount === null)
  477.             {
  478.                 if (Silv.DashStamina.CommonEvents[ceIdx].thresholdIsPerc)
  479.                 {
  480.                     if (staminaPercRounded === Silv.DashStamina.CommonEvents[ceIdx].threshold) { this.staminaRunCE(ceIdx); }
  481.                 }
  482.                 else
  483.                 {
  484.                     if (this.dashStamina === Silv.DashStamina.CommonEvents[ceIdx].threshold) { this.staminaRunCE(ceIdx); }
  485.                 }
  486.             }
  487.             else
  488.             {
  489.                 if (Math.abs(Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount - this.dashStamina) >= (Silv.DashStamina.CE_Delta - 1))
  490.                 {
  491.                     Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount = null; // Allow this CE to be run again next time
  492.                 }
  493.             }
  494.         }
  495.     }
  496. };
  497.  
  498. Game_Player.prototype.staminaRunCE = function(ceIdx)
  499. {
  500.     $gameTemp.reserveCommonEvent(Silv.DashStamina.CommonEvents[ceIdx].commonEventID);
  501.     Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount = this.dashStamina;
  502. };
  503.  
  504. //------------------------------------------------------------------------------------------------------------------------------------
  505. // #Update Stamina
  506. //------------------------------------------------------------------------------------------------------------------------------------
  507. Game_Player.prototype.updateStamina = function()
  508. {
  509.     this.oldDashStamina = this.dashStamina;
  510.    
  511.     var isConsumingStamina = this.isConsumingStamina();
  512.     if (isConsumingStamina)
  513.     {
  514.         // Only consume stamina here if the decrease-mode is set to default
  515.         if (Silv.DashStamina.DecreaseMode === 'default') { this.dashStamina -= Silv.DashStamina.StaminaDecrease; }
  516.         this.postDecreaseStaminaHandling();
  517.     }
  518.     else // not currently consuming stamina
  519.     {      
  520.         if (this.isRecoveringStamina)
  521.         {
  522.             this.staminaRecoveryTimeCnt++;
  523.             if (this.dashStamina < this.dashStaminaMax) // Recover the stamina
  524.             {
  525.                 // Recover stamina
  526.                 this.dashStamina += this.calculateStaminaRegen(Silv.DashStamina.StaminaRecoveryRate);
  527.                 Silv.DashStamina.clampMaxStamina();
  528.                
  529.                 if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  530.             }
  531.             else // Already at max stamina, find out when to hide the window if applicable and do so if possible
  532.             {
  533.                 // If the stamina window is used, attempt to autohide it if applicable
  534.                 if (Silv.DashStamina.ShowWindow && Silv.DashStamina.AutoHideStaminaWindow)
  535.                 {
  536.                     this.hideStaminaWindowDelayCnt += 1;
  537.                     if (this.hideStaminaWindowDelayCnt >= Silv.DashStamina.HideStaminaWindowDelay)
  538.                     {
  539.                         Silv.DashStamina.Window.hideMe();
  540.                         this.hideStaminaWindowDelayCnt = 0;
  541.                     }
  542.                 }
  543.                    
  544.             }
  545.         }
  546.         else // not currently recovering stamina, so find out when to start recovering it and do so if required
  547.         {
  548.             this.staminaRecoveryTimeCnt = 0;
  549.             this.staminaRecoveryDelayCnt += 1;
  550.             if (this.staminaRecoveryDelayCnt >= Silv.DashStamina.StaminaRecoveryDelay)
  551.             {
  552.                 this.staminaRecoveryDelayCnt = 0;
  553.                 this.isRecoveringStamina = true;
  554.             }
  555.         }
  556.     }
  557.  
  558.     // Stamina Percentage
  559.     this.dashStaminaPerc = this.dashStamina / parseFloat(this.dashStaminaMax);
  560.  
  561.     // Threshold
  562.     if (!isConsumingStamina)
  563.     {
  564.         this.requiresThresholdAmount = this.dashStaminaPerc * 100 < Silv.DashStamina.StaminaAutoDashThreshold;
  565.     }
  566.    
  567.     if (this.oldDashStamina !== this.dashStamina) { this.onAfterStaminaChanged() }
  568.    
  569.     this.staminaEventChecks();
  570. };
  571.  
  572. Game_Player.prototype.onAfterStaminaChanged = function()
  573. {
  574.     if (Silv.DashStamina.Window) { Silv.DashStamina.Window.onAfterStaminaChanged(); }
  575. };
  576.  
  577. //------------------------------------------------------------------------------------------------------------------------------------
  578. // Regeneration Formula
  579. //------------------------------------------------------------------------------------------------------------------------------------
  580. Game_Player.prototype.calculateStaminaRegen = function()
  581. {
  582.     if (!Silv.DashStamina.UseCustomRegenFormula) { return this.calculateStaminaRegenEval(); }
  583.     else { return this.calculateStaminaRegenNoEval();}
  584. };
  585.    
  586. Game_Player.prototype.calculateStaminaRegenEval = function()
  587. {
  588.     var base = Silv.DashStamina.StaminaRecoveryRate;
  589.     var x = this.staminaRecoveryTimeCnt;
  590.     return eval(Silv.DashStamina.RegenFormula);
  591. };
  592.  
  593. Game_Player.prototype.calculateStaminaRegenNoEval = function()
  594. {
  595.     return Silv.DashStamina.StaminaRecoveryRate + Math.sqrt(this.staminaRecoveryTimeCnt / 50);
  596. };
  597.  
  598. //------------------------------------------------------------------------------------------------------------------------------------
  599. // #Initialize
  600. //------------------------------------------------------------------------------------------------------------------------------------
  601. Silv.AddAlias('dashStamina_Game_Player_initialize', Game_Player.prototype.initialize);
  602. Game_Player.prototype.initialize = function()
  603. {
  604.     Silv.Alias.dashStamina_Game_Player_initialize.apply(this, arguments);
  605.  
  606.     this.dashStamina = this.dashStaminaMax = Silv.DashStamina.StaminaMax;
  607.     this.staminaRecoveryDelayCnt = 0; // counter for when to start recovering stamina
  608.     this.staminaRecoveryTimeCnt = 0; // counter for how long the player has been recovering stamina (in frames)
  609.     this.isRecoveringStamina = false;
  610.     this.dashStaminaPerc = 1.0;
  611.     this.hideStaminaWindowDelayCnt = 0;
  612.     this.requiresThresholdAmount = false;
  613.     this.wasDashing = false;
  614. };
  615.  
  616. //------------------------------------------------------------------------------------------------------------------------------------
  617. // Is the player currently allowed to dash?
  618. //------------------------------------------------------------------------------------------------------------------------------------
  619. Game_Player.prototype.dashingAllowed = function()
  620. {
  621.     if (this.dashStamina === 0 ||
  622.        (!this.wasDashing && this.requiresThresholdAmount)) // Do not allow to dash if the player was not dashing the previous frame AND if the threshold was passed.
  623.     {
  624.         return false;
  625.     }
  626.    
  627.     return true;
  628. };
  629.  
  630. //------------------------------------------------------------------------------------------------------------------------------------
  631. // Alias for disallowing dashing
  632. //------------------------------------------------------------------------------------------------------------------------------------
  633. Silv.AddAlias('dashStamina_Game_Player_updateDashing', Game_Player.prototype.updateDashing);
  634. Game_Player.prototype.updateDashing = function()
  635. {
  636.     Silv.Alias.dashStamina_Game_Player_updateDashing.apply(this, arguments);
  637.    
  638.     if (Silv.DashStamina.DashingDisabled)
  639.     {
  640.         this._dashing = false;
  641.         return;
  642.     }
  643.    
  644.     if (!this.dashingAllowed()) { this._dashing = false; }
  645.     this.wasDashing = this._dashing;
  646. };
  647.  
  648. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  649. // Game Screen
  650. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  651. // Do not just show the minimap on top of a faded-out screen.
  652. Silv.AddAlias('dashStamina_Game_Screen_updateFadeOut', Game_Screen.prototype.updateFadeOut);
  653. Game_Screen.prototype.updateFadeOut = function()
  654. {
  655.     Silv.Alias.dashStamina_Game_Screen_updateFadeOut.apply(this, arguments);
  656.    
  657.     if (this._brightness < 255) // (this._fadeOutDuration > 0)
  658.     {
  659.         if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  660.         Silv.DashStamina.ScreenIsFading = true;
  661.     }
  662.     else
  663.     {
  664.         Silv.DashStamina.ScreenIsFading = false;
  665.     }
  666. };
  667.  
  668. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  669. // Scene Base
  670. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  671. Silv.AddAlias('dashStamina_Scene_Base_startFadeIn', Scene_Base.prototype.startFadeIn);
  672. Scene_Base.prototype.startFadeIn = function()
  673. {
  674.     Silv.Alias.dashStamina_Scene_Base_startFadeIn.apply(this, arguments);
  675.     if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  676. };
  677.  
  678. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  679. // Scene Map
  680. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  681. // Hook into the main loop
  682. Silv.AddAlias('dashStamina_Scene_Map_updateMain', Scene_Map.prototype.updateMain);
  683. Scene_Map.prototype.updateMain = function()
  684. {
  685.     Silv.Alias.dashStamina_Scene_Map_updateMain.apply(this, arguments);
  686.     $gamePlayer.updateStamina();
  687.     if (Silv.DashStamina.ShowWindow) { Silv.DashStamina.Window.update(); }
  688. };
  689.  
  690. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  691. // Game Map
  692. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  693. // Disable Dashing?
  694. Silv.AddAlias('dashStamina_Game_Map_setup', Game_Map.prototype.setup);
  695. Game_Map.prototype.setup = function(mapId)
  696. {
  697.     Silv.Alias.dashStamina_Game_Map_setup.apply(this, arguments);
  698.     Silv.DashStamina.DashingDisabled = ('disable_dashing' in $dataMap.meta);
  699. };
  700.  
  701. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  702. // Stamina Regen/Deplete #Items
  703. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  704. Silv.AddAlias('dashStamina_Scene_ItemBase_useItem', Scene_ItemBase.prototype.useItem);
  705. Scene_ItemBase.prototype.useItem = function()
  706. {
  707.     var item = this.item();
  708.     if ('dash_stamina' in item.meta)
  709.     {
  710.         var args = item.meta.dash_stamina.split(' ');
  711.         switch(args[0].toLowerCase())
  712.         {
  713.             case 'add':
  714.                 if (args.length < 2) { throw 'Item-add-command is missing the value-argument.'; }
  715.                 $gamePlayer.dashStamina += parseInt(args[1]);
  716.                 if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  717.                 else if ($gamePlayer.dashStamina < 0) { $gamePlayer.dashStamina = 0; }
  718.                 break;
  719.             case 'refill':
  720.                 $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  721.                 break;
  722.             case 'deplete':
  723.                 $gamePlayer.dashStamina = 0;
  724.                 break;
  725.             case 'increasemax':
  726.                 if (args.length < 2) { throw 'Item-increasemax-command is missing the value-argument.'; }
  727.                 $gamePlayer.dashStaminaMax += parseInt(args[1]);
  728.                 if ($gamePlayer.dashStaminaMax < 1 ) { $gamePlayer.dashStaminaMax = 1; }
  729.                 break;
  730.             default:
  731.                 throw 'Unknown dash_stamina itemnotetag: ' + item.meta;
  732.         }
  733.        
  734.     }
  735.    
  736.     Silv.Alias.dashStamina_Scene_ItemBase_useItem.apply(this, arguments);
  737. };
  738.  
  739. Silv.AddAlias('dashStamina_Game_Action_testApply', Game_Action.prototype.testApply);
  740. Game_Action.prototype.testApply = function(target)
  741. {
  742.     if ('dash_stamina' in this.item().meta)
  743.     {
  744.         return true;
  745.     }
  746.     else
  747.     {
  748.         return Silv.Alias.dashStamina_Game_Action_testApply.apply(this, arguments);
  749.     }
  750. };
  751. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  752. // Stamina #Window
  753. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  754. function Window_DashStamina() { this.initialize.apply(this, arguments); }
  755. Window_DashStamina.prototype = Object.create(Window_Base.prototype);
  756. Window_DashStamina.prototype.constructor = Window_DashStamina;
  757. // Font Size
  758. Window_DashStamina.prototype.standardFontSize = function() { return Silv.DashStamina.FontSize; };
  759.  
  760. // #Initialize
  761. Window_DashStamina.prototype.initialize = function(x, y, width, height)
  762. {
  763.     Window_Base.prototype.initialize.call(this, x, y, width, height);
  764.     this._helpWindow = null;
  765.     this._handlers = {};
  766.     this._touching = false;
  767.     this.deactivate();
  768.    
  769.     this.opacity = Silv.DashStamina.WindowOpacity;
  770.     this.slideDirection = {x: 0, y: 0 };
  771.     this.originalWinLoc = {x: 0, y: 0 };
  772.     this.sliding = 'none';
  773.     this.isFullySlidedOut = false;
  774.     this.isFullySlidedIn = true;
  775.     this.originalWinLoc.x = x; // for some reason "this." is not allowed here
  776.     this.originalWinLoc.y = y; // for some reason "this." is not allowed here
  777.     this.gaugeText = '';
  778.    
  779.     if (Silv.DashStamina.WindowText != ':none')
  780.     {
  781.         this.windowText = Silv.DashStamina.WindowText;
  782.         this.windowTextWidth = this.contents.measureTextWidth(this.windowText);
  783.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  784.     }
  785.     else
  786.     {
  787.         this.windowText = null;
  788.         this.windowTextWidth = 0;
  789.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  790.     }
  791.    
  792.     this.calculateGaugeFillColour();
  793.     this.update();
  794.     this.onAfterStaminaChanged();
  795. };
  796.  
  797. //------------------------------------------------------------------------------------------------------------------------------------
  798. // Stamina Window Update
  799. //------------------------------------------------------------------------------------------------------------------------------------
  800. Window_DashStamina.prototype.update = function()
  801. {
  802.     if (Silv.DashStamina.ScreenIsFading)
  803.     {
  804.         this.visible = false;
  805.     }
  806.     else
  807.     {
  808.         Window_Base.prototype.update.call(this);
  809.         this.drawStaminaWindow(0, 0);
  810.         this.updateSliding();
  811.     }
  812. };
  813.  
  814. //------------------------------------------------------------------------------------------------------------------------------------
  815. // Stamina Window Drawing #Optimizations (are not called every frame, unless the stamina is changing every frame of course)
  816. //------------------------------------------------------------------------------------------------------------------------------------
  817. Window_DashStamina.prototype.setGaugeFillWidth = function(rate)
  818. {
  819.     this.gaugeFillWidth = Math.floor(Silv.DashStamina.StaminaGaugeRectangle.width * rate);
  820. };
  821.  
  822. // Set the desired gauge-text variable
  823. Window_DashStamina.prototype.setGaugeText = function()
  824. {
  825.     switch(Silv.DashStamina.DrawStaminaValue) // allowed values: absolute/percentage/both/none
  826.     {
  827.         case 'absolute':
  828.             this.gaugeText = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax);
  829.             break;
  830.         case 'percentage':
  831.             this.gaugeText = Math.round($gamePlayer.dashStaminaPerc * 100) + '%';
  832.             break;
  833.         case 'both':
  834.             this.gaugeText = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax) + ' (' + Math.round($gamePlayer.dashStaminaPerc * 100) + '%)';
  835.             break;
  836.         case 'none':
  837.             this.gaugeText = '';
  838.             return;
  839.         default:
  840.             throw 'ERROR: drawStaminaWindow missing case-statement or incorrect Silv.DashStamina.DrawStaminaValue value. Value: ' + Silv.DashStamina.DrawStaminaValue;
  841.     }
  842. };
  843.  
  844. Window_DashStamina.prototype.onAfterStaminaChanged = function()
  845. {
  846.     this.calculateGaugeFillColour();
  847.     this.setGaugeFillWidth($gamePlayer.dashStaminaPerc);
  848.     this.setGaugeText();
  849. };
  850.  
  851. //------------------------------------------------------------------------------------------------------------------------------------
  852. // Stamina Window #Drawing
  853. //------------------------------------------------------------------------------------------------------------------------------------
  854. Window_DashStamina.prototype.drawStaminaGauge = function(x, y)
  855. {
  856.     var gaugeY = y + this.lineHeight() - 8;
  857.     this.contents.fillRect(x, gaugeY, Silv.DashStamina.StaminaGaugeRectangle.width, Silv.DashStamina.StaminaGaugeRectangle.height, this.gaugeBackColor());
  858.     this.contents.fillRect(x, gaugeY, this.gaugeFillWidth, Silv.DashStamina.StaminaGaugeRectangle.height, this.gaugeFillColour);
  859. };
  860.  
  861. Window_DashStamina.prototype.drawStaminaWindow = function(x, y)
  862. {
  863.     this.contents.clear();
  864.    
  865.     // Draw the prefix text (if applicable)
  866.     if (this.windowText !== null)
  867.     {
  868.         this.contents.drawText(this.windowText, 0, Silv.DashStamina.WindowTextOffsetY, this.windowTextWidth, 0);
  869.     }
  870.  
  871.     // Draw gauge
  872.     this.drawStaminaGauge(Silv.DashStamina.StaminaGaugeRectangle.x + this.windowTextWidth + this.windowTextGaugeSpacingX, Silv.DashStamina.StaminaGaugeRectangle.y);
  873.    
  874.     this.resetTextColor();
  875.     this.drawText(this.gaugeText, x + this.windowTextWidth + this.windowTextGaugeSpacingX, y + 1, Silv.DashStamina.StaminaGaugeRectangle.width, 'center');
  876. };
  877.  
  878. // Calculate what colour the gauge should be between the two colours depending on the percentage value of the current-stamina value.
  879. Window_DashStamina.prototype.calculateGaugeFillColour = function()
  880. {
  881.     var c1 = hexToRgb(Silv.DashStamina.StaminaGaugeColor1);
  882.     var c2 = hexToRgb(Silv.DashStamina.StaminaGaugeColor2);
  883.        
  884.     var ratio = $gamePlayer.dashStaminaPerc;
  885.     var hex = function(x) {
  886.         x = x.toString(16);
  887.         return (x.length === 1) ? '0' + x : x;
  888.     };
  889.  
  890.     var r = Math.ceil(c1.r * ratio + c2.r * (1-ratio));
  891.     var g = Math.ceil(c1.g * ratio + c2.g * (1-ratio));
  892.     var b = Math.ceil(c1.b * ratio + c2.b * (1-ratio));
  893.  
  894.     var middle = '#' + hex(r) + hex(g) + hex(b);
  895.     this.gaugeFillColour = middle;
  896. };
  897.  
  898. //------------------------------------------------------------------------------------------------------------------------------------
  899. // Stamina Window #Show & #Hide
  900. //------------------------------------------------------------------------------------------------------------------------------------
  901. Window_DashStamina.prototype.showMe = function()
  902. {
  903.     //if (this.visible) { return; };
  904.     if (this.isFullySlidedIn && (!Silv.DashStamina.ForcedHidden))
  905.     {
  906.         this.visible = true;
  907.         return;
  908.     }
  909.    
  910.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide' || this.visible === false) { this.visible = true; }
  911.     else
  912.     {
  913.         this.sliding = 'in';
  914.         switch (Silv.DashStamina.WindowSlideOutDir)
  915.         {
  916.             case 'top':
  917.                 this.slideDirection.x = 0;
  918.                 this.slideDirection.y = 1;
  919.                 break;
  920.             case 'left':
  921.                 this.slideDirection.x = 1;
  922.                 this.slideDirection.y = 0;
  923.                 break;
  924.             case 'right':
  925.                 this.slideDirection.x = -1;
  926.                 this.slideDirection.y = 0;
  927.                 break;
  928.             case 'bottom':
  929.                 this.slideDirection.x = 0;
  930.                 this.slideDirection.y = -1;
  931.                 break;
  932.             default:
  933.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  934.         }
  935.     }
  936.    
  937.     if (Silv.DashStamina.ForcedHidden) { this.visible = false; }
  938. };
  939.  
  940. Window_DashStamina.prototype.hideMe = function()
  941. {
  942.     if (!this.visible || this.isFullySlidedOut) { return; }
  943.    
  944.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide') { this.visible = false; }
  945.     else
  946.     {
  947.         this.sliding = 'out';
  948.         switch (Silv.DashStamina.WindowSlideOutDir)
  949.         {
  950.             case 'top':
  951.                 this.slideDirection.x = 0;
  952.                 this.slideDirection.y = -1;
  953.                 break;
  954.             case 'left':
  955.                 this.slideDirection.x = -1;
  956.                 this.slideDirection.y = 0;
  957.                 break;
  958.             case 'right':
  959.                 this.slideDirection.x = 1;
  960.                 this.slideDirection.y = 0;
  961.                 break;
  962.             case 'bottom':
  963.                 this.slideDirection.x = 0;
  964.                 this.slideDirection.y = 1;
  965.                 break;
  966.             default:
  967.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  968.         }
  969.     }
  970.    
  971.     if (Silv.DashStamina.ForcedHidden) { this.visible = false; }
  972. };
  973.  
  974. //------------------------------------------------------------------------------------------------------------------------------------
  975. // Stamina Window #Sliding of the Window
  976. //------------------------------------------------------------------------------------------------------------------------------------
  977. Window_DashStamina.prototype.handleSlidingEnd = function()
  978. {
  979.     if (this.sliding === 'in')
  980.     {
  981.         // Stop sliding in
  982.         if (this.slideDirection.x  === 1 && this.x > this.originalWinLoc.x ||
  983.             this.slideDirection.x  === -1 && this.x < this.originalWinLoc.x ||
  984.             this.slideDirection.y  === 1 && this.y > this.originalWinLoc.y ||
  985.             this.slideDirection.y  === -1 && this.y < this.originalWinLoc.y)
  986.         {
  987.             this.sliding = 'none';
  988.             this.isFullySlidedIn = true;
  989.         }
  990.     }
  991.     else
  992.     {
  993.         // Stop sliding out
  994.         if (this.x < -this.width || this.x > Graphics._width + this.width ||
  995.             this.y < -this.height || this.x > Graphics._height + this.height)
  996.         {
  997.             this.sliding = 'none';
  998.             this.isFullySlidedOut = true;
  999.         }
  1000.     }
  1001. };
  1002.  
  1003. Window_DashStamina.prototype.updateSliding = function()
  1004. {
  1005.     if (this.sliding === 'none') { return; }
  1006.     this.x += this.slideDirection.x * Silv.DashStamina.WindowSlideOutSpeed;
  1007.     this.y += this.slideDirection.y * Silv.DashStamina.WindowSlideOutSpeed;
  1008.    
  1009.     this.isFullySlidedOut = false;
  1010.     this.isFullySlidedIn = false;
  1011.    
  1012.     this.handleSlidingEnd();
  1013. };
  1014.  
  1015. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1016. // #Create Stamina Window
  1017. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1018. Scene_Map.prototype.createDashWindow = function()
  1019. {
  1020.     // Dispose the old window, if any
  1021.     if (Silv.DashStamina.Window !== null) { this.removeWindow(Silv.DashStamina.Window); }
  1022.    
  1023.     // Does the map consume stamina?
  1024.     $gamePlayer.mapConsumeStamina = !('dstam_disable' in $dataMap.meta);
  1025.     if(Silv.DashStamina.ShowWindow)
  1026.     {
  1027.         var x = 0;
  1028.         if (Silv.DashStamina.WindowHorizontalAlignment === 'right') { x = Graphics.width - Silv.DashStamina.WindowWidth; }
  1029.         var y = 0;
  1030.         if (Silv.DashStamina.WindowVerticalAlignment === 'bottom') { y = Graphics.height - Silv.DashStamina.WindowHeight; }
  1031.        
  1032.         Silv.DashStamina.Window = new Window_DashStamina(x + Silv.DashStamina.Window_X, y + Silv.DashStamina.Window_Y, Silv.DashStamina.WindowWidth, Silv.DashStamina.WindowHeight);
  1033.        
  1034.         this.addChild(Silv.DashStamina.Window, Silv.DashStamina.Window_Z);
  1035.         if (Silv.DashStamina.AutoHideStaminaWindow) { Silv.DashStamina.Window.visible = false; }
  1036.     }
  1037. };
  1038.  
  1039. // Omg why does RPG Maker not have this method by default...
  1040. Scene_Base.prototype.removeWindow = function(window)
  1041. {
  1042.     var index = this.children.indexOf(window);
  1043.     if (index > -1) { this.children.splice(index, 1); }
  1044. };
  1045.  
  1046. Silv.AddAlias('dashStamina_Scene_Map_createDisplayObjects', Scene_Map.prototype.createDisplayObjects);
  1047. Scene_Map.prototype.createDisplayObjects = function()
  1048. {
  1049.     Silv.Alias.dashStamina_Scene_Map_createDisplayObjects.apply(this, arguments);
  1050.     this.createDashWindow();
  1051. };
  1052. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1053. // Play SFX
  1054. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1055. function Play_SE(filename)
  1056. {
  1057.     var volume = (Silv.DashStamina.DefaultVolume > -1) ? Silv.DashStamina.DefaultVolume : AudioManager.seVolume;
  1058.     AudioManager.playSe({name: filename, volume: volume, pitch: 100, pan: 0});
  1059. }
  1060.  
  1061. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1062. // Saving & Loading
  1063. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1064. Silv.AddAlias('dashStamina_DataManager_makeSaveContents', DataManager.makeSaveContents);
  1065. DataManager.makeSaveContents = function()
  1066. {
  1067.     contents = Silv.Alias.dashStamina_DataManager_makeSaveContents.apply(this, arguments);
  1068.     contents.dashStamina = $gamePlayer.dashStamina;
  1069.     contents.dashStaminaMax = $gamePlayer.dashStaminaMax;
  1070.     return contents;
  1071. };
  1072.  
  1073. Silv.AddAlias('dashStamina_DataManager_extractSaveContents', DataManager.extractSaveContents);
  1074. DataManager.extractSaveContents = function(contents)
  1075. {
  1076.     Silv.Alias.dashStamina_DataManager_extractSaveContents.apply(this, arguments);
  1077.     $gamePlayer.dashStamina = contents.dashStamina;
  1078.     $gamePlayer.dashStaminaMax = contents.dashStaminaMax;
  1079. };
  1080.  
  1081. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1082. // Plugin Command
  1083. // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
  1084. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1085. Silv.AddAlias('dashStamina_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
  1086. Game_Interpreter.prototype.pluginCommand = function(command, args)
  1087. {
  1088.     Silv.Alias.dashStamina_Game_Interpreter_pluginCommand.apply(this, arguments);
  1089.     if (command.toLowerCase() === Silv.DashStamina.PluginCmdId) { Silv.DashStamina.PluginCommand(command, args); }
  1090. };
  1091.  
  1092. // Concatenate the 'arguments' starting at a specific index because they are all part of an eval.
  1093. Silv.DashStamina.concatArgs = function(args, startIdx)
  1094. {
  1095.     var evalstr = '';
  1096.     for (var argIdx = startIdx; argIdx < args.length; argIdx++)
  1097.     {
  1098.         evalstr += args[argIdx] + ' ';
  1099.     }
  1100.     return evalstr;
  1101. };
  1102.  
  1103. // newValue must be between 0 - 100.
  1104. Silv.DashStamina.assignNewStaminaValue = function(newValue)
  1105. {
  1106.     var perc = Math.max(0, Math.min(100, newValue)); // clamp value between 0-100
  1107.     $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax * (perc / 100.0);
  1108. };
  1109.  
  1110. Silv.DashStamina.clampMaxStamina = function()
  1111. {
  1112.     if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  1113. };
  1114.  
  1115. Silv.DashStamina.PluginCommand = function(cmd, args)
  1116. {
  1117.     switch(args[0].toLowerCase())
  1118.     {
  1119.         case 'refill':
  1120.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  1121.             break;
  1122.         case 'deplete':
  1123.             $gamePlayer.dashStamina = 0;
  1124.             break;
  1125.         case 'set':
  1126.             Silv.DashStamina.assignNewStaminaValue(parseInt(args[1]));
  1127.         break;
  1128.         case 'setvar':
  1129.             Silv.DashStamina.assignNewStaminaValue(parseInt($gameVariables.value(args[1])));
  1130.         break;
  1131.         case 'seteval':
  1132.             var perc = parseInt(eval(Silv.DashStamina.concatArgs(args, 1)));
  1133.             if (isNaN(perc)) { throw new Error('Plugin command: "Stamina SetEval" evaled to a NaN value: ' + perc + '.'); }
  1134.             Silv.DashStamina.assignNewStaminaValue(perc);
  1135.         break;
  1136.         case 'showwindow':
  1137.             if (Silv.DashStamina.Window !== null)
  1138.             {
  1139.                 $gamePlayer.hideStaminaWindowDelayCnt = 0;
  1140.                 Silv.DashStamina.Window.visible = (!Silv.DashStamina.ForcedHidden);
  1141.             }
  1142.             break;
  1143.         case 'refillhide':
  1144.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  1145.             // NO break-statement here! We want to hide the window as well!
  1146.         case 'hidewindow':
  1147.             if (Silv.DashStamina.Window !== null)
  1148.             {
  1149.                 $gamePlayer.hideStaminaWindowDelayCnt = Silv.DashStamina.HideStaminaWindowDelay;
  1150.                 Silv.DashStamina.Window.visible = false;
  1151.             }
  1152.             break;
  1153.         case 'setmax':
  1154.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(args[1]));
  1155.             Silv.DashStamina.clampMaxStamina();
  1156.             break;
  1157.         case 'setmaxeval':
  1158.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(eval(Silv.DashStamina.concatArgs(args, 1))));
  1159.             Silv.DashStamina.clampMaxStamina();
  1160.             break;
  1161.         case 'setmaxvar':
  1162.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt($gameVariables.value(args[1])));
  1163.             Silv.DashStamina.clampMaxStamina();
  1164.             break;
  1165.         case 'increasemax':
  1166.             $gamePlayer.dashStaminaMax += parseInt(args[1]);
  1167.             if ($gamePlayer.dashStaminaMax < 1) { $gamePlayer.dashStaminaMax = 1; }
  1168.             if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  1169.             break;
  1170.         case 'enabledashing':
  1171.             switch(args[1].toLowerCase())
  1172.             {
  1173.                 case 'true':
  1174.                     Silv.DashStamina.DashingDisabled = false;
  1175.                     break;
  1176.                 case 'false':
  1177.                     Silv.DashStamina.DashingDisabled = true;
  1178.                     break;
  1179.                 case 'toggle':
  1180.                     Silv.DashStamina.DashingDisabled = !Silv.DashStamina.DashingDisabled;
  1181.                     break;
  1182.                 default:
  1183.                     throw new Error('"Stamina EnableDashing" received an unknown argument: ' + args[1] + '. Expected: True, False or Toggle.');
  1184.             }
  1185.             break;
  1186.         case 'forcehide':
  1187.             Silv.DashStamina.ForcedHidden = (args[1].toLowerCase() === 'true');
  1188.             if (Silv.DashStamina.Window !== null)
  1189.             {
  1190.                 Silv.DashStamina.Window.visible = !Silv.DashStamina.ForcedHidden;
  1191.             }
  1192.             break;
  1193.         default:
  1194.             throw 'Stamina PluginCommand invalid command: ' + args[0];
  1195.     }
  1196. };
  1197.  
  1198. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1199. })();
  1200. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1201. // This is the end of this awesome script!
  1202. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement