SHARE
TWEET

Untitled

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