Advertisement
Guest User

V.M. Enemy HP Bars + critical hp level

a guest
Dec 26th, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.75 KB | None | 0 0
  1.     #--# Basic Enemy HP Bars v 2.2.01
  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 left:
  13.     #    on the thread for the script
  14.     #    given by email: sumptuaryspade@live.ca
  15.     #    or by facebook: http://www.facebook.com/DaimoniousTailsGames
  16.     #
  17.     #- Free to use in any project with credit given, though donations are always
  18.     #   welcome via paypal (same as email)
  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.      
  28.       #The width of the hp bar
  29.       BAR_WIDTH = 66
  30.       #The height of the hp bar
  31.       BAR_HEIGHT = 5
  32.       #The width of the border around the hp bar
  33.       BORDER_WIDTH = 1
  34.       #The height of the border around the hp bar
  35.       BORDER_HEIGHT = 1
  36.       #Offset the hp bar along the x-axis(left,right)
  37.       BAR_OFFSET_X = 0
  38.       #Offset the hp bar along the y-axis(up,down)
  39.       BAR_OFFSET_Y = 0
  40.      
  41.       #Color for the back of the hp bar
  42.       COLOR_BAR_BACK = Color.new(0,0,0,200)
  43.    
  44.       #Outside border color
  45.       COLOR_BORDER_1 = Color.new(0,0,0,185)
  46.       #Inside border color
  47.       COLOR_BORDER_2 = Color.new(255,255,255,185)
  48.      
  49.       #Set critical hp (bar changes color at said hp %)
  50.       #Set between 0-1, set to 0 to disable critical hp display)
  51.       CRITICAL_HP = 0.5
  52.       #Colors for normal hp (gradient, left-right)
  53.       COLOR_BAR_1 = Color.new(255,0,0)
  54.       COLOR_BAR_2 = Color.new(200,100,100)
  55.       #Colors for critical hp (gradient, left-right)
  56.       COLOR_BAR_3 = Color.new(50,205,50)
  57.       COLOR_BAR_4 = Color.new(188,238,104)
  58.  
  59.       #Whether to display text or not
  60.       USE_TEXT = false
  61.       #Text to be displayed, chp = current hp, mhp = max hp, php = percentage hp
  62.       #Examples: "php%" or "chp/mhp" or "chp - php%"
  63.       TEXT_DISPLAY = "chp"
  64.       #Offset for the text along the x-axis(left,right)
  65.       TEXT_OFFSET_X = 5
  66.       #Offset for the text along the y-axis(up,down)
  67.       TEXT_OFFSET_Y = -24
  68.       #Size of the displayed text
  69.       TEXT_SIZE = Font.default_size
  70.       #Font of the displayed text
  71.       TEXT_FONT = Font.default_name
  72.      
  73.       #Show bars only when specific actor in party. Array format. Example: [8,7]
  74.       #Set to [] to not use actor only
  75.       SPECIFIC_ACTOR = []
  76.       #Show enemy hp bar only if certain state is applied (like a scan state)
  77.       #Set to 0 to not use state only
  78.       SCAN_STATE = 0
  79.     end
  80.     #Customization ends here
  81.      
  82.     class Sprite_Battler
  83.       alias hpbar_update update
  84.       alias hpbar_dispose dispose
  85.       def update
  86.         hpbar_update
  87.         return unless @battler.is_a?(Game_Enemy)
  88.         if @battler
  89.           update_hp_bar
  90.         end
  91.       end
  92.       def update_hp_bar
  93.         if @hp_bar.nil?
  94.           @hp_bar = Sprite_Base.new(self.viewport)
  95.           width = DTP_HP::BAR_WIDTH + DTP_HP::BORDER_WIDTH * 4
  96.           height = DTP_HP::BAR_HEIGHT + DTP_HP::BORDER_HEIGHT * 4
  97.           @hp_bar.bitmap = Bitmap.new(width,height)
  98.           @hp_bar.x = self.x - @hp_bar.width / 2 + DTP_HP::BAR_OFFSET_X
  99.           @hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y - self.bitmap.height - @hp_bar.height
  100.           @hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y unless DTP_HP::ABOVE_MONSTER
  101.           @hp_bar.x = 0 if @hp_bar.x < 0
  102.           @hp_bar.y = 0 if @hp_bar.y < 0
  103.           @hp_bar.z = 98
  104.         end
  105.         if @text_display.nil?
  106.           @text_display = Sprite_Base.new(self.viewport)
  107.           @text_display.bitmap = Bitmap.new(100,DTP_HP::TEXT_SIZE)
  108.           @text_display.bitmap.font.size = DTP_HP::TEXT_SIZE
  109.           @text_display.bitmap.font.name = DTP_HP::TEXT_FONT
  110.           @text_display.x = @hp_bar.x + DTP_HP::TEXT_OFFSET_X
  111.           @text_display.y = @hp_bar.y + DTP_HP::TEXT_OFFSET_Y
  112.           @text_display.z = 99
  113.         end
  114.         determine_visible
  115.         return unless @hp_bar.visible
  116.         @hp_bar.bitmap.clear
  117.         width = DTP_HP::BAR_WIDTH
  118.         height = DTP_HP::BAR_HEIGHT
  119.         bwidth = DTP_HP::BORDER_WIDTH
  120.         bheight = DTP_HP::BORDER_HEIGHT
  121.         btotal = (bwidth + bheight) * 2
  122.         rwidth = @hp_bar.bitmap.width
  123.         rheight = @hp_bar.bitmap.height
  124.         if !DTP_HP::CUSTOM_BAR
  125.           @hp_bar.bitmap.fill_rect(0,0,rwidth,rheight,DTP_HP::COLOR_BAR_BACK)
  126.           @hp_bar.bitmap.fill_rect(bwidth,bheight,rwidth-bwidth*2,rheight-bheight*2,DTP_HP::COLOR_BORDER_2)
  127.           @hp_bar.bitmap.fill_rect(bwidth*2,bheight*2,width,height,DTP_HP::COLOR_BORDER_1)
  128.         end
  129.         hp_width = @battler.hp_rate * width
  130.         if @battler.hp_rate < DTP_HP::CRITICAL_HP
  131.         @hp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,hp_width,height,DTP_HP::COLOR_BAR_1,DTP_HP::COLOR_BAR_2)
  132.         else
  133.         @hp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,hp_width,height,DTP_HP::COLOR_BAR_3,DTP_HP::COLOR_BAR_4)
  134.         end
  135.         if DTP_HP::CUSTOM_BAR
  136.           border_bitmap = Bitmap.new("Graphics/System/Custom_HP.png")
  137.           rect = Rect.new(0,0,border_bitmap.width,border_bitmap.height)
  138.           @hp_bar.bitmap.blt(0,0,border_bitmap,rect)
  139.         end
  140.         return unless DTP_HP::USE_TEXT
  141.         @text_display.bitmap.clear
  142.         text = DTP_HP::TEXT_DISPLAY.clone
  143.         text.gsub!(/chp/) {@battler.hp}
  144.         text.gsub!(/mhp/) {@battler.mhp}
  145.         text.gsub!(/php/) {(@battler.hp_rate * 100).to_i}
  146.         @text_display.bitmap.draw_text(0,0,100,@text_display.height,text)
  147.       end
  148.       def determine_visible
  149.         if !@battler.alive?
  150.           @hp_bar.visible = false
  151.           @text_display.visible = false
  152.           return if !@battler.alive?
  153.         end
  154.         @hp_bar.visible = true
  155.         if DTP_HP::SCAN_STATE != 0
  156.           @hp_bar.visible = false
  157.           @hp_bar.visible = true if @battler.state?(DTP_HP::SCAN_STATE)
  158.         end
  159.         if !DTP_HP::SPECIFIC_ACTOR.empty?
  160.           @hp_bar.visible = false unless DTP_HP::SCAN_STATE != 0
  161.           DTP_HP::SPECIFIC_ACTOR.each do |i|
  162.             next unless $game_party.battle_members.include?($game_actors[i])
  163.             @hp_bar.visible = true
  164.           end
  165.         end
  166.         @text_display.visible = false if !@hp_bar.visible
  167.         @text_display.visible = true if @hp_bar.visible
  168.       end
  169.       def dispose
  170.         @hp_bar.dispose if @hpbar
  171.         @text_display.dispose if @text_display
  172.         hpbar_dispose
  173.       end
  174.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement