cosmic/scripts/Base/Menu/buy_sell.gd
2024-05-22 19:09:36 +03:00

50 lines
1.3 KiB
GDScript

extends Node
@onready var base = get_parent().base
const ITEM_TEMPLATE = "BASE_FETCH_PRICE_ITEM"
## List of items that this base sells
var items_on_sell
## List of items that this base buys
var items_on_buy
## List of prices of items that this base sells
var sell_prices
## List of prices of items that this base buys
var buy_prices
var template = tr(ITEM_TEMPLATE)
func _ready():
get_tree().create_timer(0.05).timeout.connect(fetch_items)
## Fetches items and pricelists from base
func fetch_items():
items_on_sell = base.items_on_sell
items_on_buy = base.items_on_buy
sell_prices = base.sell_prices
buy_prices = base.buy_prices
## Returns buy/sell description of a selected item
func item_description(buy: bool, id: int) -> String:
assert(id >= 0)
var item = items_on_buy[id] if buy else items_on_sell[id]
var price = buy_prices[id] if buy else sell_prices[id]
var format = {
"item_img": item.icon.resource_path,
"item_name": tr(item.name),
"item_price": price
}
return template.format(format)
## Returns buy/sell list of all items
func get_buy_sell_list(buy: bool) -> String:
var total = ""
var list = buy_prices if buy else sell_prices
for item in range(len(list)):
total += item_description(buy, item) + "\n"
return total
func get_buy_sell_len(buy: bool) -> int:
var list = items_on_buy if buy else items_on_sell
return len(list)