TenaarFeiri

RMMV - Enemy Health Bars

Apr 27th, 2016
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Show monster healthbars.
  3.  * @author Modified by Tenaar Feiri; Originally by Jeremy Cannady
  4.  *
  5.  * @param High HP
  6.  * @desc Insert the colour code.
  7.  * @default #009900
  8.  *
  9.  * @param Medium HP
  10.  * @desc Insert the colour code.
  11.  * @default #ffcc00
  12.  *
  13.  * @param Low HP
  14.  * @desc Insert the colour code.
  15.  * @default #ff6600
  16.  *
  17.  * @param Critical HP
  18.  * @desc Insert the colour code.
  19.  * @default #ff3300
  20.  *
  21.  * @help Put <hpBar> in the enemy note tag to activate the hp bar for that enemy during battle.
  22.  * Put <hpBarSelectionOnly> to actiavte the hp bar only during selection.
  23.  * Put<hpBarTurnASelection> to actiavet he bar during selection and attack.
  24.  * Put <hpBarTurnOnly> to only show the bar during the attack phase.
  25.  * Put <hpBarBoss> for boss encounters.
  26.  * PLEASE PUT ONLY ONE NOTETAG.
  27.  *
  28.  *
  29.  *
  30.  */
  31.  
  32. (function() {
  33.  
  34.     // Define our parameters so we are picked up on in the plugin manager.
  35.     var parameters = PluginManager.parameters('monster healthbar');
  36.    
  37.     //==========================================================================
  38.     // Create variables and define colour.
  39.     //==========================================================================
  40.    
  41.     var _Game_Enemy_prototype = Game_Enemy.prototype;
  42.     var _Scene_Battle_prototype = Scene_Battle.prototype;
  43.     var _Game_Enemy_prototype = Game_Enemy.prototype;
  44.    
  45.     //Game_Enemy.prototype.battlerHeight = null;
  46.     //Game_Enemy.prototype.battlerWidth = null;
  47.     _Game_Enemy_prototype.battlerHeight = null;
  48.     _Game_Enemy_prototype.battlerWidth = null;
  49.    
  50.     var high = parameters['High HP'] || '#009900';
  51.     var medium = parameters['Medium HP'] || '#ffcc00';
  52.     var low = parameters['Low HP'] || '#ff6600';
  53.     var critical = parameters['Critical HP'] || '#ff3300';
  54.    
  55.    
  56.     //==========================================================================
  57.     // Create enemy HP window to display all the health bars.
  58.     //==========================================================================
  59.    
  60.     function Enemy_Bars() {
  61.         //this.initialize.apply(this, arguments);
  62.         this.initialize.apply(this, arguments);
  63.     };
  64.    
  65.     // Create a new window object for the new function.
  66.     // We'll use Window_Base for this.
  67.     Enemy_Bars.prototype = Object.create(Window_Base.prototype);
  68.    
  69.     // Then set up a constructor.
  70.     Enemy_Bars.prototype.constructor = Enemy_Bars;
  71.    
  72.    
  73.     // Next we'll define the position and look of the bars.
  74.     Enemy_Bars.prototype.initialize = function(x, y, width, height) {
  75.        
  76.         // Setup the window hovering above the Enemy with the tag.
  77.         Window_Base.prototype.initialize.call(this, x, y, width, height);
  78.        
  79.         // Then deactivate the initialization.
  80.         this.deactivate();
  81.        
  82.         // Then make transparent.
  83.         this.opacity = 0;
  84.     };
  85.    
  86.    
  87.     // This update function clears the window and updates all the bars.
  88.     Enemy_Bars.prototype.update = function() {
  89.        
  90.         Window_Base.prototype.update.call(this);
  91.         this.contents.clear();
  92.         this.drawBar();
  93.        
  94.     };
  95.    
  96.    
  97.     // Then return the colour of the HP bar based on current HP!
  98.     Enemy_Bars.prototype.getColour = function(rate) {
  99.         if(rate > 0.75) {
  100.             return high;
  101.         }
  102.         else if(rate > 0.50) {
  103.             return medium;
  104.         }
  105.         else if(rate > 0.25) {
  106.             return low;
  107.         }
  108.         else {
  109.             return critical;
  110.         }
  111.     };
  112.    
  113.     // This is the variable for the linebreak name offset.
  114.     // I figured it was more efficiant to define this as a constant,
  115.     // instead of redefining it over and over again in the loop.
  116.     var nameOffset = 40;
  117.    
  118.     // Finally draw the bars.
  119.     // Pay attention to the loop!
  120.     // This will be useful for many scripts!
  121.     Enemy_Bars.prototype.drawBar = function() {
  122.        
  123.         // Loop through the enemies and draw their bars.
  124.         for(var i = 0; i < $gameTroop._enemies.length; i++) {
  125.            
  126.             // Store enemy ID in variable.
  127.             var enemyId = $gameTroop._enemies[i]._enemyId;
  128.            
  129.             // Store enemy selection in variable.
  130.             var selected = $gameTroop._enemies[i]._selected;
  131.            
  132.             // Check to see if it is enemy's turn.
  133.             // Used for HP bar shown only when enemy attacks.
  134.             if(BattleManager._phase === "action" || BattleManager._phase === "turn") {
  135.                 var turn = true;
  136.             }
  137.             else {
  138.                 var turn = false;
  139.                 //console.log(BattleManager._phase);
  140.             }
  141.            
  142.             // Boolean check to see if health bars are enabled.
  143.             var enabled = false;
  144.             var isBoss = false;
  145.             // Read data from enemy notes.
  146.             // $dataEnemies[id].meta appears to be what reads the notes.
  147.             var meta = new Array(5);
  148.             meta[0] = $dataEnemies[enemyId].meta.hpBar;
  149.             meta[1] = $dataEnemies[enemyId].meta.hpBarSelectionOnly;
  150.             meta[2] = $dataEnemies[enemyId].meta.hpBarTurnASelection;
  151.             meta[3] = $dataEnemies[enemyId].meta.hpBarTurnOnly;
  152.             meta[4] = $dataEnemies[enemyId].meta.hpBarBoss;
  153.            
  154.             if(meta[0]) {
  155.                 enabled = true;
  156.                 //console.log(meta[0]);
  157.             }
  158.             else if(meta[1] && selected) {
  159.                 enabled = true;
  160.                 //console.log(meta[1]);
  161.             }
  162.             else if(meta[2] && (selected || turn)) {
  163.                 enabled = true;
  164.                 //console.log(meta[2]);
  165.             }
  166.             else if(meta[3] && turn) {
  167.                 enabled = true;
  168.                 //console.log(meta[3]);
  169.             }
  170.             else if(meta[4]) {
  171.                 enabled = true;
  172.                 isBoss = true;
  173.                
  174.             }
  175.             else {
  176.                 enabled = false;
  177.                 // I noticed this was spamming the console.
  178.                 // Presumably it was from the original creator's dev process.
  179.                 //console.log('No meta detected.');
  180.             }
  181.            
  182.             // This is one mother of a loop!
  183.             // Anyway, get current HP.
  184.             var currentHp = $gameTroop._enemies[i]._hp;
  185.             // If enemy HP is not zero, draw HP bar.
  186.             if(currentHp > 0 && enabled) {
  187.                 // Get Max HP
  188.                 var maxHp = $dataEnemies[enemyId].params[0];
  189.                
  190.                 // Rate will be the current HP compared to max HP.
  191.                 var rate = currentHp/maxHp;
  192.                
  193.                 var enemyName = $gameTroop._enemies[i].name();
  194.                 /*if(isBoss)
  195.                 {
  196.                     //this.changeTextSize(18);
  197.                     enemyName = String('(BOSS) ' + enemyName);
  198.                 }
  199.                 else {
  200.                     //this.changeTextSize(12);
  201.                 }*/
  202.                
  203.                 // If monster is not a boss...
  204.                 //
  205.                 // Then set the width of the bar.
  206.                 var condi = String(/*enemyName + '\n' + */String(currentHp)+ ' / ' + String(maxHp));
  207.                 var width = this.textWidth(condi);//$dataEnemies[enemyId].battlerWidth;
  208.                 // Then its position.
  209.                 var xOffset = width / 2 + 16;
  210.                 var x = $gameTroop._enemies[i]._screenX - xOffset;
  211.                 var y = $gameTroop._enemies[i]._screenY - (64 * 4);
  212.                
  213.                
  214.                 // Boss check.
  215.                 if(!isBoss)
  216.                 {
  217.                     this.drawGauge(x, y, width, rate, this.getColour(rate), this.getColour(rate));
  218.                     this.changeTextColor(this.getColour(rate));
  219.                     // We have to drawText twice in order to create a line break.
  220.                     this.drawText(enemyName, x, (y - nameOffset), width, 'center');
  221.                     this.drawText(condi, x, (y-8), width, 'center');
  222.                 }
  223.                 else {
  224.                     width = Window_BattleStatus.prototype.windowWidth();
  225.                     var height = Window_BattleStatus.prototype.windowHeight();
  226.                     // I couldn't work out how to automate finding the centre so...
  227.                     // This is an approximation gained through trial & error.
  228.                     x = (Graphics.boxWidth / 10) - 2.5;
  229.                     y = (Graphics.boxHeight - (height * 1.32));
  230.                     this.drawGauge(x, y, width, rate, this.getColour(rate), this.getColour(rate));
  231.                     this.changeTextColor(this.getColour(rate));
  232.                     // drawText twice for linebreak.
  233.                     this.drawText(enemyName, x, (y - nameOffset), width, 'center');
  234.                     this.drawText(condi, x, (y-8), width, 'center');
  235.                 }                
  236.             }
  237.            
  238.         }
  239.        
  240.        
  241.     };
  242.    
  243.    
  244.     //==========================================================================
  245.     // Alias the Scene_Battle createAllWindows to add the HP window.
  246.     //==========================================================================
  247.    
  248.     var _Scene_Battle_createAllWindows = Scene_Battle.prototype.createAllWindows;
  249.     Scene_Battle.prototype.createAllWindows = function() {
  250.         _Scene_Battle_createAllWindows.call(this);
  251.         this.Bar = new Enemy_Bars(0, 0, Graphics.width, Graphics.height);
  252.         this.addChildAt(this.Bar, 1);
  253.     };
  254.    
  255.    
  256.     //==========================================================================
  257.     // Then alias the Sprite_Enemy.updateFrame to return information back to HP window.
  258.     //==========================================================================
  259.    
  260.     var _Sprite_Enemy_update = Sprite_Enemy.prototype.updateFrame;
  261.     Sprite_Enemy.prototype.updateFrame = function() {
  262.         Sprite_Battler.prototype.updateFrame.call(this);
  263.         _Sprite_Enemy_update.call(this);
  264.         // Then return bitmaps height and width, as we're
  265.         // updating sprites.
  266.         $dataEnemies[this._enemy._enemyId].battlerHeight = this.bitmap.height;
  267.         $dataEnemies[this._enemy._enemyId].battlerWidth = this.bitmap.width;
  268.     };
  269.    
  270. })();
Advertisement
Add Comment
Please, Sign In to add comment