Home > RGSS Script Reference > Window_ShopBuy
Window_ShopBuy
Inherits from: Window_Selectable
Description: This window shows the items available for sale when you select "Buy" from the main shop command window.
class Window_ShopBuy < Window_Selectable
# ------------------------------------
def initialize(shop_goods)
super(0, 128, 368, 352)
@shop_goods = shop_goods
refresh
self.index = 0
end
# ------------------------------------
def item
return @data[self.index]
end
# ------------------------------------
def refresh
if self.contents != nil
self.contents.dispose
self.contents = nil
end
@data = []
for goods_item in @shop_goods
case goods_item[0]
when 0
@data.push($data_items[goods_item[1]])
when 1
@data.push($data_weapons[goods_item[1]])
when 2
@data.push($data_armors[goods_item[1]])
end
end
@item_max = @data.size
if @item_max > 0
self.contents = Bitmap.new(width - 32, row_max * 32)
for i in 0...@item_max
draw_item(i)
end
end
end
# ------------------------------------
def draw_item(index)
item = @data[index]
case item
when RPG::Item
number = $game_party.item_number(item.id)
when RPG::Weapon
number = $game_party.weapon_number(item.id)
when RPG::Armor
number = $game_party.armor_number(item.id)
end
if item.price <= $game_party.gold and number < 99
self.contents.font.color = normal_color
else
self.contents.font.color = disabled_color
end
x = 4
y = index * 32
rect = Rect.new(x, y, self.width - 32, 32)
self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
bitmap = RPG::Cache.icon(item.icon_name)
opacity = self.contents.font.color == normal_color ? 255 : 128
self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
self.contents.draw_text(x + 240, y, 88, 32, item.price.to_s, 2)
end
# ------------------------------------
def update_help
@help_window.set_text(self.item == nil ? "" : self.item.description)
end
end
|
Shop_Goods: An array of arrays representing the items for sale at the shop. Each sub-array in the main array has two entries. Index 0 is a number representing the type of item (0 = Item, 1 = Weapon, 2 = Armor) and index 1 is the ID number of that item, weapon, or armor as declared in the database.
Data: An array containing the items to be drawn. The contents of this array are determined in the refresh method.
Initialize
Arguments:
Shop_Goods: An array of arrays representing the items for sale at the shop. Each sub-array in the main array has two entries. Index 0 is a number representing the type of item (0 = Item, 1 = Weapon, 2 = Armor) and index 1 is the ID number of that item, weapon, or armor as declared in the database.
Local Variables: None
How it Works: This method initializes the window. First, the window's x coordinate is initialized to 0, the y coordinate is initialized to 128, the width to 368, and the height to 352. Then, the @shop_goods instance variables is set to the value passed to this method. The window contents are then drawn with the refresh method.
Item
Arguments: None
Local Variables: None
How it Works: This method returns the item in the @data array corresponding to the index of the currently selected item.
Refresh
Arguments: None
Local Variables: None
How it Works: This method draws the contents of the window. Since this window can be scrolled, the bitmap is disposed and recreated each time the window is refreshed. The @data array is set to be an empty array. For each element in the @shop_goods, the method checks the first index of the sub-array. If that value is 0, then the item with ID equal to the second index of the sub-array is pushed onto the @data array. If that value is 1, then the weapon with the ID equal to the second index of the sub-array is pushed onto @data. If that value is 2, then the armor with ID equal to the second index of the sub-array is pushed onto @data. The window's @item_max instance variable is set equal to the size of the @data array. If the item max is greater than 0, then the bitmap is created, and the draw_item method is called to draw the icon and item for each item in the @data array.
Draw_Item
Arguments:
Index: The index of the item to be drawn.
Local Variables:
Item: An alias for the item at the index of the @data array that is being drawn.
How it Works: This method draws an individual item within the shop buy window. The first thing to do is decide whether the item being drawn is a RPG::Item, RPG::Weapon, or RPG::Armor object. This allows the method to decide method to use to determine the number of the item the party has. If the item's price is less than or equal to the price of the item and the party has fewer than 99 of the item, then the item name is drawn in the normal color. Otherwise, it's drawn in the disabled color. Next, the method needs to determine the x and y coordinates at which to draw the item. The x coordinate is always 4, and the y coordinate is 32 times the index of the item. The rectangle is then constructed with those x and y coordinates, and a length of the window's width minus 32, and a height of 32. It then draws the item's icon, name, and price within the rectangle. Note that the draw_text methods for both the name and price have the optional alignment argument. For the name, the argument is 0, meaning left-alignment, and for the price, the argument is 2, meaning right-alignment.
Update_Help
Arguments: None
Local Variables: None
How it Works: This method updates the help window with the description of the currently selected item.
|
|