Advertisement
LiTTleDRAgo

[RGSS/2/3] Hunger & Thirst - Hud

Sep 21st, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.92 KB | None | 0 0
  1. #==============================================================================
  2. # ** Hunger Hud
  3. #------------------------------------------------------------------------------
  4. #  Author: ForeverZer0
  5. #==============================================================================
  6. class Hunger_HUD < Window_Base
  7.  
  8.   #====================================================================#
  9.   #                    BEGIN CONFIGURATION                             #
  10.   #====================================================================#
  11.  
  12.   # Define the colors used for each of the bars.
  13.   HUNGER_EMPTY = Color.new(255, 0, 0)
  14.   HUNGER_FULL = Color.new(0, 255, 0)
  15.  
  16.   THIRST_EMPTY = Color.new(96, 96, 96)
  17.   THIRST_FULL = Color.new(128, 128, 255)
  18.  
  19.   BACKGROUND_COLOR = Color.new(0, 0, 0)
  20.  
  21.   # Define the type of bar used. (0 = Gradient, 1 = Transitional)
  22.   # It would take longer to explain the differences than to just try each out.
  23.   # There's only two as of the moment.
  24.   BAR_STYLE = 0
  25.  
  26.   # Define the width and height, in pixels, of the bars.
  27.   BAR_WIDTH = 128
  28.   BAR_HEIGHT = 20
  29.  
  30.   # Define switch for active/visibility control within game.
  31.   ONOFF_SWITCH = 10
  32.  
  33.   #====================================================================#
  34.   #                     END CONFIGURATION                              #
  35.   #====================================================================#
  36.   #--------------------------------------------------------------------------
  37.   # ● New Method: initialize
  38.   #--------------------------------------------------------------------------
  39.   def initialize(y = -12)
  40.     super(0, y, Graphics.width, 96)
  41.     # Set the windowskin's opacity to 0.
  42.     @windowskin_org = self.windowskin
  43.     self.windowskin = nil
  44.     @colors1 = [HUNGER_EMPTY, HUNGER_FULL, BACKGROUND_COLOR]
  45.     @colors2 = [THIRST_EMPTY, THIRST_FULL, BACKGROUND_COLOR]
  46.     @actors = $game_party.members
  47.     @function_stats ||= lambda do
  48.       @actors.collect {|a| [a.hunger, a.max_hunger, a.thirst, a.max_thirst]}
  49.     end
  50.     @stats = @function_stats.call
  51.     refresh
  52.   end
  53.   #--------------------------------------------------------------------------
  54.   # ● New Method: refresh
  55.   #--------------------------------------------------------------------------
  56.   def refresh
  57.     # Dispose the contents of the HUD.
  58.     if self.contents != nil
  59.       self.contents.dispose
  60.       self.contents = nil
  61.     end
  62.     # Adjust width and location of window.
  63.     self.height = @actors.size * (BAR_HEIGHT + 40)
  64.     self.x = (Graphics.width - self.width) / 2
  65.     self.contents = Bitmap.new(self.width, self.height)
  66.     # Iterate actors.
  67.     @actors.each_index do |i|
  68.       actor = @actors[i]
  69.       # Calculate locations for each actor's bars.
  70.       x = 0
  71.       y = i * (BAR_HEIGHT + 30)
  72.       # Draw actor's name.
  73.       self.contents.font.size = 16
  74.       if self.respond_to?(:draw_actor_name)
  75.         begin
  76.           self.windowskin = @windowskin_org
  77.           if self.method(:draw_actor_name).parameters.size == 4
  78.             draw_actor_name(actor, x, y, BAR_WIDTH)
  79.             draw_name = true
  80.           elsif self.method(:draw_actor_name).parameters.size == 3
  81.             draw_actor_name(actor, x, y)
  82.             draw_name = true
  83.           end
  84.           self.windowskin = nil
  85.         rescue
  86.           draw_name = false
  87.         end
  88.       end
  89.       self.contents.draw_text(x, y, BAR_WIDTH, 16, actor.name) unless draw_name
  90.       # Draw hunger bars.
  91.       w, h, rate, max = BAR_WIDTH, 8, @stats[i][0], @stats[i][1]
  92.       self.contents.draw_bar_hunger(x, 16+y, w, h, rate, max, BAR_STYLE, @colors1)
  93.       # Draw thirst bars.
  94.       rate, max, height = @stats[i][2], @stats[i][3], 16+8+4
  95.       self.contents.draw_bar_hunger(x, height+y, w, h, rate, max, BAR_STYLE, @colors2)
  96.     end
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # ● New Method: update
  100.   #--------------------------------------------------------------------------
  101.   def update
  102.     self.visible = $game_switches[ONOFF_SWITCH]
  103.     if self.visible
  104.       if (@stats != @function_stats.call) || (@actors != $game_party.members)
  105.         @stats, @actors = @function_stats.call, $game_party.members
  106.         refresh
  107.       end
  108.     end
  109.   end
  110. end
  111.  
  112. #==============================================================================
  113. # ** Bitmap
  114. #------------------------------------------------------------------------------
  115. #  
  116. #==============================================================================
  117. class Bitmap
  118.   #--------------------------------------------------------------------------
  119.   # ● New Method: draw_bar_hunger
  120.   #--------------------------------------------------------------------------
  121.   def draw_bar_hunger(x, y, w, h, rate, max, style, colors = nil)
  122.     # Set required instance variables.
  123.     @bar_rect = Rect.new(x, y, w, h)
  124.     @rate, @max, @style, @colors = rate, max, style, colors
  125.     @fill_width_bar_hunger ||= lambda do
  126.       ((@rate / @max.to_f) * (@bar_rect.width - 2)).round
  127.     end
  128.     # Define default colors if not defined. (RED <--> GREEN)
  129.     @colors ||= [Color.new(255, 0, 0), Color.new(0, 255, 0), Color.new(0, 0, 0)]
  130.     # Draw the background color.
  131.     self.fill_rect(@bar_rect, @colors[2])
  132.     # Branch by what style is being used.
  133.     case @style
  134.     when 0 then gradient_bar_hunger
  135.     when 1 then transition_bar_hunger
  136.     end
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   # ● New Method: gradient_bar_hunger
  140.   #--------------------------------------------------------------------------
  141.   def gradient_bar_hunger
  142.     # Get the bar from the cache.
  143.     key = [@style, @bar_rect.width-2, @bar_rect.height-2, @colors[0], @colors[1]]
  144.     bar = LiTTleDRAgo.cache.gradient_bar(*key)
  145.     # Draw the gradient bar using rectangular transfer.
  146.     rect = Rect.new(0, 0, @fill_width_bar_hunger.call, @bar_rect.height)
  147.     self.blt(@bar_rect.x+1, @bar_rect.y+1, bar, rect)
  148.   end
  149.   #--------------------------------------------------------------------------
  150.   # ● New Method: transition_bar_hunger
  151.   #--------------------------------------------------------------------------
  152.   def transition_bar_hunger
  153.     # Returns the color for current rate.
  154.     c1 = [@colors[0].red, @colors[0].green, @colors[0].blue, @colors[0].alpha]
  155.     c2 = [@colors[1].red, @colors[1].green, @colors[1].blue, @colors[1].alpha]
  156.     rgba, rate = [],  1 - (@rate.to_f / @max)
  157.     c1.each_index {|i| rgba[i] = c2[i] - ((c2[i] - c1[i]) * rate) }
  158.     # Set the bars fill rate and color depending on value.
  159.     rect = Rect.new(@bar_rect.x+1, @bar_rect.y+1,
  160.                     @fill_width_bar_hunger.call, @bar_rect.height-2)
  161.     self.fill_rect(rect, Color.new(*rgba))
  162.   end
  163. end
  164.  
  165. #==============================================================================
  166. # ** Scene_Map
  167. #------------------------------------------------------------------------------
  168. #  This class performs the map screen processing.
  169. #==============================================================================
  170.  
  171. class Scene_Map
  172.   #--------------------------------------------------------------------------
  173.   # ● Alias Listing
  174.   #--------------------------------------------------------------------------
  175.   unless method_defined?(:zer0_hunger_hud_main)
  176.     alias_method :zer0_hunger_hud_main, :main
  177.     alias_method :zer0_hunger_hud_upd, :update
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # ● Aliased Method: main
  181.   #--------------------------------------------------------------------------
  182.   def main
  183.     # Add the bars to Scene_Map.
  184.     @hunger_hud = Hunger_HUD.new
  185.     @hunger_hud.visible = $game_switches[Hunger_HUD::ONOFF_SWITCH]
  186.     zer0_hunger_hud_main
  187.     @hunger_hud.dispose unless @hunger_hud == nil || @hunger_hud.disposed?
  188.   end
  189.   #--------------------------------------------------------------------------
  190.   # ● Aliased Method: update
  191.   #--------------------------------------------------------------------------
  192.   def update
  193.     # Update the bars as needed.
  194.     @hunger_hud.update
  195.     zer0_hunger_hud_upd
  196.   end
  197. end
  198. #==============================================================================
  199. # ** Cache
  200. #------------------------------------------------------------------------------
  201. #  This module loads graphics, creates bitmap objects, and retains them.
  202. # To speed up load times and conserve memory, this module holds the
  203. # created bitmap object in the internal hash, allowing the program to
  204. # return preexisting objects when the same bitmap is requested again.
  205. #==============================================================================
  206. ModCache = defined?(Window_ActorCommand) ? Cache : RPG::Cache
  207. module ModCache
  208.   #--------------------------------------------------------------------------
  209.   # ● New Method: self.gradient_bar
  210.   #--------------------------------------------------------------------------
  211.   def self.gradient_bar(style, width, height, color1, color2)
  212.     # Create a unique key to call the bar back with.
  213.     path = [style, width, height, color1, color2]
  214.     @cache ||= {}
  215.     # Check if cache already has bitmap defined, if not create it now.
  216.     if !@cache.include?(path) || @cache[path].disposed?
  217.       bitmap, rates = Bitmap.new(width, height), []
  218.       # Iterate through each pixel horizontally, setting a gradient color.
  219.       c1 = [color1.red, color1.green, color1.blue, color1.alpha]
  220.       c2 = [color2.red, color2.green, color2.blue, color2.alpha]
  221.       # Draw the bar, having in transition from the first color to the second.
  222.       c1.each_index {|i| rates[i] = (c1[i] - c2[i]).to_f / width }
  223.       width.times do |i|
  224.         values = [0, 1, 2, 3].collect {|j| c1[j] - (i * rates[j]) }
  225.         # Set the color incrementally. This will be used later.
  226.         bitmap.fill_rect(i, 0, 1, height, Color.new(*values))
  227.       end
  228.       @cache[path] = bitmap
  229.     end
  230.     # Return the created bitmap.
  231.     return @cache[path]
  232.   end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement