OMG, thank you! I feel a little dumb because the answer was really simple, but it worked, and I'm happy that I can get that food collection/looting element to my game. Highly appreciated! I'll let y'all know if I encounter other difficulties.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
QuoteThere should be a part that specifies which switch controls the HUD being displayed. I believe the default is switch 10. To turn on the HUD you simply need to create an event that turns on the switch. I actually use the script in several of my projects, and I accomplish this by simply running an autorun event on the first map I use, and tell it to turn it on, then erase the event.
#===============================================================================
# ** Hunger_HUD
#===============================================================================
class Hunger_HUD < Window_Base
#====================================================================#
# BEGIN CONFIGURATION #
#====================================================================#
# Define the colors used for each of the bars.
HUNGER_EMPTY = Color.new(255, 0, 0)
HUNGER_FULL = Color.new(0, 255, 0)
THIRST_EMPTY = Color.new(96, 96, 96)
THIRST_FULL = Color.new(128, 128, 255)
BACKGROUND_COLOR = Color.new(0, 0, 0)
# Define the type of bar used. (0 = Gradient, 1 = Transitional)
# It would take longer to explain the differences than to just try each out.
# There's only two as of the moment.
BAR_STYLE = 1
# Define the width and height, in pixels, of the bars.
BAR_WIDTH = 128
BAR_HEIGHT = 8
# Define switch for active/visibility control within game.
ONOFF_SWITCH = 10
#====================================================================#
# END CONFIGURATION #
#====================================================================#
def initialize(y = -12)
super(0, y, 640, 96)
# Set the windowskin's opacity to 0.
self.windowskin = nil
@colors1 = [HUNGER_EMPTY, HUNGER_FULL, BACKGROUND_COLOR]
@colors2 = [THIRST_EMPTY, THIRST_FULL, BACKGROUND_COLOR]
@actors = $game_party.actors
@stats = stats
refresh
end
def refresh
# Dispose the contents of the HUD.
if self.contents != nil
self.contents.dispose
self.contents = nil
end
# Adjust width and location of window.
self.width = @actors.size * (BAR_WIDTH + 48)
self.x = (640 - self.width) / 2
self.contents = Bitmap.new(self.width, self.height)
# Iterate actors.
@actors.each_index {|i|
actor = @actors[i]
# Calculate locations for each actor's bars.
x = i * (BAR_WIDTH + 48)
# Draw actor's name.
self.contents.font.size = 16
self.contents.draw_text(x, 0, BAR_WIDTH, 16, actor.name)
# Draw hunger bars.
w, h, rate, max = BAR_WIDTH, BAR_HEIGHT, @stats[i][0], @stats[i][1]
self.contents.draw_bar(x, 16, w, h, rate, max, BAR_STYLE, @colors1)
# Draw thirst bars.
rate, max, height = @stats[i][2], @stats[i][3], 16+BAR_HEIGHT+4
self.contents.draw_bar(x, height, w, h, rate, max, BAR_STYLE, @colors2)
}
end
def stats
return @actors.collect {|a| [a.hunger, a.max_hunger, a.thirst, a.max_thirst]}
end
def update
self.visible = $game_switches[ONOFF_SWITCH]
if self.visible
if (@stats != stats) || (@actors != $game_party.actors)
@stats, @actors = stats, $game_party.actors
refresh
end
end
end
end
#===============================================================================
# ** Gradient_Bar
#===============================================================================
class Bitmap
def draw_bar(x, y, w, h, rate, max, style, colors = nil)
# Set required instance variables.
@bar_rect = Rect.new(x, y, w, h)
@rate, @max, @style, @colors = rate, max, style, colors
# Define default colors if not defined. (RED <--> GREEN)
if @colors == nil
@colors = [Color.new(255, 0, 0), Color.new(0, 255, 0), Color.new(0, 0, 0)]
end
# Draw the background color.
self.fill_rect(@bar_rect, @colors[2])
# Branch by what style is being used.
case @style
when 0 then gradient
when 1 then transition
end
end
#-------------------------------------------------------------------------------
def gradient
# Get the bar from the cache.
key = [@style, @bar_rect.width-2, @bar_rect.height-2, @colors[0], @colors[1]]
bar = RPG::Cache.gradient_bar(*key)
# Draw the gradient bar using rectangular transfer.
rect = Rect.new(0, 0, fill_width, @bar_rect.height)
self.blt(@bar_rect.x+1, @bar_rect.y+1, bar, rect)
end
#-------------------------------------------------------------------------------
def transition
# Returns the color for current rate.
c1 = [@colors[0].red, @colors[0].green, @colors[0].blue, @colors[0].alpha]
c2 = [@colors[1].red, @colors[1].green, @colors[1].blue, @colors[1].alpha]
rgba, rate = [], 1 - (@rate.to_f / @max)
c1.each_index {|i| rgba[i] = c2[i] - ((c2[i] - c1[i]) * rate) }
# Set the bars fill rate and color depending on value.
rect = Rect.new(@bar_rect.x+1, @bar_rect.y+1, fill_width, @bar_rect.height-2)
self.fill_rect(rect, Color.new(*rgba))
end
#-------------------------------------------------------------------------------
def fill_width
# Calculate the difference, in percentage, of the min and max rates.
return ((@rate / @max.to_f) * (@bar_rect.width - 2)).round
end
#-------------------------------------------------------------------------------
end
#===============================================================================
# ** Scene_Map
#===============================================================================
class Scene_Map
alias zer0_hunger_hud_main main
def main
# Add the bars to Scene_Map.
@hunger_hud = Hunger_HUD.new
zer0_hunger_hud_main
@hunger_hud.dispose unless @hunger_hud.disposed? || @hunger_hud == nil
end
alias zer0_hunger_hud_upd update
def update
# Update the bars as needed.
@hunger_hud.update
zer0_hunger_hud_upd
end
end
#===============================================================================
# ** RPG::Cache
#===============================================================================
module RPG::Cache
def self.gradient_bar(style, width, height, color1, color2)
# Create a unique key to call the bar back with.
path = [style, width, height, color1, color2]
# Check if cache already has bitmap defined, if not create it now.
if !@cache.include?(path) || @cache[path].disposed?
bitmap, rates = Bitmap.new(width, height), []
# Iterate through each pixel horizontally, setting a gradient color.
c1 = [color1.red, color1.green, color1.blue, color1.alpha]
c2 = [color2.red, color2.green, color2.blue, color2.alpha]
# Draw the bar, having in transition from the first color to the second.
c1.each_index {|i| rates[i] = (c1[i] - c2[i]).to_f / width }
(0...width).each {|i|
values = [0, 1, 2, 3].collect {|j| c1[j] - (i * rates[j]) }
# Set the color incrementally. This will be used later.
bitmap.fill_rect(i, 0, 1, height, Color.new(*values))
}
@cache[path] = bitmap
end
# Return the created bitmap.
return @cache[path]
end
end
class Scene_Map
attr_accessor :hunger_hud
end
# Define switch for active/visibility control within game.
ONOFF_SWITCH = 10