Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Script to show enemy HP??

khmp

Sponsor

RMVX Show HP Bars over enemies. There are a few constants you can play with to get different results and they are located at the top of Enemy_HPBar's class definition.  A word of notice. The battler sprites I have no idea what math they use to position there sprites but I can't seem to get it where I think it should be all the time. The the health bar might go wacky with certain sprites. I tested a few and the results were ok. But that's just a warning.

Code:
#==============================================================================
# ** HP_Bar
#------------------------------------------------------------------------------
#  This sprite will show the user how much HP an enemy has.
#==============================================================================

class Enemy_HPBar < Sprite_Base
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  # The bar uses a gradient. Default is (red to green)/(left to right) 
  # respectively.
  HP_COLOR_LEFT = 
    Color.new(255, 0, 0) # The left color of the HP Bar. *Default : Red*
  HP_COLOR_RIGHT = 
    Color.new(0, 255, 0) # The right color of the HP Bar. *Default : Green*
  HP_BAR_LENGTH = 64     # The length of the bar. 1 pixel border is not 
                         # taken into account.
  HP_BAR_HEIGHT = 6      # The height ob the bar. 1 pixel border is not
                         # taken into account.
                              
  HP_BAR_OPACITY = 170   # The opacity of the bar.
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(viewport, sprite_battler)
    super(viewport)
    # Save the battler.
    @sprite_battler = sprite_battler
    
    self.opacity = HP_BAR_OPACITY
    
    # Create the bitmap.
    self.bitmap = Bitmap.new(HP_BAR_LENGTH + 2, HP_BAR_HEIGHT + 2)
    
    # Create the outline it will never change.
    self.bitmap.fill_rect(0, 0, HP_BAR_LENGTH + 2, 1, Color.new(0, 0, 0))
    self.bitmap.fill_rect(0, HP_BAR_HEIGHT + 1, HP_BAR_LENGTH + 2, 1, Color.new(0, 0, 0))
    self.bitmap.fill_rect(0, 1, 1, HP_BAR_HEIGHT, Color.new(0, 0, 0))
    self.bitmap.fill_rect(HP_BAR_LENGTH + 1, 1, 1, HP_BAR_HEIGHT, Color.new(0, 0, 0))
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super()
    
    # Turn it off if the battler is either dead or hidden.
    self.visible = @sprite_battler.visible
    
    # Return if this object can not be seen.
    return unless self.visible
    
    # Put the bar dead center over the sprite.
    self.x = @sprite_battler.x + 
      (@sprite_battler.bitmap.width / 2) - HP_BAR_LENGTH
    # Move it up away from the sprite just a little.
    self.y = @sprite_battler.y - 10
    self.z = @sprite_battler.z + 1
    
    if @hp != @sprite_battler.battler.hp
      # Clear out the portion we want to draw to.
      self.bitmap.clear_rect(1, 1, HP_BAR_LENGTH, HP_BAR_HEIGHT)
      # Save the current hp.
      @hp = @sprite_battler.battler.hp
      # Determine how many pixels wide the health bar should be.
      pixel_width = HP_BAR_LENGTH * (@hp / @sprite_battler.battler.maxhp.to_f)
      # Draw the health bar.
      self.bitmap.gradient_fill_rect(Rect.new(1, 1, pixel_width, HP_BAR_HEIGHT), 
        HP_COLOR_LEFT, HP_COLOR_RIGHT)
    end
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super()
    self.bitmap.dispose unless self.bitmap.disposed?
  end
end

#==============================================================================
# ** Spriteset_Battle
#------------------------------------------------------------------------------
#  This class brings together battle screen sprites. It's used within the
# Scene_Battle class.
#==============================================================================

class Spriteset_Battle
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias_method :djfl_hpbars_spriteset_battle_create_enemies, :create_enemies
  alias_method :djfl_hpbars_spriteset_battle_dispose_enemies, :dispose_enemies
  alias_method :djfl_hpbars_spriteset_battle_update_enemies, :update_enemies
  #--------------------------------------------------------------------------
  # * Create Enemy Sprite
  #--------------------------------------------------------------------------
  def create_enemies
    djfl_hpbars_spriteset_battle_create_enemies
    @enemy_hpbars = []
    @enemy_sprites.each { |enemy| 
      @enemy_hpbars << Enemy_HPBar.new(@viewport1, enemy) 
    }
  end
  #--------------------------------------------------------------------------
  # * Dispose of Enemy Sprite
  #--------------------------------------------------------------------------
  def dispose_enemies
    djfl_hpbars_spriteset_battle_dispose_enemies
    @enemy_hpbars.each { |enemy_hp| enemy_hp.dispose }
  end
  #--------------------------------------------------------------------------
  # * Update Enemy Sprite
  #--------------------------------------------------------------------------
  def update_enemies
    djfl_hpbars_spriteset_battle_update_enemies
    @enemy_hpbars.each { |enemy_hp| enemy_hp.update }
  end
end

Good luck with it djflan! :thumb:
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top