Advertisement
khanhdu

Hunger_HUD

Aug 10th, 2018
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.84 KB | None | 0 0
  1. #===============================================================================
  2. # ** Hunger_HUD
  3. #===============================================================================
  4.  
  5. class Hunger_HUD < Window_Base
  6.  
  7.   #====================================================================#
  8.   #                    BEGIN CONFIGURATION                             #
  9.   #====================================================================#
  10.  
  11.   # Define the colors used for each of the bars.
  12.   HUNGER_EMPTY = Color.new(255, 0, 0)
  13.   HUNGER_FULL = Color.new(0, 255, 0)
  14.  
  15.   THIRST_EMPTY = Color.new(96, 96, 96)
  16.   THIRST_FULL = Color.new(128, 128, 255)
  17.  
  18.   BACKGROUND_COLOR = Color.new(0, 0, 0)
  19.  
  20.   # Define the type of bar used. (0 = Gradient, 1 = Transitional)
  21.   # It would take longer to explain the differences than to just try each out.
  22.   # There's only two as of the moment.
  23.   BAR_STYLE = 1
  24.  
  25.   # Define the width and height, in pixels, of the bars.
  26.   BAR_WIDTH = 128
  27.   BAR_HEIGHT = 8
  28.  
  29.   # Define switch for active/visibility control within game.
  30.   ONOFF_SWITCH = 10
  31.  
  32.   #====================================================================#
  33.   #                     END CONFIGURATION                              #
  34.   #====================================================================#
  35.  
  36.   def initialize(y = -12)
  37.     super(0, y, 640, 96)
  38.     # Set the windowskin's opacity to 0.
  39.     self.windowskin = nil
  40.     @colors1 = [HUNGER_EMPTY, HUNGER_FULL, BACKGROUND_COLOR]
  41.     @colors2 = [THIRST_EMPTY, THIRST_FULL, BACKGROUND_COLOR]
  42.     @actors = $game_party.actors
  43.     @stats = stats
  44.     refresh
  45.   end
  46.  
  47.   def refresh
  48.     # Dispose the contents of the HUD.
  49.     if self.contents != nil
  50.       self.contents.dispose
  51.       self.contents = nil
  52.     end
  53.     # Adjust width and location of window.
  54.     self.width = @actors.size * (BAR_WIDTH + 48)
  55.     self.x = (640 - self.width) / 25
  56.     self.contents = Bitmap.new(self.width, self.height)
  57.     # Iterate actors.
  58.     @actors.each_index {|i|
  59.       actor = @actors[i]
  60.       # Calculate locations for each actor's bars.
  61.       x = i * (BAR_WIDTH + 48)
  62.       # Draw actor's name.
  63.       self.contents.font.size = 16
  64.       self.contents.draw_text(x, 0, BAR_WIDTH, 16, actor.name)
  65.       # Draw hunger bars.
  66.       w, h, rate, max = BAR_WIDTH, BAR_HEIGHT, @stats[i][0], @stats[i][1]
  67.       self.contents.draw_bar(x, 16, w, h, rate, max, BAR_STYLE, @colors1)
  68.       # Draw thirst bars.
  69.       rate, max, height = @stats[i][2], @stats[i][3], 16+BAR_HEIGHT+4
  70.       self.contents.draw_bar(x, height, w, h, rate, max, BAR_STYLE, @colors2)
  71.     }
  72.   end
  73.  
  74.   def stats
  75.     return @actors.collect {|a| [a.hunger, a.max_hunger, a.thirst, a.max_thirst]}
  76.   end
  77.  
  78.   def update
  79.     self.visible = $game_switches[ONOFF_SWITCH]
  80.     if self.visible
  81.       if (@stats != stats) || (@actors != $game_party.actors)
  82.         @stats, @actors = stats, $game_party.actors
  83.         refresh
  84.       end
  85.     end
  86.   end
  87. end
  88.  
  89. #===============================================================================
  90. # ** Gradient_Bar
  91. #===============================================================================
  92.  
  93. class Bitmap
  94.  
  95.   def draw_bar(x, y, w, h, rate, max, style, colors = nil)
  96.     # Set required instance variables.
  97.     @bar_rect = Rect.new(x, y, w, h)
  98.     @rate, @max, @style, @colors = rate, max, style, colors
  99.     # Define default colors if not defined. (RED <--> GREEN)
  100.     if @colors == nil
  101.       @colors = [Color.new(255, 0, 0), Color.new(0, 255, 0), Color.new(0, 0, 0)]
  102.     end
  103.     # Draw the background color.
  104.     self.fill_rect(@bar_rect, @colors[2])
  105.     # Branch by what style is being used.
  106.     case @style
  107.     when 0 then gradient
  108.     when 1 then transition
  109.     end
  110.   end
  111. #-------------------------------------------------------------------------------
  112.   def gradient
  113.     # Get the bar from the cache.
  114.     key = [@style, @bar_rect.width-2, @bar_rect.height-2, @colors[0], @colors[1]]
  115.     bar = RPG::Cache.gradient_bar(*key)
  116.     # Draw the gradient bar using rectangular transfer.
  117.     rect = Rect.new(0, 0, fill_width, @bar_rect.height)
  118.     self.blt(@bar_rect.x+1, @bar_rect.y+1, bar, rect)
  119.   end
  120. #-------------------------------------------------------------------------------
  121.   def transition
  122.     # Returns the color for current rate.
  123.     c1 = [@colors[0].red, @colors[0].green, @colors[0].blue, @colors[0].alpha]
  124.     c2 = [@colors[1].red, @colors[1].green, @colors[1].blue, @colors[1].alpha]
  125.     rgba, rate = [],  1 - (@rate.to_f / @max)
  126.     c1.each_index {|i| rgba[i] = c2[i] - ((c2[i] - c1[i]) * rate) }
  127.     # Set the bars fill rate and color depending on value.
  128.     rect = Rect.new(@bar_rect.x+1, @bar_rect.y+1, fill_width, @bar_rect.height-2)
  129.     self.fill_rect(rect, Color.new(*rgba))
  130.   end
  131. #-------------------------------------------------------------------------------
  132.   def fill_width
  133.     # Calculate the difference, in percentage, of the min and max rates.
  134.     return ((@rate / @max.to_f) * (@bar_rect.width - 2)).round
  135.   end
  136. #-------------------------------------------------------------------------------
  137. end
  138.  
  139. #===============================================================================
  140. # ** Scene_Map
  141. #===============================================================================
  142.  
  143. class Scene_Map
  144.  
  145.   alias zer0_hunger_hud_main main
  146.   def main
  147.     # Add the bars to Scene_Map.
  148.     @hunger_hud = Hunger_HUD.new
  149.     zer0_hunger_hud_main
  150.     @hunger_hud.dispose unless @hunger_hud.disposed? || @hunger_hud == nil
  151.   end
  152.  
  153.   alias zer0_hunger_hud_upd update
  154.   def update
  155.     # Update the bars as needed.
  156.     @hunger_hud.update
  157.     zer0_hunger_hud_upd
  158.   end
  159. end
  160.  
  161. #===============================================================================
  162. # ** RPG::Cache
  163. #===============================================================================
  164.  
  165. module RPG::Cache
  166.  
  167.   def self.gradient_bar(style, width, height, color1, color2)
  168.     # Create a unique key to call the bar back with.
  169.     path = [style, width, height, color1, color2]
  170.     # Check if cache already has bitmap defined, if not create it now.
  171.     if !@cache.include?(path) || @cache[path].disposed?
  172.       bitmap, rates = Bitmap.new(width, height), []
  173.       # Iterate through each pixel horizontally, setting a gradient color.
  174.       c1 = [color1.red, color1.green, color1.blue, color1.alpha]
  175.       c2 = [color2.red, color2.green, color2.blue, color2.alpha]
  176.       # Draw the bar, having in transition from the first color to the second.
  177.       c1.each_index {|i| rates[i] = (c1[i] - c2[i]).to_f / width }
  178.       (0...width).each {|i|
  179.         values = [0, 1, 2, 3].collect {|j| c1[j] - (i * rates[j]) }
  180.         # Set the color incrementally. This will be used later.
  181.         bitmap.fill_rect(i, 0, 1, height, Color.new(*values))
  182.       }
  183.       @cache[path] = bitmap
  184.     end
  185.     # Return the created bitmap.
  186.     return @cache[path]
  187.   end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement