From 30e53f858fdbe25666439be7f191fe0612547c28 Mon Sep 17 00:00:00 2001 From: gotfishmakesticks <80163046+gotfishmakesticks@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:13:36 +0300 Subject: [PATCH] Added trading system and cargo saving --- items/energy_cell.tres | 4 +- items/food_supplies.tres | 4 +- items/raw_materials.tres | 4 +- items/water_barrel.tres | 4 +- scenes/menus/BaseMenu.tscn | 48 +++++++++- scripts/Space.gd | 1 + scripts/game.gd | 3 +- scripts/item.gd | 1 - scripts/menu/BaseMenu.gd | 94 +++++++++++++++++++ scripts/misc/BaseMenuOpen.gd | 4 +- scripts/objects/Base.gd | 10 +- sprites/items_icons/energy.png | Bin 0 -> 254 bytes sprites/items_icons/energy.png.import | 34 +++++++ sprites/items_icons/food.png | Bin 0 -> 260 bytes sprites/items_icons/food.png.import | 34 +++++++ sprites/items_icons/raw_materials.png | Bin 0 -> 311 bytes sprites/items_icons/raw_materials.png.import | 34 +++++++ sprites/items_icons/water.png | Bin 0 -> 243 bytes sprites/items_icons/water.png.import | 34 +++++++ 19 files changed, 301 insertions(+), 12 deletions(-) create mode 100644 sprites/items_icons/energy.png create mode 100644 sprites/items_icons/energy.png.import create mode 100644 sprites/items_icons/food.png create mode 100644 sprites/items_icons/food.png.import create mode 100644 sprites/items_icons/raw_materials.png create mode 100644 sprites/items_icons/raw_materials.png.import create mode 100644 sprites/items_icons/water.png create mode 100644 sprites/items_icons/water.png.import diff --git a/items/energy_cell.tres b/items/energy_cell.tres index 428c5d8..6aed219 100644 --- a/items/energy_cell.tres +++ b/items/energy_cell.tres @@ -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 diff --git a/items/food_supplies.tres b/items/food_supplies.tres index b4771a7..9c7631e 100644 --- a/items/food_supplies.tres +++ b/items/food_supplies.tres @@ -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 diff --git a/items/raw_materials.tres b/items/raw_materials.tres index deaf316..322d984 100644 --- a/items/raw_materials.tres +++ b/items/raw_materials.tres @@ -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 diff --git a/items/water_barrel.tres b/items/water_barrel.tres index bb340c0..75e3a59 100644 --- a/items/water_barrel.tres +++ b/items/water_barrel.tres @@ -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 diff --git a/scenes/menus/BaseMenu.tscn b/scenes/menus/BaseMenu.tscn index 621f878..e5ad366 100644 --- a/scenes/menus/BaseMenu.tscn +++ b/scenes/menus/BaseMenu.tscn @@ -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 diff --git a/scripts/Space.gd b/scripts/Space.gd index 9819c1e..4045ac1 100644 --- a/scripts/Space.gd +++ b/scripts/Space.gd @@ -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() diff --git a/scripts/game.gd b/scripts/game.gd index 40f2cc3..66b1cd6 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -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 diff --git a/scripts/item.gd b/scripts/item.gd index adb5c9f..16bf762 100644 --- a/scripts/item.gd +++ b/scripts/item.gd @@ -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 diff --git a/scripts/menu/BaseMenu.gd b/scripts/menu/BaseMenu.gd index 1599f9e..a408360 100644 --- a/scripts/menu/BaseMenu.gd +++ b/scripts/menu/BaseMenu.gd @@ -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" diff --git a/scripts/misc/BaseMenuOpen.gd b/scripts/misc/BaseMenuOpen.gd index 7e1d339..bc27c8c 100644 --- a/scripts/misc/BaseMenuOpen.gd +++ b/scripts/misc/BaseMenuOpen.gd @@ -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() diff --git a/scripts/objects/Base.gd b/scripts/objects/Base.gd index 459a79f..2edc858 100644 --- a/scripts/objects/Base.gd +++ b/scripts/objects/Base.gd @@ -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) diff --git a/sprites/items_icons/energy.png b/sprites/items_icons/energy.png new file mode 100644 index 0000000000000000000000000000000000000000..25f87114e84db39f285e7dcd70319a0838837459 GIT binary patch literal 254 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}J3L(+Ln2z= zP7ma2FyL{H{`fC^o_rHep`NdD>Vx0&vZq~JTWXxmc0p>(n?rXOmRd{6+%@=h_M2nF7<}X(0^Ex)1W~qqVcG1olo@!Xz?vzR&_SU4`Ob z8yG^Lem{|aNbY2E3tMs{i^0K*U#9$L4d6ZFH(B%C7Ur9`H2fXC4&0xh5wTfO^DX<8 zDds8zOkpfm*LI5bKW8uS4)6+id+QI-4-B5JelF{r5}E)z C)np<7 literal 0 HcmV?d00001 diff --git a/sprites/items_icons/energy.png.import b/sprites/items_icons/energy.png.import new file mode 100644 index 0000000..4ff633f --- /dev/null +++ b/sprites/items_icons/energy.png.import @@ -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 diff --git a/sprites/items_icons/food.png b/sprites/items_icons/food.png new file mode 100644 index 0000000000000000000000000000000000000000..b1b8ea337898fdd1225ea4c6f2a04a92a26eacdf GIT binary patch literal 260 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}`#fD7Ln2z= zhMyK{P~eD>`u~6WOBds%0YY8u-!po4?@{4kaAw^6OOxlQlKP^=BaiM(bk+MF&XhI@ zh$r7?NtqDOwCM!n0uR%^9Lv)?Kc#&T&+jw6U7!}jG{aIS^)6G3g;wrM&4Bbq2kx(* z???*Px#@kvBMR9J=Om(dQxAP7ahP5=Lw*^>>UARyB=*?nB_asr4QT5HrE)bV8UR{)&a zk3@b~0IUHZqL6ts&eK;5q$v@Z)c_Exlqa2D5UHhBcR-y9xW-h|cLGQ5TnWs}P)n5)<`Ua|=@#+6OT z&$=_PdL6rO0f1pest|`|lId{;;;KM(pk^V=8QAd>4gq%Q5dhw*skgu~jV!OO9*ZXa z7tCz9%zvB%}vAJ{#Z85SH{C*pAP zZz{vgHQo%XR2b)Mo_~#5bP?mCoU2-U)-%~3C~3%ez4CUzsslH5Hg_$RpS0#>;QQB& r=NWDOXB}gaE3@qF(L303@&-eSQHb4)bS?{^!x=nX{an^LB{Ts5@H}4W literal 0 HcmV?d00001 diff --git a/sprites/items_icons/water.png.import b/sprites/items_icons/water.png.import new file mode 100644 index 0000000..43175fc --- /dev/null +++ b/sprites/items_icons/water.png.import @@ -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