TheoAllen

Image Based HUD

Aug 21st, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.05 KB | None | 0 0
  1. # =============================================================================
  2. # Image Based HUD
  3. # =============================================================================
  4. =begin
  5.  
  6.   Isian HUD namain "Gauge"
  7.   Back HUD namain "BackGauge"
  8.   Taruh di Graphics/system
  9.  
  10. =end
  11.  
  12.   Alter_Speed = 10
  13. # 1000 for instant change
  14.  
  15.   HideSwitch = 1
  16. # Switch buat nyembunyiin HUD (kalo ON)
  17.  
  18. # -----------------------------------------------------------------------------
  19. # Script calls
  20. # -----------------------------------------------------------------------------
  21. # buat nambah HUD
  22. # add_hud(x,y,id,max)
  23. # -----------------------------------------------------------------------------
  24.  
  25. # -----------------------------------------------------------------------------
  26. # buat remove HUD
  27. # remove_hud(id)
  28. # -----------------------------------------------------------------------------
  29.  
  30. # =============================================================================
  31. # Script
  32. # =============================================================================
  33. class HUD_Value < Sprite
  34.  
  35.   def initialize(data,viewport = nil)
  36.     @data = data
  37.     super(viewport)
  38.     self.bitmap = Cache.system("Gauge")
  39.     @ori_width = src_rect.width
  40.     @src_width = rate
  41.     update_visibility
  42.     update_placement
  43.     update_gauge
  44.   end
  45.  
  46.   def var
  47.     $game_variables[@data.id]
  48.   end
  49.  
  50.   def rate
  51.     var/@data.max.to_f
  52.   end
  53.  
  54.   def update
  55.     super
  56.     update_visibility
  57.     update_placement
  58.     update_gauge
  59.     update_width
  60.   end
  61.  
  62.   def update_visibility
  63.     self.visible = !$game_switches[HideSwitch]
  64.   end
  65.  
  66.   def update_placement
  67.     self.x = @data.x
  68.     self.y = @data.y
  69.   end
  70.  
  71.   def update_gauge
  72.     src_rect.width = @ori_width * @src_width
  73.   end
  74.  
  75.   def update_width
  76.     spd = Alter_Speed * 0.001
  77.     @src_width = [@src_width + spd,rate].min if @src_width < rate
  78.     @src_width = [@src_width - spd,rate].max if @src_width > rate
  79.   end
  80.  
  81. end
  82.  
  83. class HUD
  84.  
  85.   def initialize(data,viewport = nil)
  86.     @hud = HUD_Value.new(data,viewport)
  87.     @hud.z += 50
  88.     @back_hud = Sprite.new(viewport)
  89.     @back_hud.bitmap = Cache.system("BackGauge")
  90.   end
  91.  
  92.   def update
  93.     @hud.update
  94.     @back_hud.update
  95.   end
  96.  
  97.   def dispose
  98.     @hud.dispose
  99.     @back_hud.dispose
  100.   end
  101.  
  102. end
  103.  
  104. class ImgHUDs < Array
  105.  
  106.   def update
  107.     each {|hud| hud.update }
  108.   end
  109.  
  110.   def dispose
  111.     each {|hud| hud.dispose }
  112.   end
  113.  
  114. end
  115.  
  116. class DataHUD
  117.   attr_accessor :x
  118.   attr_accessor :y
  119.   attr_accessor :id
  120.   attr_accessor :max
  121.  
  122.   def initialize(x,y,id,max)
  123.     @x = x
  124.     @y = y
  125.     @id = id
  126.     @max = max
  127.   end
  128.  
  129. end
  130.  
  131. class Game_Temp
  132.   attr_accessor :refresh_hud
  133.  
  134.   alias theo_imghud_init initialize
  135.   def initialize
  136.     theo_imghud_init
  137.     @refresh_hud = false
  138.   end
  139.  
  140. end
  141.  
  142. class Game_System
  143.   attr_reader :huds
  144.  
  145.   alias theo_imghud_init initialize
  146.   def initialize
  147.     theo_imghud_init
  148.     @huds = {}
  149.   end
  150.  
  151.   def add_hud(x,y,id,max)
  152.     @huds[id] = DataHUD.new(x,y,id,max)
  153.   end
  154.  
  155.   def remove_hud(id)
  156.     @huds.delete(id)
  157.   end
  158.  
  159. end
  160.  
  161. class Game_Interpreter
  162.  
  163.   def add_hud(x,y,id,max)
  164.     $game_system.add_hud(x,y,id,max)
  165.     puts $game_system.huds.size
  166.     $game_temp.refresh_hud = true
  167.   end
  168.  
  169.   def remove_hud(id)
  170.     $game_system.remove_hud(id)
  171.     puts $game_system.huds.size
  172.     $game_temp.refresh_hud = true
  173.   end
  174.  
  175. end
  176.  
  177. class Scene_Map < Scene_Base
  178.  
  179.   alias theo_imghud_start start
  180.   def start
  181.     theo_imghud_start
  182.     create_imghud
  183.   end
  184.  
  185.   def create_imghud
  186.     @huds = ImgHUDs.new
  187.     $game_system.huds.values.each do |data|
  188.       @huds.push(HUD.new(data))
  189.     end
  190.   end
  191.  
  192.   alias theo_imghud_update update
  193.   def update
  194.     theo_imghud_update
  195.     @huds.update
  196.     if $game_temp.refresh_hud
  197.       dispose_hud
  198.       create_imghud
  199.       $game_temp.refresh_hud = false
  200.     end
  201.   end
  202.  
  203.   def dispose_hud
  204.     @huds.dispose
  205.   end
  206.  
  207.   alias theo_imghud_terminate terminate
  208.   def terminate
  209.     theo_imghud_terminate
  210.     dispose_hud
  211.   end
  212.  
  213. end
Advertisement
Add Comment
Please, Sign In to add comment