//-----------------------------------------------------------------------------
// Window_ShopBuy
//
// The window for selecting an item to buy on the shop screen.
/**
* The window for selecting an item to buy on the shop screen.
*
* @class
* @extends Window_Selectable
*/
function Window_ShopBuy() {
this.initialize(...arguments);
}
Window_ShopBuy.prototype = Object.create(Window_Selectable.prototype);
Window_ShopBuy.prototype.constructor = Window_ShopBuy;
Window_ShopBuy.prototype.initialize = function(rect) {
Window_Selectable.prototype.initialize.call(this, rect);
this._money = 0;
};
/**
* Set up the window
*
* @param {Array} shopGoods - An array of shop goods
*/
Window_ShopBuy.prototype.setupGoods = function(shopGoods) {
this._shopGoods = shopGoods;
this.refresh();
this.select(0);
};
/**
* Get the maximum amount of items to display in the window
*
* @return {number} Amount of items to display in the window
*/
Window_ShopBuy.prototype.maxItems = function() {
return this._data ? this._data.length : 1;
};
/**
* Get the currently selected item
*
* @return {Object} The item currently selected
*/
Window_ShopBuy.prototype.item = function() {
return this.itemAt(this.index());
};
/**
* Get the item object at the given index
*
* @param {number} index - The index of the item
* @return {Object} The item at the given index
*/
Window_ShopBuy.prototype.itemAt = function(index) {
return this._data && index >= 0 ? this._data[index] : null;
};
/**
* Set the amount of money
*
* @param {number} money - The amount of money
*/
Window_ShopBuy.prototype.setMoney = function(money) {
this._money = money;
this.refresh();
};
/**
* Check if the currently selected item is enabled
*
* @return {boolean} True if enabled
*/
Window_ShopBuy.prototype.isCurrentItemEnabled = function() {
return this.isEnabled(this._data[this.index()]);
};
/**
* Get the price of the given item
*
* @param {Object} item - The item to check the price of
* @return {number} The price
*/
Window_ShopBuy.prototype.price = function(item) {
return this._price[this._data.indexOf(item)] || 0;
};
/**
* Check if the given item is enabled
*
* @param {Object} item - The item to check
* @return {boolean} True if enabled
*/
Window_ShopBuy.prototype.isEnabled = function(item) {
return (
item && this.price(item) <= this._money && !$gameParty.hasMaxItems(item)
);
};
Window_ShopBuy.prototype.refresh = function() {
this.makeItemList();
Window_Selectable.prototype.refresh.call(this);
};
/**
* Make the list of items in the shop
*/
Window_ShopBuy.prototype.makeItemList = function() {
this._data = [];
this._price = [];
for (const goods of this._shopGoods) {
const item = this.goodsToItem(goods);
if (item) {
this._data.push(item);
this._price.push(goods[2] === 0 ? item.price : goods[3]);
}
}
};
/**
* Convert a shopGoods array item to the actual item object
*
* @param {Array} goods - The shop goods array item
* @return {Object|null} The item object, or null if not a valid good
*/
Window_ShopBuy.prototype.goodsToItem = function(goods) {
switch (goods[0]) {
case 0:
return $dataItems[goods[1]];
case 1:
return $dataWeapons[goods[1]];
case 2:
return $dataArmors[goods[1]];
default:
return null;
}
};
/**
* Draws the item at the given index
*
* @param {number} index - The index to draw
*/
Window_ShopBuy.prototype.drawItem = function(index) {
const item = this.itemAt(index);
const price = this.price(item);
const rect = this.itemLineRect(index);
const priceWidth = this.priceWidth();
const priceX = rect.x + rect.width - priceWidth;
const nameWidth = rect.width - priceWidth;
this.changePaintOpacity(this.isEnabled(item));
this.drawItemName(item, rect.x, rect.y, nameWidth);
this.drawText(price, priceX, rect.y, priceWidth, "right");
this.changePaintOpacity(true);
};
/**
* Get the width of the price area
*
* @return {number} The width of the price area
*/
Window_ShopBuy.prototype.priceWidth = function() {
return 96;
};
/**
* Set the status window
*
* @param {Window_ShopStatus} statusWindow - The status window
*/
Window_ShopBuy.prototype.setStatusWindow = function(statusWindow) {
this._statusWindow = statusWindow;
this.callUpdateHelp();
};
/**
* Update helper windows
*/
Window_ShopBuy.prototype.updateHelp = function() {
this.setHelpWindowItem(this.item());
if (this._statusWindow) {
this._statusWindow.setItem(this.item());
}
};