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

@ -1,5 +1,6 @@
[gd_resource type="Resource" script_class="Item" load_steps=2 format=3 uid="uid://16xqvcrmuga6"]
[gd_resource type="Resource" script_class="Item" load_steps=3 format=3 uid="uid://16xqvcrmuga6"]
[ext_resource type="Texture2D" uid="uid://daojdy0tpvsoh" path="res://sprites/items_icons/energy.png" id="1_1ocvl"]
[ext_resource type="Script" path="res://scripts/item.gd" id="1_wdaj0"]
[resource]
@ -9,4 +10,5 @@ description = ""
min_price = 1.0
max_price = 20.0
weight = 1.0
icon = ExtResource("1_1ocvl")
type = 0

View file

@ -1,6 +1,7 @@
[gd_resource type="Resource" script_class="Item" load_steps=2 format=3 uid="uid://ca8cpmd1aluut"]
[gd_resource type="Resource" script_class="Item" load_steps=3 format=3 uid="uid://ca8cpmd1aluut"]
[ext_resource type="Script" path="res://scripts/item.gd" id="1_gmqal"]
[ext_resource type="Texture2D" uid="uid://w3aft10s2hxd" path="res://sprites/items_icons/food.png" id="1_w33w6"]
[resource]
script = ExtResource("1_gmqal")
@ -9,4 +10,5 @@ description = ""
min_price = 10.0
max_price = 100.0
weight = 10.0
icon = ExtResource("1_w33w6")
type = 0

View file

@ -1,5 +1,6 @@
[gd_resource type="Resource" script_class="Item" load_steps=2 format=3 uid="uid://dlnl6w416qylo"]
[gd_resource type="Resource" script_class="Item" load_steps=3 format=3 uid="uid://dlnl6w416qylo"]
[ext_resource type="Texture2D" uid="uid://dlh6eyywjrsk4" path="res://sprites/items_icons/raw_materials.png" id="1_an4j4"]
[ext_resource type="Script" path="res://scripts/item.gd" id="1_l7wcw"]
[resource]
@ -9,4 +10,5 @@ description = ""
min_price = 10.0
max_price = 50.0
weight = 5.0
icon = ExtResource("1_an4j4")
type = 0

View file

@ -1,5 +1,6 @@
[gd_resource type="Resource" script_class="Item" load_steps=2 format=3 uid="uid://b4jy8uknltolk"]
[gd_resource type="Resource" script_class="Item" load_steps=3 format=3 uid="uid://b4jy8uknltolk"]
[ext_resource type="Texture2D" uid="uid://cova10yh1rlk1" path="res://sprites/items_icons/water.png" id="1_fv1u8"]
[ext_resource type="Script" path="res://scripts/item.gd" id="1_w7v0g"]
[resource]
@ -9,4 +10,5 @@ description = ""
min_price = 5.0
max_price = 30.0
weight = 10.0
icon = ExtResource("1_fv1u8")
type = 0

View file

@ -1,5 +1,6 @@
[gd_scene load_steps=4 format=3 uid="uid://bdwk0dqcx0xhp"]
[gd_scene load_steps=5 format=3 uid="uid://bdwk0dqcx0xhp"]
[ext_resource type="Script" path="res://scripts/menu/BaseMenu.gd" id="1_0wv8j"]
[ext_resource type="Texture2D" uid="uid://db4euprxhape0" path="res://sprites/9s.png" id="1_vb0tn"]
[ext_resource type="Script" path="res://scripts/menu/CloseMenuButton.gd" id="2_ld3o5"]
[ext_resource type="Script" path="res://scripts/menu/OpenMenuButton.gd" id="3_63o1s"]
@ -12,6 +13,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_0wv8j")
[node name="MainMenu" type="NinePatchRect" parent="."]
layout_mode = 0
@ -109,6 +111,50 @@ offset_bottom = 40.0
text = "X"
script = ExtResource("2_ld3o5")
[node name="SellList" type="ItemList" parent="TradingMenu"]
layout_mode = 0
offset_top = 64.0
offset_right = 320.0
offset_bottom = 512.0
[node name="BuyList" type="ItemList" parent="TradingMenu"]
offset_left = 320.0
offset_top = 64.0
offset_right = 640.0
offset_bottom = 512.0
[node name="SellInput" type="LineEdit" parent="TradingMenu"]
layout_mode = 0
offset_left = 1.0
offset_top = 512.0
offset_right = 160.0
offset_bottom = 576.0
placeholder_text = "Amount to buy"
alignment = 1
[node name="BuyInput" type="LineEdit" parent="TradingMenu"]
offset_left = 320.0
offset_top = 512.0
offset_right = 480.0
offset_bottom = 576.0
placeholder_text = "Amount to sell"
alignment = 1
[node name="BuyButton" type="Button" parent="TradingMenu"]
layout_mode = 0
offset_left = 160.0
offset_top = 512.0
offset_right = 320.0
offset_bottom = 576.0
text = "Buy selected item"
[node name="SellButton" type="Button" parent="TradingMenu"]
offset_left = 480.0
offset_top = 512.0
offset_right = 639.0
offset_bottom = 576.0
text = "Sell selected item"
[node name="QuestMenu" type="NinePatchRect" parent="."]
visible = false
layout_mode = 0

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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://daojdy0tpvsoh"
path="res://.godot/imported/energy.png-56852b83575bd48e69877e4e7f810483.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/items_icons/energy.png"
dest_files=["res://.godot/imported/energy.png-56852b83575bd48e69877e4e7f810483.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://w3aft10s2hxd"
path="res://.godot/imported/food.png-d786f9d11f7b590dd57cbb0e0eba166e.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/items_icons/food.png"
dest_files=["res://.godot/imported/food.png-d786f9d11f7b590dd57cbb0e0eba166e.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dlh6eyywjrsk4"
path="res://.godot/imported/raw_materials.png-17c645253a86197988c3802ae3935bdc.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/items_icons/raw_materials.png"
dest_files=["res://.godot/imported/raw_materials.png-17c645253a86197988c3802ae3935bdc.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cova10yh1rlk1"
path="res://.godot/imported/water.png-c8b3df05b2aab92358df5a49fb5a60ba.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/items_icons/water.png"
dest_files=["res://.godot/imported/water.png-c8b3df05b2aab92358df5a49fb5a60ba.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1