-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reintroduce currency trading in a new window, triggered by a shortcut
- Loading branch information
1 parent
d2f44bc
commit 36a3ed4
Showing
9 changed files
with
251 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
screens/game/currency_trading/currency_trading_window.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
extends Window | ||
class_name CurrencyTradingWindow | ||
|
||
@export var buy_spin_box: SpinBox | ||
@export var buy_option_button: OptionButton | ||
@export var buy_balance_label: Label | ||
@export var trade_button: Button | ||
@export var sell_spin_box: SpinBox | ||
@export var sell_option_button: OptionButton | ||
@export var sell_balance_label: Label | ||
|
||
const CURRENCIES_DIRECTORY = "res://mechanics/economy/currencies/" | ||
|
||
var bank_account: BankAccount | ||
var _currencies: Array[Currency] | ||
var _updating: bool | ||
|
||
func _ready() -> void: | ||
self._updating = true | ||
self.bank_account.changed.connect(_update) | ||
self._currencies = self._load_all_currencies() | ||
|
||
for currency in self._currencies: | ||
self.buy_option_button.add_item(currency.name) | ||
self.sell_option_button.add_item(currency.name) | ||
|
||
self.buy_option_button.select(1) | ||
self.sell_option_button.select(0) | ||
self._updating = false | ||
|
||
self._update() | ||
|
||
func _input(event: InputEvent) -> void: | ||
if event.is_action_pressed("toggle_currency_trading"): | ||
self.get_viewport().set_input_as_handled() | ||
self.queue_free() | ||
|
||
func _get_buy_currency() -> Currency: | ||
return self._currencies[self.buy_option_button.selected] if self.buy_option_button.selected >= 0 else null | ||
|
||
func _get_sell_currency() -> Currency: | ||
return self._currencies[self.sell_option_button.selected] if self.sell_option_button.selected >= 0 else null | ||
|
||
func _on_trade_button_pressed() -> void: | ||
var buy_currency: Currency = self._get_buy_currency() | ||
var sell_currency: Currency = self._get_sell_currency() | ||
assert(buy_currency and sell_currency and buy_currency != sell_currency, "Expected to have distinct currencies set for buying and selling") | ||
|
||
self._updating = true | ||
|
||
var price := buy_currency.price_in_credits / sell_currency.price_in_credits | ||
var withdrew := self.bank_account.withdraw_up_to(sell_currency, self.sell_spin_box.value) | ||
self.bank_account.deposit(buy_currency, withdrew * price) | ||
|
||
self._updating = false | ||
self._update() | ||
|
||
func _on_buy_option_button_item_selected(_index: int) -> void: | ||
if self._updating: | ||
return | ||
|
||
self._update() | ||
self._update_buy_spin_box_value() | ||
|
||
func _on_buy_spin_box_value_changed(_value: float) -> void: | ||
self._update_sell_spin_box_value() | ||
|
||
func _update_sell_spin_box_value() -> void: | ||
if self._updating: | ||
return | ||
|
||
self._updating = true | ||
|
||
var buy_currency: Currency = self._get_buy_currency() | ||
var sell_currency: Currency = self._get_sell_currency() | ||
if buy_currency and sell_currency and buy_currency != sell_currency: | ||
self.sell_spin_box.value = sell_currency.round(self.buy_spin_box.value * buy_currency.price_in_other_currency(sell_currency)) | ||
|
||
self._updating = false | ||
|
||
func _on_sell_option_button_item_selected(_index: int) -> void: | ||
if self._updating: | ||
return | ||
|
||
self._update() | ||
self._update_sell_spin_box_value() | ||
|
||
func _on_sell_spin_box_value_changed(_value: float) -> void: | ||
self._update_buy_spin_box_value() | ||
|
||
func _update_buy_spin_box_value() -> void: | ||
if self._updating: | ||
return | ||
|
||
self._updating = true | ||
|
||
var buy_currency: Currency = self._get_buy_currency() | ||
var sell_currency: Currency = self._get_sell_currency() | ||
if buy_currency and sell_currency and buy_currency != sell_currency: | ||
self.buy_spin_box.value = buy_currency.round(self.sell_spin_box.value * sell_currency.price_in_other_currency(buy_currency)) | ||
|
||
self._updating = false | ||
|
||
func _update() -> void: | ||
self._updating = true | ||
|
||
var buy_currency: Currency = self._get_buy_currency() | ||
var sell_currency: Currency = self._get_sell_currency() | ||
|
||
if buy_currency: | ||
var balance: float = self.bank_account.currencies.get(buy_currency, 0.0) | ||
self.buy_balance_label.text = "Balance: %s" % buy_currency.amount_as_string(balance) | ||
self.buy_spin_box.step = pow(10, -buy_currency.precision) | ||
else: | ||
self.buy_balance_label.text = "" | ||
|
||
if sell_currency: | ||
var balance: float = self.bank_account.currencies.get(sell_currency, 0.0) | ||
self.sell_balance_label.text = "Balance: %s" % sell_currency.amount_as_string(balance) | ||
self.sell_spin_box.step = pow(10, -sell_currency.precision) | ||
else: | ||
self.sell_balance_label.text = "" | ||
|
||
if buy_currency and sell_currency and buy_currency != sell_currency: | ||
var sell_balance: float = self.bank_account.currencies.get(sell_currency, 0.0) | ||
|
||
self.trade_button.disabled = sell_balance < buy_currency.price_in_other_currency(sell_currency) | ||
self.buy_spin_box.max_value = buy_currency.round(sell_balance * sell_currency.price_in_other_currency(buy_currency)) | ||
self.sell_spin_box.max_value = sell_balance | ||
else: | ||
self.trade_button.disabled = true | ||
|
||
self._updating = false | ||
|
||
func _on_close_requested() -> void: | ||
self.queue_free() | ||
|
||
func _load_all_currencies() -> Array[Currency]: | ||
var result: Array[Currency] = [] | ||
|
||
for file in DirAccess.get_files_at(CURRENCIES_DIRECTORY): | ||
result.push_back(load("%s/%s" % [CURRENCIES_DIRECTORY, file])) | ||
|
||
return result |
84 changes: 84 additions & 0 deletions
84
screens/game/currency_trading/currency_trading_window.tscn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
[gd_scene load_steps=2 format=3 uid="uid://b1yvdsvgyesj5"] | ||
|
||
[ext_resource type="Script" path="res://screens/game/currency_trading/currency_trading_window.gd" id="1_a2pkb"] | ||
|
||
[node name="CurrencyTradingWindow" type="Window" node_paths=PackedStringArray("buy_spin_box", "buy_option_button", "buy_balance_label", "trade_button", "sell_spin_box", "sell_option_button", "sell_balance_label")] | ||
title = "Currency Exchange" | ||
initial_position = 2 | ||
size = Vector2i(600, 200) | ||
script = ExtResource("1_a2pkb") | ||
buy_spin_box = NodePath("PanelContainer/GridContainer/BuyContainer/BuySpinBox") | ||
buy_option_button = NodePath("PanelContainer/GridContainer/BuyContainer/BuyOptionButton") | ||
buy_balance_label = NodePath("PanelContainer/GridContainer/BuyBalanceLabel") | ||
trade_button = NodePath("PanelContainer/GridContainer/SellContainer/TradeButton") | ||
sell_spin_box = NodePath("PanelContainer/GridContainer/SellContainer/SellSpinBox") | ||
sell_option_button = NodePath("PanelContainer/GridContainer/SellContainer/SellOptionButton") | ||
sell_balance_label = NodePath("PanelContainer/GridContainer/SellBalanceLabel") | ||
|
||
[node name="PanelContainer" type="PanelContainer" parent="."] | ||
anchors_preset = 15 | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
grow_horizontal = 2 | ||
grow_vertical = 2 | ||
size_flags_horizontal = 3 | ||
size_flags_vertical = 3 | ||
|
||
[node name="GridContainer" type="GridContainer" parent="PanelContainer"] | ||
layout_mode = 2 | ||
size_flags_horizontal = 4 | ||
size_flags_vertical = 4 | ||
columns = 4 | ||
|
||
[node name="BuyLabel" type="Label" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
text = "BUY " | ||
|
||
[node name="BuyContainer" type="HBoxContainer" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="BuySpinBox" type="SpinBox" parent="PanelContainer/GridContainer/BuyContainer"] | ||
layout_mode = 2 | ||
custom_arrow_step = 1.0 | ||
|
||
[node name="BuyOptionButton" type="OptionButton" parent="PanelContainer/GridContainer/BuyContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="SellLabel" type="Label" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
text = " FOR " | ||
|
||
[node name="SellContainer" type="HBoxContainer" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="SellSpinBox" type="SpinBox" parent="PanelContainer/GridContainer/SellContainer"] | ||
layout_mode = 2 | ||
custom_arrow_step = 1.0 | ||
|
||
[node name="SellOptionButton" type="OptionButton" parent="PanelContainer/GridContainer/SellContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="TradeButton" type="Button" parent="PanelContainer/GridContainer/SellContainer"] | ||
layout_mode = 2 | ||
text = "TRADE" | ||
|
||
[node name="Empty" type="Control" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="BuyBalanceLabel" type="Label" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
text = "Balance: 123 credits" | ||
|
||
[node name="Empty2" type="Control" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
|
||
[node name="SellBalanceLabel" type="Label" parent="PanelContainer/GridContainer"] | ||
layout_mode = 2 | ||
text = "Balance: 123 lumins" | ||
|
||
[connection signal="close_requested" from="." to="." method="_on_close_requested"] | ||
[connection signal="value_changed" from="PanelContainer/GridContainer/BuyContainer/BuySpinBox" to="." method="_on_buy_spin_box_value_changed"] | ||
[connection signal="item_selected" from="PanelContainer/GridContainer/BuyContainer/BuyOptionButton" to="." method="_on_buy_option_button_item_selected"] | ||
[connection signal="value_changed" from="PanelContainer/GridContainer/SellContainer/SellSpinBox" to="." method="_on_sell_spin_box_value_changed"] | ||
[connection signal="item_selected" from="PanelContainer/GridContainer/SellContainer/SellOptionButton" to="." method="_on_sell_option_button_item_selected"] | ||
[connection signal="pressed" from="PanelContainer/GridContainer/SellContainer/TradeButton" to="." method="_on_trade_button_pressed"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters