SHARE
TWEET

Untitled

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