Vlue

Basic Enemy HP Bars

Nov 24th, 2012
22,032
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #--# Basic Enemy HP Bars v 2.9
  2. #
  3. # Adds customizable hp bars to enemies in battle. See configuration
  4. #  below for more detail. Also allows for the option of using a nice
  5. #  graphical hp bar from a image file.
  6. #
  7. # Usage: Plug and play, customize as needed.
  8. #
  9. #------#
  10. #-- Script by: V.M of D.T
  11. #
  12. #- Questions or comments can be:
  13. #    given by email: sumptuaryspade@live.ca
  14. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  15. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  16. #
  17. #--- Free to use in any project, commercial or non-commercial, with credit given
  18. # - - Though a donation's always a nice way to say thank you~ (I also accept actual thank you's)
  19.  
  20. #Customization starts here:
  21. module DTP_HP
  22.   #Whether to place the hp bar above or below the enemy
  23.   ABOVE_MONSTER = true
  24.   #Whether to use a custome image or not:
  25.   #Image would be placed in Graphics/System and named Custom_HP.png
  26.   CUSTOM_BAR = false
  27.   #Whether to include the hp bar or not
  28.   USE_HP_BAR = true
  29.   #Whether to include an mp bar or not
  30.   USE_MP_BAR = true
  31.  
  32.   #The width of the hp bar
  33.   BAR_WIDTH = 66
  34.   #The height of the hp bar
  35.   BAR_HEIGHT = 5
  36.   #The width of the border around the hp bar
  37.   BORDER_WIDTH = 1
  38.   #The height of the border around the hp bar
  39.   BORDER_HEIGHT = 1
  40.   #Offset the hp bar along the x-axis(left,right)
  41.   BAR_OFFSET_X = 0
  42.   #Offset the hp bar along the y-axis(up,down)
  43.   BAR_OFFSET_Y = 0
  44.  
  45.   #Color for the back of the hp bar
  46.   COLOR_BAR_BACK = Color.new(0,0,0,200)
  47.   #First color for the hp bar gradient
  48.   COLOR_BAR_1 = Color.new(255,0,0)
  49.   #Second color for the hp bar gradient
  50.   COLOR_BAR_2 = Color.new(200,100,100)
  51.   #Outside border color
  52.   COLOR_BORDER_1 = Color.new(0,0,0,185)
  53.   #Inside border color
  54.   COLOR_BORDER_2 = Color.new(255,255,255,185)
  55.   #First color for the mp bar gradient
  56.   MP_COLOR_BAR_1 = Color.new(0,175,255)
  57.   #Second color fot he mp bar gradient
  58.   MP_COLOR_BAR_2 = Color.new(0,0,255)
  59.  
  60.   #Whether to display text or not
  61.   USE_TEXT = true
  62.   #Text to be displayed, chp = current hp, mhp = max hp, php = percentage hp
  63.   #Examples: "php%" or "chp/mhp" or "chp - php%"
  64.   TEXT_DISPLAY = "chp"
  65.   #Offset for the text along the x-axis(left,right)
  66.   TEXT_OFFSET_X = 5
  67.   #Offset for the text along the y-axis(up,down)
  68.   TEXT_OFFSET_Y = -24
  69.   #Size of the displayed text
  70.   TEXT_SIZE = Font.default_size
  71.   #Font of the displayed text
  72.   TEXT_FONT = Font.default_name
  73.  
  74.   #Show bars only when specific actor in party. Array format. Example: [8,7]
  75.   #Set to [] to not use actor only
  76.   SPECIFIC_ACTOR = []
  77.   #Show enemy hp bar only if certain state is applied (like a scan state)
  78.   #Set to 0 to not use state only
  79.   SCAN_STATE = 0
  80.   #Enemies will show hp bar as long as they have been affected but scan state
  81.   #at least once before
  82.   SCAN_ONCE = false
  83.   #Hp bars will only show when you are targetting a monster
  84.   ONLY_ON_TARGET = false
  85.  
  86.   #Text to display if it's a boss monster, accepts same arguments
  87.   BOSS_TEXT = "???"
  88.   #The width of the boss hp bar
  89.   BOSS_BAR_WIDTH = 66
  90.   #The height of the boss hp bar
  91.   BOSS_BAR_HEIGHT = 5
  92.   #The width of the border around the boss hp bar
  93.   BOSS_BORDER_WIDTH = 1
  94.   #The height of the border around the boss hp bar
  95.   BOSS_BORDER_HEIGHT = 1
  96.   #ID's of boss monsters in array format.
  97.   BOSS_MONSTERS = []
  98. end
  99. #Customization ends here
  100.  
  101. class Sprite_Battler
  102.   alias hpbar_update update
  103.   alias hpbar_dispose dispose
  104.   def update
  105.     hpbar_update
  106.     return unless @battler.is_a?(Game_Enemy)
  107.     if @battler
  108.       update_hp_bar
  109.     end
  110.   end
  111.   def update_hp_bar
  112.     boss = DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
  113.     setup_bar if @hp_bar.nil?
  114.     if @text_display.nil?
  115.       @text_display = Sprite_Base.new(self.viewport)
  116.       @text_display.bitmap = Bitmap.new(100,DTP_HP::TEXT_SIZE)
  117.       @text_display.bitmap.font.size = DTP_HP::TEXT_SIZE
  118.       @text_display.bitmap.font.name = DTP_HP::TEXT_FONT
  119.       @text_display.x = @hp_bar.x + DTP_HP::TEXT_OFFSET_X
  120.       @text_display.y = @hp_bar.y + DTP_HP::TEXT_OFFSET_Y
  121.       @text_display.z = 105
  122.     end
  123.     determine_visible
  124.     return unless @hp_bar.visible
  125.     if @hp_bar.opacity != self.opacity
  126.       @hp_bar.opacity = self.opacity
  127.       @mp_bar.opacity = @hp_bar.opacity if DTP_HP::USE_MP_BAR
  128.     end
  129.     @hp_bar.bitmap.clear
  130.     if !boss
  131.       width = DTP_HP::BAR_WIDTH
  132.       height = DTP_HP::BAR_HEIGHT
  133.       bwidth = DTP_HP::BORDER_WIDTH
  134.       bheight = DTP_HP::BORDER_HEIGHT
  135.     else
  136.       width = DTP_HP::BOSS_BAR_WIDTH
  137.       height = DTP_HP::BOSS_BAR_HEIGHT
  138.       bwidth = DTP_HP::BOSS_BORDER_WIDTH
  139.       bheight = DTP_HP::BOSS_BORDER_HEIGHT
  140.     end
  141.     btotal = (bwidth + bheight) * 2
  142.     rwidth = @hp_bar.bitmap.width
  143.     rheight = @hp_bar.bitmap.height
  144.     if !DTP_HP::CUSTOM_BAR && DTP_HP::USE_HP_BAR
  145.       @hp_bar.bitmap.fill_rect(0,0,rwidth,rheight,DTP_HP::COLOR_BAR_BACK)
  146.       @hp_bar.bitmap.fill_rect(bwidth,bheight,rwidth-bwidth*2,rheight-bheight*2,DTP_HP::COLOR_BORDER_2)
  147.       @hp_bar.bitmap.fill_rect(bwidth*2,bheight*2,width,height,DTP_HP::COLOR_BORDER_1)
  148.     end
  149.     hp_width = @battler.hp_rate * width
  150.     if DTP_HP::USE_HP_BAR
  151.       @hp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,hp_width,height,DTP_HP::COLOR_BAR_1,DTP_HP::COLOR_BAR_2)
  152.     end
  153.     if DTP_HP::CUSTOM_BAR && DTP_HP::USE_HP_BAR
  154.       border_bitmap = Bitmap.new("Graphics/System/Custom_HP.png")
  155.       rect = Rect.new(0,0,border_bitmap.width,border_bitmap.height)
  156.       @hp_bar.bitmap.blt(0,0,border_bitmap,rect)
  157.     end
  158.     if DTP_HP::USE_MP_BAR
  159.       @mp_bar.bitmap.clear
  160.       if !DTP_HP::CUSTOM_BAR
  161.         @mp_bar.bitmap.fill_rect(0,0,rwidth,rheight,DTP_HP::COLOR_BAR_BACK)
  162.         @mp_bar.bitmap.fill_rect(bwidth,bheight,rwidth-bwidth*2,rheight-bheight*2,DTP_HP::COLOR_BORDER_2)
  163.         @mp_bar.bitmap.fill_rect(bwidth*2,bheight*2,width,height,DTP_HP::COLOR_BORDER_1)
  164.       end
  165.       mp_width = @battler.mp_rate * width
  166.       @mp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,mp_width,height,DTP_HP::MP_COLOR_BAR_1,DTP_HP::MP_COLOR_BAR_2)
  167.       if DTP_HP::CUSTOM_BAR
  168.         border_bitmap = Bitmap.new("Graphics/System/Custom_HP.png")
  169.         rect = Rect.new(0,0,border_bitmap.width,border_bitmap.height)
  170.         @mp_bar.bitmap.blt(0,0,border_bitmap,rect)
  171.       end
  172.     end
  173.     return unless DTP_HP::USE_TEXT
  174.     @text_display.opacity = @hp_bar.opacity if @text_display.opacity != @hp_bar.opacity
  175.     @text_display.bitmap.clear
  176.     text = DTP_HP::TEXT_DISPLAY.clone
  177.     text = DTP_HP::BOSS_TEXT.clone if DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
  178.     text.gsub!(/chp/) {@battler.hp}
  179.     text.gsub!(/mhp/) {@battler.mhp}
  180.     text.gsub!(/php/) {(@battler.hp_rate * 100).to_i}
  181.     @text_display.bitmap.draw_text(0,0,100,@text_display.height,text)
  182.   end
  183.   def setup_bar
  184.     boss = DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
  185.     @hp_bar = Sprite_Base.new(self.viewport)
  186.     if !boss
  187.       width = DTP_HP::BAR_WIDTH + DTP_HP::BORDER_WIDTH * 4
  188.       height = DTP_HP::BAR_HEIGHT + DTP_HP::BORDER_HEIGHT * 4
  189.     else
  190.       width = DTP_HP::BOSS_BAR_WIDTH + DTP_HP::BOSS_BORDER_WIDTH * 4
  191.       height = DTP_HP::BOSS_BAR_HEIGHT + DTP_HP::BOSS_BORDER_HEIGHT * 4
  192.     end
  193.     if DTP_HP::CUSTOM_BAR
  194.       tempbmp = Bitmap.new("Graphics/System/Custom_HP.png")
  195.       width = tempbmp.width
  196.       height = tempbmp.height
  197.     end
  198.     @hp_bar.bitmap = Bitmap.new(width,height)
  199.     @hp_bar.x = self.x - @hp_bar.width / 2 + DTP_HP::BAR_OFFSET_X
  200.     @hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y - self.bitmap.height - @hp_bar.height
  201.     @hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y unless DTP_HP::ABOVE_MONSTER
  202.     @hp_bar.x = 0 if @hp_bar.x < 0
  203.     @hp_bar.y = 0 if @hp_bar.y < 0
  204.     @hp_bar.z = 104
  205.     if DTP_HP::USE_MP_BAR
  206.       @mp_bar = Sprite_Base.new(self.viewport)
  207.       @mp_bar.bitmap = Bitmap.new(@hp_bar.width,@hp_bar.height)
  208.       @mp_bar.x = @hp_bar.x + 6
  209.       @mp_bar.y = @hp_bar.y + @mp_bar.height - 3
  210.       @mp_bar.z = 103
  211.     end
  212.   end
  213.   def determine_visible
  214.     if !@battler.alive?
  215.       @hp_bar.visible = false
  216.       @mp_bar.visible = false if @mp_bar
  217.       @text_display.visible = false
  218.       if DTP_HP::SCAN_ONCE and DTP_HP::SCAN_STATE == 1
  219.         $game_party.monster_scans[@battler.enemy_id] = true
  220.       end
  221.       return if !@battler.alive?
  222.     end
  223.     @hp_bar.visible = true
  224.     if DTP_HP::SCAN_STATE != 0
  225.       @hp_bar.visible = false
  226.       @hp_bar.visible = true if @battler.state?(DTP_HP::SCAN_STATE)
  227.       if DTP_HP::SCAN_ONCE
  228.         @hp_bar.visible = true if $game_party.monster_scans[@battler.enemy_id] == true
  229.         $game_party.monster_scans[@battler.enemy_id] = true if @hp_bar.visible
  230.       end
  231.     end
  232.     if !DTP_HP::SPECIFIC_ACTOR.empty?
  233.       @hp_bar.visible = false unless DTP_HP::SCAN_STATE != 0
  234.       DTP_HP::SPECIFIC_ACTOR.each do |i|
  235.         next unless $game_party.battle_members.include?($game_actors[i])
  236.         @hp_bar.visible = true
  237.       end
  238.     end
  239.     if DTP_HP::ONLY_ON_TARGET
  240.       return unless SceneManager.scene.is_a?(Scene_Battle)
  241.       return unless SceneManager.scene.enemy_window
  242.       @hp_bar.visible = SceneManager.scene.target_window_index == @battler.index
  243.       @hp_bar.visible = false if !SceneManager.scene.enemy_window.active
  244.     end
  245.     @text_display.visible = false if !@hp_bar.visible
  246.     @text_display.visible = true if @hp_bar.visible
  247.     @mp_bar.visible = @hp_bar.visible if DTP_HP::USE_MP_BAR
  248.   end
  249.   def dispose
  250.     @hp_bar.dispose if @hp_bar
  251.     @mp_bar.dispose if @mp_bar
  252.     @text_display.dispose if @text_display
  253.     hpbar_dispose
  254.   end
  255. end
  256.  
  257. class Scene_Battle
  258.   attr_reader  :enemy_window
  259.   def target_window_index
  260.     begin
  261.     @enemy_window.enemy.index
  262.     rescue
  263.       return -1
  264.     end
  265.   end
  266. end
  267.  
  268. class Game_Party
  269.   alias hp_bar_init initialize
  270.   attr_accessor  :monster_scans
  271.   def initialize
  272.     hp_bar_init
  273.     @monster_scans = []
  274.   end
  275. end
RAW Paste Data