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.

Need a small add-on of an enemy HP bar script.

KAIRE

Member

I'm planning to use this enemy HP bar script.

Code:
class Window_Base
  # define enemy name
  def draw_enemy_name(enemy, x, y)
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, 120, 32, enemy.name)
  end
  
  #--------------------------------------------------------------------------
  # * Draw Slant Bar(by SephirothSpawn)
  #--------------------------------------------------------------------------
  def draw_slant_bar(x, y, min, max, width = 152, height = 6,
      bar_color = Color.new(0, 150, 150, 255), end_color = Color.new(0, 255, 255, 255))
    # Draw Border
    for i in 0..height
      self.contents.fill_rect(x + i, y + height - i, width + 1, 1, Color.new(50, 50, 50, 255))
    end
    # Draw Background
    for i in 1..(height - 1)
      r = 100 * (height - i) / height + 0 * i / height
      g = 100 * (height - i) / height + 0 * i / height
      b = 100 * (height - i) / height + 0 * i / height
      a = 255 * (height - i) / height + 255 * i / height
      self.contents.fill_rect(x + i, y + height - i, width, 1, Color.new(r, b, g, a))
    end
    # Draws Bar
    for i in 1..( (min / max.to_f) * width - 1)
      for j in 1..(height - 1)
        r = bar_color.red * (width - i) / width + end_color.red * i / width
        g = bar_color.green * (width - i) / width + end_color.green * i / width
        b = bar_color.blue * (width - i) / width + end_color.blue * i / width
        a = bar_color.alpha * (width - i) / width + end_color.alpha * i / width
        self.contents.fill_rect(x + i + j, y + height - j, 1, 1, Color.new(r, g, b, a))
      end
    end
  end
end

#ENEMY HP WINDOW
class Window_EnemyHP < Window_Base
def initialize
super(0, 0, 160, 106)
self.contents = Bitmap.new(width - 32, height - 32)
self.opacity = 0
refresh
end

#REFRESH
def refresh
   self.contents.clear
    for i in 0...$game_troop.enemies.size
      enemy = $game_troop.enemies[i]
        x = 0
        y = i * 25
       draw_slant_bar(x, y + 25, enemy.hp, enemy.maxhp, width = 100, height = 6, bar_color = Color.new(0, 100, 100, 255), end_color = Color.new(0, 255, 200, 255))
       draw_enemy_name(enemy, x, y)
     end
   end
 end
 
class Scene_Battle
  
  alias queed_update update
  alias queed_update_phase5 update_phase5
  alias queed_update_phase4_step1 update_phase4_step1
  alias queed_update_phase4_step5 update_phase4_step5
  alias queed_start_skill_select start_skill_select
  alias queed_end_skill_select end_skill_select
  alias queed_start_item_select start_item_select
  alias queed_end_item_select end_item_select
  
  def main
    # Initialize each kind of temporary battle data
    $game_temp.in_battle = true
    $game_temp.battle_turn = 0
    $game_temp.battle_event_flags.clear
    $game_temp.battle_abort = false
    $game_temp.battle_main_phase = false
    $game_temp.battleback_name = $game_map.battleback_name
    $game_temp.forcing_battler = nil
    # Initialize battle event interpreter
    $game_system.battle_interpreter.setup(nil, 0)
    # Prepare troop
    @troop_id = $game_temp.battle_troop_id
    $game_troop.setup(@troop_id)
    # Make actor command window
    s1 = $data_system.words.attack
    s2 = $data_system.words.skill
    s3 = $data_system.words.guard
    s4 = $data_system.words.item
    @actor_command_window = Window_Command.new(160, [s1, s2, s3, s4])
    @actor_command_window.y = 160
    @actor_command_window.back_opacity = 160
    @actor_command_window.active = false
    @actor_command_window.visible = false
    # Make other windows
    @party_command_window = Window_PartyCommand.new
    @help_window = Window_Help.new
    @help_window.back_opacity = 160
    @help_window.visible = false
    @status_window = Window_BattleStatus.new
    @message_window = Window_Message.new
    # Make sprite set
    @spriteset = Spriteset_Battle.new
    @enemy_window = Window_EnemyHP.new
    @enemy_window.z = 255

    # Initialize wait count
    @wait_count = 0
    # Execute transition
    if $data_system.battle_transition == ""
      Graphics.transition(20)
    else
      Graphics.transition(40, "Graphics/Transitions/" +
        $data_system.battle_transition)
    end
    # Start pre-battle phase
    start_phase1
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Refresh map
    $game_map.refresh
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @actor_command_window.dispose
    @party_command_window.dispose
    @help_window.dispose
    @status_window.dispose
    @message_window.dispose
    @enemy_window.dispose
    if @skill_window != nil
      @skill_window.dispose
    end
    if @item_window != nil
      @item_window.dispose
    end
    if @result_window != nil
      @result_window.dispose
    end
    # Dispose of sprite set
    @spriteset.dispose
    # If switching to title screen
    if $scene.is_a?(Scene_Title)
      # Fade out screen
      Graphics.transition
      Graphics.freeze
    end
    # If switching from battle test to any screen other than game over screen
    if $BTEST and not $scene.is_a?(Scene_Gameover)
      $scene = nil
    end
  end
  
  def update
    @enemy_window.update
    queed_update
end

  def start_skill_select
    queed_start_skill_select
    @enemy_window.visible = false
  end

    def end_skill_select
     queed_end_skill_select
     @enemy_window.visible = true
    end

    def start_item_select
      queed_start_item_select
      @enemy_window.visible = false
    end
    
    def end_item_select
      queed_end_item_select
      @enemy_window.visible = true
    end
    
  def update_phase5
    # If wait count is larger than 0
    if @phase5_wait_count > 0
      # Decrease wait count
      @phase5_wait_count -= 1
      # If wait count reaches 0
      if @phase5_wait_count == 0
        @enemy_window.visible = false
        # Show result window
        @result_window.visible = true
        # Clear main phase flag
        $game_temp.battle_main_phase = false
        # Refresh status window
        @status_window.refresh
        @enemy_window.refresh
      end
      return
    end
queed_update_phase5
end

def update_phase4_step1
  queed_update_phase4_step1
  @enemy_window.refresh
end

  def update_phase4_step5
    # Hide help window
    @help_window.visible = false
    # Refresh status window
    @status_window.refresh
    @enemy_window.refresh
    queed_update_phase4_step5 
  end
end

But what I want is an add-on so that Bosses HP Bar are much longer than non-boss-enemies. So that's want I need and I hope someone here can help me with this, thanks. :please: Ofcourse credit will be given. (That is if my game will ever be completed...':| )
 
instead of putting "i" for how big it should be, why not set a default HP bar limit?
I'm no scripter but I think thats how its done...

example:

r = bar_color.red * (width - 300) / width + end_color.red * i / width
g = bar_color.green * (width - 300) / width + end_color.green * i / width
b = bar_color.blue * (width - 300) / width + end_color.blue * i / width
a = bar_color.alpha * (width - 300) / width + end_color.alpha * i / width

I think thats how you do it...
sry..but I'm trying to use much logic as I can to do these...I dunno anything about scripting but I'm reading into it.

sry sry
 

KAIRE

Member

hmmm... I don't get what you're trying to say...

this is the code for how long the bar is now...

draw_slant_bar(x, y + 25, enemy.hp, enemy.maxhp, width = 100, height = 6, bar_color = Color.new(0, 100, 100, 255), end_color = Color.new(0, 255, 200, 255))

I need this for regular enemy and for bossed the width will be about 300-400 long. Ofcourse I know it will get cut off by the window super(0, 0, 160, 106). But im gonna chnage the size of that later to fit since i have the opacity on 0 anyways it doesn't matter how big the window is.
 
Use this:
Code:
BOSS_ENEMIES = [1,2,3] # Put here the id of the enemies that are bosses
class Window_EnemyHP < Window_Base
  def initialize
    super(0, 0, 640, 106)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 0
    self.z -= 1000
    refresh
  end
  def refresh
   self.contents.clear
    for i in 0...$game_troop.enemies.size
      enemy = $game_troop.enemies[i]
      x = 0
      y = i * 25
      if BOSS_ENEMIES.include?(enemy.id)
        width = 400 # Change here the width of the boss hp var
      else
        width = 100 # Change here the width of the normal enemy hp var
      end
      draw_slant_bar(x, y + 25, enemy.hp, enemy.maxhp, width, height = 6, bar_color = Color.new(0, 100, 100, 255), end_color = Color.new(0, 255, 200, 255))
      draw_enemy_name(enemy, x, y)
    end
  end
end
PD: This script is very buggy(Not what I modify, the original ones)
 
I second that opinion. It also has a problem in that the width and length of the window is 'substandard' if you had more than two enemies at the same time. The window didn't resize.

Here's the same script that vgvgf just posted, with a resizing tweak, and besides an extra category of enemies... I transparencied the bars, in case they went in front of the enemies...
Code:
BOSS_ENEMIES = [1,2,3] # Put here the id of the enemies that are bosses
UBER_ENEMIES=[4] # For those pesky... uberbosses

#==============================================================================
# ** Window_EnemyHP
#------------------------------------------------------------------------------
#  This window is used to display the Enemy's name and HP bars.
#==============================================================================
class Window_EnemyHP < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------  
  def initialize
    e_check = $game_troop.enemies.size
    super(0, 0, 300, 32 + (32 * e_check))
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 0
    refresh
  end  
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------  
  def refresh
    self.contents.clear
    for i in 0...$game_troop.enemies.size
      enemy = $game_troop.enemies[i]
      x = 0
      y = i * 25
      # Area you can change...
      if BOSS_ENEMIES.include?(enemy.id)
        width = 175
      elsif UBER_ENEMIES.include?(enemy.id)
        width = 250
      else
        width = 100
      end
      # End of changes, other than the width condition   V, and transparent bars
      draw_slant_bar(x, y + 25, enemy.hp, enemy.maxhp, width, height = 6, 
                                bar_color = Color.new(0, 100, 100, 128),
                                end_color = Color.new(0, 255, 200, 64))
      draw_enemy_name(enemy, x, y)
    end
  end
end
 

KAIRE

Member

...so where do I put this code?

Add it into my code above in post #1 or add a new scene above main?

And what about my code above, is that still needed or what....?
 

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