Added trading system and cargo saving
This commit is contained in:
parent
50be4a0bb2
commit
30e53f858f
19 changed files with 301 additions and 12 deletions
|
|
@ -1,3 +1,97 @@
|
|||
extends Control
|
||||
|
||||
var base
|
||||
@onready var ship = get_tree().current_scene.ship
|
||||
@onready var sell_list = $TradingMenu/SellList
|
||||
@onready var buy_list = $TradingMenu/BuyList
|
||||
@onready var main_menu_header = $MainMenu/Header
|
||||
@onready var sell_input = $TradingMenu/SellInput
|
||||
@onready var buy_input = $TradingMenu/BuyInput
|
||||
@onready var sell_button = $TradingMenu/SellButton
|
||||
@onready var buy_button = $TradingMenu/BuyButton
|
||||
|
||||
var base_types = ["Power Supply", "Mining", "Food Production", "Trading", "Modules"]
|
||||
|
||||
func _ready():
|
||||
update_lists()
|
||||
main_menu_header.text = "{BaseType} Station".format({"BaseType" : base_types[base.type]})
|
||||
buy_button.button_up.connect(buy_item)
|
||||
sell_button.button_up.connect(sell_item)
|
||||
buy_input.text_changed.connect(buy_text_changed)
|
||||
sell_input.text_changed.connect(sell_text_changed)
|
||||
|
||||
func update_lists():
|
||||
buy_list.clear()
|
||||
sell_list.clear()
|
||||
for i in range(len(base.want_to_buy)):
|
||||
var format = {
|
||||
"name" : base.want_to_buy[i].name,
|
||||
"price" : base.buy_prices[i]
|
||||
}
|
||||
var buy_text = "{name} -> {price} MU".format(format)
|
||||
buy_list.add_item(buy_text, base.want_to_buy[i].icon)
|
||||
|
||||
for i in range(len(base.want_to_sell)):
|
||||
var format = {
|
||||
"name" : base.want_to_sell[i].name,
|
||||
"price" : base.sell_prices[i]
|
||||
}
|
||||
var sell_text = "{name} <- {price} MU".format(format)
|
||||
sell_list.add_item(sell_text, base.want_to_sell[i].icon)
|
||||
|
||||
func buy_item():
|
||||
var amount = int(sell_input.text)
|
||||
amount = clamp(amount, 0, amount)
|
||||
if amount <= 0:
|
||||
sell_input.text = ""
|
||||
sell_input.placeholder_text = "Wrong amount!"
|
||||
return
|
||||
if len(sell_list.get_selected_items()) == 0:
|
||||
sell_input.text = ""
|
||||
sell_input.placeholder_text = "Item not chosen!"
|
||||
return
|
||||
var item_data = sell_list.get_item_text(sell_list.get_selected_items()[0]).split(" <- ")
|
||||
var price = float(item_data[1])
|
||||
var total_price = price * amount
|
||||
if total_price > ship.money:
|
||||
sell_input.text = ""
|
||||
sell_input.placeholder_text = "Insufficient funds!"
|
||||
return
|
||||
if ship.hull.cargo.has(item_data[0]):
|
||||
ship.hull.cargo[item_data[0]] += amount
|
||||
else:
|
||||
ship.hull.cargo[item_data[0]] = amount
|
||||
ship.money -= total_price
|
||||
print(ship.hull.cargo)
|
||||
|
||||
func sell_item():
|
||||
if len(buy_list.get_selected_items()) == 0:
|
||||
buy_input.text = ""
|
||||
buy_input.placeholder_text = "Item not chosen!"
|
||||
return
|
||||
var item_data = buy_list.get_item_text(buy_list.get_selected_items()[0]).split(" -> ")
|
||||
if !ship.hull.cargo.has(item_data[0]):
|
||||
buy_input.text = ""
|
||||
buy_input.placeholder_text = "No such item in cargo!"
|
||||
return
|
||||
var price = float(item_data[1])
|
||||
var amount = int(buy_input.text)
|
||||
var total_price = price * amount
|
||||
amount = clamp(amount, 0, amount)
|
||||
if amount <= 0:
|
||||
buy_input.text = ""
|
||||
buy_input.placeholder_text = "Wrong amount!"
|
||||
return
|
||||
if amount > ship.hull.cargo[item_data[0]]:
|
||||
buy_input.text = ""
|
||||
buy_input.placeholder_text = "Can't sell more than have!"
|
||||
return
|
||||
ship.hull.cargo[item_data[0]] -= amount
|
||||
ship.money += total_price
|
||||
print(ship.hull.cargo)
|
||||
|
||||
func buy_text_changed(_new_text):
|
||||
buy_input.placeholder_text = "Amount to sell"
|
||||
|
||||
func sell_text_changed(_new_text):
|
||||
sell_input.placeholder_text = "Amount to buy"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue