Skip to content

Commit

Permalink
Merge branch 'fuel-refill'
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryDiv committed Oct 4, 2021
2 parents 8294540 + f0bf7d7 commit 7da3f21
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 29 deletions.
5 changes: 3 additions & 2 deletions destination/destination_menu/destination_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
30 changes: 25 additions & 5 deletions destination/destination_menu/destination_menu.tscn
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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"]
44 changes: 22 additions & 22 deletions destination/destination_menu/repair_option/repair_option.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions main_game/PurchaseHandler.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class_name PurchaseHandler
extends Node2D
signal cannot_afford(price, balance)
signal item_purchased(name, price, balance)
Expand Down
3 changes: 3 additions & 0 deletions main_game/main_game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
6 changes: 6 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -102,6 +107,7 @@ _global_script_class_icons={
"PassengerSpawner": "",
"PlanetBody": "",
"Player": "",
"PurchaseHandler": "",
"RepairOption": "",
"Rocket": "",
"ShipBaseModule": "",
Expand Down
10 changes: 10 additions & 0 deletions spaceship/spaceship.gd
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,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)

0 comments on commit 7da3f21

Please sign in to comment.