Advertisement
Jnk1296

StarField Health Bar

May 30th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package net.risenphoenix.jnk.StarField.HUD;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6.  
  7. import net.risenphoenix.jnk.StarField.Engine;
  8. import net.risenphoenix.jnk.StarField.INIT;
  9.  
  10. public class HealthBar implements SFHudElement {
  11.  
  12.     double currentHealth;
  13.     double healthLimit;
  14.     double percentage;
  15.    
  16.     BufferedImage heart1;
  17.     BufferedImage heart2;
  18.     BufferedImage heart3;
  19.    
  20.     public HealthBar() {
  21.         this.heart1 = Engine.RE.getResource("heart_full");
  22.         this.heart2 = Engine.RE.getResource("heart_half");
  23.         this.heart3 = Engine.RE.getResource("heart_none");
  24.     }
  25.    
  26.     @Override
  27.     public void drawElement(Graphics g) {      
  28.         this.currentHealth = Engine.game.getPlayer().getHealth();
  29.         this.healthLimit = Engine.game.getPlayer().getHealthLimit();
  30.         int numFull;
  31.         int numEmpty;
  32.         int halfPos;
  33.        
  34.         numEmpty = (int) Math.ceil((100 - this.currentHealth) / 10);
  35.         numFull = (int)this.currentHealth / 10;
  36.         halfPos = numFull;
  37.        
  38.         boolean shouldDrawHalf = false;
  39.         boolean shouldDrawFull = false;
  40.        
  41.         // Half Heart Logic
  42.         if (((int)this.currentHealth % 10) < 5 && ((int)this.currentHealth % 10) > 0) {
  43.             shouldDrawHalf = true;
  44.         } else if (((int)this.currentHealth % 10) > 5) {
  45.             shouldDrawFull = true;
  46.         }
  47.        
  48.         // Dead Hearts
  49.         for(int i = (INIT.getFrameSize().width - 40) - (9*23); i < INIT.getFrameSize().width && numEmpty > 0; i += 23) {
  50.             g.drawImage(this.heart3, i, 5, null);
  51.             numEmpty--;
  52.         }
  53.        
  54.         // Full Hearts
  55.         for(int i = INIT.getFrameSize().width - 40; i > 0 && numFull > 0; i -= 23) {
  56.             g.drawImage(this.heart1, i, 5, null);
  57.             numFull--;
  58.         }
  59.        
  60.         // For Dynamic Half Heart
  61.         if (shouldDrawHalf) {
  62.             g.drawImage(this.heart2, (INIT.getFrameSize().width - 40) - (halfPos*23), 5, null);
  63.         } else if (shouldDrawFull) {
  64.             g.drawImage(this.heart1, (INIT.getFrameSize().width - 40) - (halfPos*23), 5, null);
  65.         }
  66.        
  67.         g.setColor(Color.WHITE);
  68.        
  69.         int xPos;
  70.        
  71.         // Determine xPosition of Readout
  72.         if (this.currentHealth == 100) {
  73.             xPos = ((INIT.getFrameSize().width - 40) - (10*23) - 5);
  74.         } else {
  75.             xPos = ((INIT.getFrameSize().width - 40) - (10*23));
  76.         }
  77.        
  78.         // Numeric Readout
  79.         g.drawString("" + (int)this.currentHealth, xPos, 20);
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement