From f0bf7d76ec9e3c047bdab4dbb5caf517c523dfcd Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Tue, 5 Oct 2021 00:00:53 +0200 Subject: [PATCH] Implement refill per planet menu \o/ --- .../destination_menu/destination_menu.gd | 5 +- .../destination_menu/destination_menu.tscn | 30 +++++- .../fuel_station_container.gd | 53 +++++++++++ .../fuel_station_container.tscn | 91 +++++++++++++++++++ .../repair_option/repair_option.tscn | 44 ++++----- main_game/PurchaseHandler.gd | 1 + main_game/main_game.gd | 3 + project.godot | 6 ++ spaceship/spaceship.gd | 10 ++ 9 files changed, 214 insertions(+), 29 deletions(-) create mode 100644 destination/destination_menu/fuel_station_tab/fuel_station_container.gd create mode 100644 destination/destination_menu/fuel_station_tab/fuel_station_container.tscn diff --git a/destination/destination_menu/destination_menu.gd b/destination/destination_menu/destination_menu.gd index 495d5c9..fd3f552 100644 --- a/destination/destination_menu/destination_menu.gd +++ b/destination/destination_menu/destination_menu.gd @@ -129,11 +129,12 @@ func show(stats, position): $PopupMenu/ColorRect/GridContainer/EconomyValue.text = stats.economy $PopupMenu/ColorRect/FlavorTextContainer/FlavorText.text = stats.flavor_text update_current_balance_label() + $"PopupMenu/ColorRect/TabContainer/Fuel Station/OptionContainer".update_labels() if show_passenger_options(stats.passengers): - $PopupMenu/ColorRect/TabContainer.current_tab = 1 + $PopupMenu/ColorRect/TabContainer.current_tab = 2 price_multiplicator = calculate_price_multiplicator(stats) if show_repair_options(): - $PopupMenu/ColorRect/TabContainer.current_tab = 0 + $PopupMenu/ColorRect/TabContainer.current_tab = 1 target_position = position $PopupMenu.popup_centered() diff --git a/destination/destination_menu/destination_menu.tscn b/destination/destination_menu/destination_menu.tscn index 1c67750..448b719 100644 --- a/destination/destination_menu/destination_menu.tscn +++ b/destination/destination_menu/destination_menu.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://destination/destination_menu/destination_menu.gd" type="Script" id=1] [ext_resource path="res://assets/LeaveButtonHigherRes.png" type="Texture" id=2] [ext_resource path="res://passenger_pickup_option/passenger_pickup_option.tscn" type="PackedScene" id=3] [ext_resource path="res://destination/destination_menu/repair_option/repair_option.tscn" type="PackedScene" id=4] +[ext_resource path="res://destination/destination_menu/fuel_station_tab/fuel_station_container.tscn" type="PackedScene" id=5] [node name="PlanetMenu" type="CanvasLayer"] layer = 3 @@ -60,7 +61,25 @@ __meta__ = { "_edit_use_anchors_": false } +[node name="Fuel Station" type="MarginContainer" parent="PopupMenu/ColorRect/TabContainer"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 4.0 +margin_top = 38.0 +margin_right = -4.0 +margin_bottom = -4.0 +custom_constants/margin_right = 20 +custom_constants/margin_top = 20 +custom_constants/margin_left = 20 +custom_constants/margin_bottom = 20 +__meta__ = { +"_editor_description_": "" +} + +[node name="OptionContainer" parent="PopupMenu/ColorRect/TabContainer/Fuel Station" instance=ExtResource( 5 )] + [node name="Ship Repairs" type="MarginContainer" parent="PopupMenu/ColorRect/TabContainer"] +visible = false anchor_right = 1.0 anchor_bottom = 1.0 margin_left = 4.0 @@ -106,13 +125,14 @@ margin_bottom = 572.0 custom_constants/separation = 20 [node name="No passengers" type="Label" parent="PopupMenu/ColorRect/TabContainer/Potential Passengers/OptionContainer"] -margin_right = 577.0 -margin_bottom = 14.0 +margin_right = 691.0 +margin_bottom = 20.0 text = "There are no passengers waiting here at the moment." [node name="AlreadyCarryingPassenger" type="Label" parent="PopupMenu/ColorRect/TabContainer/Potential Passengers/OptionContainer"] -margin_right = 40.0 -margin_bottom = 14.0 +margin_top = 40.0 +margin_right = 691.0 +margin_bottom = 83.0 text = "You can only carry one passenger at a time. Please deliver your current passenger to their destination." diff --git a/destination/destination_menu/fuel_station_tab/fuel_station_container.gd b/destination/destination_menu/fuel_station_tab/fuel_station_container.gd new file mode 100644 index 0000000..b330885 --- /dev/null +++ b/destination/destination_menu/fuel_station_tab/fuel_station_container.gd @@ -0,0 +1,53 @@ +extends VBoxContainer + +signal buy_fuel(refill_amount, price) + +var spaceship: Spaceship +var purchase_handler: PurchaseHandler +var price_per_unit = 2 + +# This is for the next refill +var refill_amount = 0 +var refill_price = 0 + +# Called when the node enters the scene tree for the first time. +func _ready(): + purchase_handler = get_node_by_group("PurchaseHandler") + spaceship = get_node_by_group("Spaceship") + update_labels() + +func get_node_by_group(group_name): + var ph_results = get_tree().get_nodes_in_group(group_name) + if ph_results.size() > 0: + return ph_results[0] + return null + +func update_labels(): + $GridContainer/FuelLevelValue.text = "%d / %d" % [spaceship.current_fuel, spaceship.total_fuel_capacity] + $GridContainer/PricePerUnit.text = "%d Cu/l" % price_per_unit + + var total_money = spaceship.get_parent().capitalism_units + var full_refill_price = 0 + refill_amount = spaceship.total_fuel_capacity - spaceship.current_fuel + + if spaceship.tanks_are_full(): + $AlreadyFullLabel.visible = true + $NotEnoughCuLabel.visible = false + else: + full_refill_price = refill_amount * price_per_unit + $AlreadyFullLabel.visible = false + $NotEnoughCuLabel.visible = full_refill_price > total_money + + if full_refill_price > total_money: + refill_amount = floor(total_money / price_per_unit) + + refill_price = refill_amount * price_per_unit + + $GridContainer/FullRefillPrice.text = "%d Cu" % full_refill_price + $BuyFuelButton.text = "Buy %d liters of fuel now for only %d Cu!" % [refill_amount, refill_price] + +func _on_BuyFuelButton_pressed(): + if purchase_handler.purchase_item(refill_price, "%d liters of fuel" % refill_amount): + spaceship.refill_fuel(refill_amount) + emit_signal("purchase_successful") + update_labels() diff --git a/destination/destination_menu/fuel_station_tab/fuel_station_container.tscn b/destination/destination_menu/fuel_station_tab/fuel_station_container.tscn new file mode 100644 index 0000000..2961c54 --- /dev/null +++ b/destination/destination_menu/fuel_station_tab/fuel_station_container.tscn @@ -0,0 +1,91 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://destination/destination_menu/fuel_station_tab/fuel_station_container.gd" type="Script" id=1] + +[node name="FuelStationOptionContainer" type="VBoxContainer"] +margin_left = 20.0 +margin_top = 20.0 +margin_right = 711.0 +margin_bottom = 572.0 +custom_constants/separation = 20 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RefillInfoLabel" type="Label" parent="."] +margin_right = 691.0 +margin_bottom = 20.0 +text = "Here you can refill your spaceship's fuel tanks." +clip_text = true + +[node name="GridContainer" type="GridContainer" parent="."] +margin_top = 40.0 +margin_right = 691.0 +margin_bottom = 108.0 +columns = 2 + +[node name="LabelCurrentFuel" type="Label" parent="GridContainer"] +margin_right = 218.0 +margin_bottom = 20.0 +text = "Current fuel: " + +[node name="FuelLevelValue" type="Label" parent="GridContainer"] +margin_left = 222.0 +margin_right = 306.0 +margin_bottom = 20.0 +text = "??? / ???" + +[node name="LabelPricePerUnit" type="Label" parent="GridContainer"] +margin_top = 24.0 +margin_right = 218.0 +margin_bottom = 44.0 +text = "Price per fuel liter:" + +[node name="PricePerUnit" type="Label" parent="GridContainer"] +margin_left = 222.0 +margin_top = 24.0 +margin_right = 306.0 +margin_bottom = 44.0 +text = "??? Cu/l" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="LabelFullRefillPrice" type="Label" parent="GridContainer"] +margin_top = 48.0 +margin_right = 218.0 +margin_bottom = 68.0 +text = "Price for a complete refill: " + +[node name="FullRefillPrice" type="Label" parent="GridContainer"] +margin_left = 222.0 +margin_top = 48.0 +margin_right = 306.0 +margin_bottom = 68.0 +text = "??? Cu" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="AlreadyFullLabel" type="Label" parent="."] +margin_top = 128.0 +margin_right = 691.0 +margin_bottom = 148.0 +text = "Your tanks are full!" + +[node name="NotEnoughCuLabel" type="Label" parent="."] +margin_top = 168.0 +margin_right = 691.0 +margin_bottom = 211.0 +text = "You don't have enough capitalism units for a complete refill. +You can spend your whole savings to buy as much fuel as possible though!" + +[node name="BuyFuelButton" type="Button" parent="."] +margin_top = 231.0 +margin_right = 364.0 +margin_bottom = 257.0 +size_flags_horizontal = 0 +text = "Buy ??? liters of fuel now for only ??? Cu!" + +[connection signal="pressed" from="BuyFuelButton" to="." method="_on_BuyFuelButton_pressed"] diff --git a/destination/destination_menu/repair_option/repair_option.tscn b/destination/destination_menu/repair_option/repair_option.tscn index 5792810..ebc6ad8 100644 --- a/destination/destination_menu/repair_option/repair_option.tscn +++ b/destination/destination_menu/repair_option/repair_option.tscn @@ -16,42 +16,42 @@ __meta__ = { } [node name="PartNameLabel" type="Label" parent="."] -margin_right = 111.0 -margin_bottom = 14.0 +margin_right = 144.0 +margin_bottom = 20.0 text = "Part name:" [node name="PartNameValue" type="Label" parent="."] -margin_left = 161.0 -margin_right = 161.0 -margin_bottom = 14.0 +margin_left = 194.0 +margin_right = 194.0 +margin_bottom = 20.0 [node name="PartPriceLabel" type="Label" parent="."] -margin_top = 15.0 -margin_right = 111.0 -margin_bottom = 29.0 +margin_top = 21.0 +margin_right = 144.0 +margin_bottom = 41.0 text = "Price:" [node name="PartPriceValue" type="Label" parent="."] -margin_left = 161.0 -margin_top = 15.0 -margin_right = 161.0 -margin_bottom = 29.0 +margin_left = 194.0 +margin_top = 21.0 +margin_right = 194.0 +margin_bottom = 41.0 [node name="LineBreak2" type="Label" parent="."] -margin_top = 30.0 -margin_right = 111.0 -margin_bottom = 44.0 +margin_top = 42.0 +margin_right = 144.0 +margin_bottom = 62.0 [node name="LineBreak1" type="Label" parent="."] -margin_left = 161.0 -margin_top = 30.0 -margin_right = 161.0 -margin_bottom = 44.0 +margin_left = 194.0 +margin_top = 42.0 +margin_right = 194.0 +margin_bottom = 62.0 [node name="BuyButton" type="Button" parent="."] -margin_top = 45.0 -margin_right = 111.0 -margin_bottom = 65.0 +margin_top = 63.0 +margin_right = 144.0 +margin_bottom = 89.0 text = "Purchase repair" [connection signal="pressed" from="BuyButton" to="." method="_on_BuyButton_pressed"] diff --git a/main_game/PurchaseHandler.gd b/main_game/PurchaseHandler.gd index 20685b8..e25b9e5 100644 --- a/main_game/PurchaseHandler.gd +++ b/main_game/PurchaseHandler.gd @@ -1,3 +1,4 @@ +class_name PurchaseHandler extends Node2D signal cannot_afford(price, balance) signal item_purchased(name, price, balance) diff --git a/main_game/main_game.gd b/main_game/main_game.gd index 5c61850..3c70dd9 100644 --- a/main_game/main_game.gd +++ b/main_game/main_game.gd @@ -81,6 +81,9 @@ func handle_debug_keys(event): # F10: Spawn new asteroid elif event.scancode == KEY_F10: spawn_asteroid() + elif event.scancode == KEY_F11: + print("[Main] Debug key: refilling fuel") + $Player/Spaceship.refill_fuel(null) elif event.scancode == KEY_F5: show_game_over('Debug') diff --git a/project.godot b/project.godot index d072e59..dcea67e 100644 --- a/project.godot +++ b/project.godot @@ -44,6 +44,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://player/player.gd" }, { +"base": "Node2D", +"class": "PurchaseHandler", +"language": "GDScript", +"path": "res://main_game/PurchaseHandler.gd" +}, { "base": "GridContainer", "class": "RepairOption", "language": "GDScript", @@ -102,6 +107,7 @@ _global_script_class_icons={ "PassengerSpawner": "", "PlanetBody": "", "Player": "", +"PurchaseHandler": "", "RepairOption": "", "Rocket": "", "ShipBaseModule": "", diff --git a/spaceship/spaceship.gd b/spaceship/spaceship.gd index 7622413..6a6e75e 100644 --- a/spaceship/spaceship.gd +++ b/spaceship/spaceship.gd @@ -304,3 +304,13 @@ func consume_fuel(fuel_amount): if current_fuel < 0: current_fuel = 0 emit_signal("fuel_changed", total_fuel_capacity, current_fuel) + +func tanks_are_full() -> bool: + return current_fuel >= total_fuel_capacity + +func refill_fuel(refill_amount = null): + if refill_amount == null: + current_fuel = total_fuel_capacity + else: + current_fuel = min(current_fuel + refill_amount, total_fuel_capacity) + emit_signal("fuel_changed", total_fuel_capacity, current_fuel)