Advertisement
Guest User

RPG Maker VX ACE Sample HUD

a guest
Dec 28th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.01 KB | None | 0 0
  1. class Scene_Map
  2.   alias start_custom_hud start
  3.   def start
  4.     start_custom_hud    
  5.     create_hud  
  6.   end
  7.  
  8.   def create_hud
  9.     @actor = $game_party.members.first
  10.     h = 104
  11.     @hud_viewport = Viewport.new(0, Graphics.height-h, Graphics.width, h)
  12.     @face = Sprite.new(@hud_viewport)
  13.     @face.bitmap = Cache.picture('Face')
  14.     @face.x, @face.y = 4, 4
  15.    
  16.     @hp_bar_back = Sprite.new(@hud_viewport)
  17.     @hp_bar_back.bitmap = Cache.picture('HpBar_Back')
  18.     @hp_bar_back.x = (Graphics.width-@hp_bar_back.bitmap.width)/2
  19.     @hp_bar_back.y = 65
  20.    
  21.     @hp_bar = Sprite.new(@hud_viewport)
  22.     @hp_bar.bitmap = Cache.picture('HpBar')
  23.     @hp_bar.x, @hp_bar.y = @hp_bar_back.x, @hp_bar_back.y
  24.     @hp_bar.z = @hp_bar_back.z + 1
  25.     @hp_bar.src_rect = Rect.new(0, 0,
  26.     (@hp_bar.bitmap.width*@actor.hp_rate).to_i, @hp_bar.bitmap.height)
  27.    
  28.     @hp_text = Sprite.new(@hud_viewport)
  29.     @hp_text.bitmap = Bitmap.new(@hp_bar.bitmap.width, @hp_bar.bitmap.height)
  30.     @hp_text.x, @hp_text.y, @hp_text.z = @hp_bar.x, @hp_bar.y, @hp_bar.z+1
  31.     draw_hp_value
  32.    
  33.     @mp_bar = Sprite.new(@hud_viewport)
  34.     @mp_bar.bitmap = Cache.picture('MpBar')
  35.     @mp_bar.x = @hp_bar.x + 1
  36.     @mp_bar.y = @hp_bar.y + 25
  37.     @mp_bar.src_rect = Rect.new(0, 0,
  38.     (@mp_bar.bitmap.width*@actor.mp_rate).to_i, @mp_bar.bitmap.height)
  39.   end
  40.  
  41.   def update_hp
  42.     current_length = @hp_bar.src_rect.width
  43.     target_length = (@hp_bar.bitmap.width * @actor.hp_rate).to_i
  44.     return if current_length == target_length
  45.     update_gauge(@hp_bar, current_length, target_length, 10)
  46.     draw_hp_value
  47.   end
  48.  
  49.   def draw_hp_value
  50.     bitmap = @hp_text.bitmap
  51.     bitmap.clear
  52.     rect = Rect.new(0, 0, bitmap.width-5, bitmap.height)
  53.     bitmap.font.outline = true
  54.     bitmap.font.bold = true
  55.     bitmap.font.size = 14
  56.     mhp_size = bitmap.text_size(@actor.mhp.to_s).width
  57.     bitmap.draw_text(rect, @actor.mhp.to_i.to_s ,2)
  58.     rect = Rect.new(0, 0, bitmap.width - 5 - mhp_size, bitmap.height)
  59.     bitmap.font.size = 20
  60.     bitmap.draw_text(rect, @actor.hp.to_i.to_s + "/", 2)
  61.   end
  62.  
  63.   def update_mp
  64.     current_length = @mp_bar.src_rect.width
  65.     target_length = (@mp_bar.bitmap.width * @actor.mp_rate).to_i
  66.     return if current_length == target_length
  67.     update_gauge(@mp_bar, current_length, target_length, 10)
  68.   end
  69.  
  70.   def update_gauge(spr, current_length, target_length, speed)
  71.     dw = target_length - current_length
  72.     dw = dw.abs > speed ? (dw>0? speed : -speed) : dw
  73.     spr.src_rect = Rect.new(0, 0, spr.src_rect.width+dw, spr.src_rect.height)
  74.   end
  75.  
  76.   def update_hud
  77.     update_hp
  78.     update_mp
  79.   end
  80.  
  81.   alias update_custom_hud update
  82.   def update
  83.     update_custom_hud
  84.     update_hud
  85.   end
  86.  
  87.   alias terminate_custom_hud terminate
  88.   def terminate
  89.     terminate_custom_hud
  90.     dispose_hud
  91.   end
  92.  
  93.   def dispose_hud
  94.     [@face, @hp_bar, @hp_bar_back, @mp_bar, @hp_text].each{|item|
  95.       item.bitmap.dispose if item.bitmap
  96.       item.dispose
  97.     }
  98.     @hud_viewport.dispose
  99.   end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement