Added trading system and cargo saving

This commit is contained in:
gotfishmakesticks 2023-11-13 17:13:36 +03:00
commit 30e53f858f
19 changed files with 301 additions and 12 deletions

View file

@ -34,6 +34,7 @@ func _ready():
ship.hull.hp = save['hp']
ship.hull.fuel = save['fuel']
ship.hull.ammunition = save['ammo']
ship.hull.cargo = save['cargo']
if save.has('primaryweapon'):
for node in ship.primary_slot.get_children():
node.queue_free()

View file

@ -27,7 +27,8 @@ static func profile_save(scene : Node) -> void:
"ammo" : scene.ship.hull.ammunition,
"hp" : scene.ship.hull.hp,
"fuel" : scene.ship.hull.fuel,
"money" : scene.ship.money
"money" : scene.ship.money,
"cargo" : scene.ship.hull.cargo
}
if scene.ship.primary_slot.get_child_count() > 0:
profile.profile_meta['game']['primaryweapon'] = scene.ship.primary_slot.get_child(0).id

View file

@ -8,5 +8,4 @@ class_name Item
@export var max_price : float
@export var weight : float
@export var icon : Texture
@export var image : Texture
@export var type : Game.ITEM_TYPE

View file

@ -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"

View file

@ -13,11 +13,13 @@ func _on_body_entered(body):
if body is MainShip:
body.allow_shooting = false
menu_inst = menu.instantiate()
body.find_child("GUI").add_child(menu_inst)
menu_inst.modulate = get_parent().modulate
menu_inst.base = get_parent()
body.find_child("GUI").add_child(menu_inst)
body.minimap.visible = false
func _on_body_exited(body):
if body is MainShip:
body.allow_shooting = true
body.minimap.visible = true
menu_inst.queue_free()

View file

@ -44,12 +44,14 @@ func update_prices():
buy_prices = []
var price : float
for item in want_to_sell:
price = randi_range(item.min_price * 100, item.max_price * 100) / 100
if type != Game.BASE_TYPE.TRADING:
price = randi_range(item.min_price, item.max_price * 100) / 100.0
else:
price = randi_range((item.max_price + item.min_price) / 2 * 100, item.max_price * 100) / 100.0
sell_prices.append(price)
for item in want_to_buy:
if type != Game.BASE_TYPE.TRADING:
price = randi_range((item.max_price + item.min_price) / 2 * 100, item.max_price * 100) / 100
price = randi_range(item.min_price, item.max_price * 100) / 100.0
else:
price = randi_range(item.min_price * 100, (item.max_price + item.min_price) / 2 * 100) / 100
price = randi_range(item.min_price * 100, (item.max_price + item.min_price) / 2 * 100) / 100.0
buy_prices.append(price)
print(sell_prices, buy_prices)