diff --git a/addons/cogito/CogitoObjects/cogito_button.gd b/addons/cogito/CogitoObjects/cogito_button.gd index 262748f8..b1a4112e 100644 --- a/addons/cogito/CogitoObjects/cogito_button.gd +++ b/addons/cogito/CogitoObjects/cogito_button.gd @@ -35,6 +35,7 @@ var interaction_text : String var player_interaction_component : PlayerInteractionComponent var interaction_nodes : Array[Node] var cogito_properties : CogitoProperties = null +var currency_check : CurrencyCheck = null var cooldown : float func _ready() -> void: @@ -42,14 +43,21 @@ func _ready() -> void: add_to_group("save_object_state") interaction_nodes = find_children("","InteractionComponent",true) #Grabs all attached interaction components cooldown = 0 #Enabling button to be pressed right away. - interaction_text = usable_interaction_text + currency_check = find_child("CurrencyCheck", true, true) + if currency_check: + interaction_text = usable_interaction_text + (currency_check.currency_text if currency_check.currency_cost != 0 else "") + else: + interaction_text = usable_interaction_text object_state_updated.emit(interaction_text) find_cogito_properties() + + func find_cogito_properties(): var property_nodes = find_children("","CogitoProperties",true) #Grabs all attached property components if property_nodes: cogito_properties = property_nodes[0] + func _physics_process(delta: float) -> void: if cooldown > 0: cooldown -= delta @@ -60,6 +68,12 @@ func interact(_player_interaction_component:PlayerInteractionComponent): return player_interaction_component = _player_interaction_component + + if currency_check and currency_check.currency_cost != 0: + if not currency_check.check_for_currency(player_interaction_component.get_parent()): + player_interaction_component.send_hint(null, currency_check.not_enough_currency_hint) + return + if !allows_repeated_interaction and has_been_used: player_interaction_component.send_hint(null, has_been_used_hint) return @@ -90,7 +104,7 @@ func press(): object.interact(player_interaction_component) -func _on_damage_received(): +func _on_damage_received(damage): interact(CogitoSceneManager._current_player_node.player_interaction_component) @@ -112,11 +126,13 @@ func check_for_item() -> bool: func set_state(): if has_been_used: interaction_text = unusable_interaction_text + elif currency_check: + interaction_text = usable_interaction_text + (currency_check.currency_text if currency_check.currency_cost != 0 else "") else: interaction_text = usable_interaction_text - - object_state_updated.emit(interaction_text) + object_state_updated.emit(interaction_text) + func save(): var state_dict = { diff --git a/addons/cogito/Components/CurrencyCheckComponent.gd b/addons/cogito/Components/CurrencyCheckComponent.gd new file mode 100644 index 00000000..21c1fd2f --- /dev/null +++ b/addons/cogito/Components/CurrencyCheckComponent.gd @@ -0,0 +1,49 @@ +extends Node +class_name CurrencyCheck + + +signal transaction_success() +signal transaction_failed() + + +@export_group("Currency Check Settings") +##How much it costs to use this button, If set to 0 currency interaction will be ignored +@export var currency_cost : int = 0 +##Text that joins Press & Cost for Interaction +@export var currency_text_joiner : String = " | Cost: " +##Name of the currency needed for this interaction to proceed - default is credits +@export var currency_name : String = "credits" +##Currency icon location for Interaction UI +@export var currency_icon = "res://addons/cogito/Assets/Graphics/UiIcons/Ui_Icon_Currency.png" +##Hint that displays if player doesn't have enough currency to interact +@export var not_enough_currency_hint : String +##Should the player lose money after Currency check? +@export var apply_transaction : bool = true + + +var player_interaction_component : PlayerInteractionComponent +var icon_bbcode : String +var currency_text : String + + +func _ready(): + if currency_cost != 0: + icon_bbcode = "[img width=16 height=16]" + currency_icon + "[/img]" + currency_text = currency_text_joiner + str(currency_cost) + icon_bbcode + " " + + +func check_for_currency(player: Node) -> bool: + var found_currency + for attribute in player.find_children("", "CogitoCurrency", false): + if attribute is CogitoCurrency and attribute.currency_name == currency_name: + found_currency = attribute + break + + if found_currency and found_currency.value_current >= currency_cost: + if apply_transaction: + found_currency.value_current -= currency_cost + transaction_success.emit() + return true + else: + transaction_failed.emit() + return false diff --git a/addons/cogito/Components/CurrencyCheckComponent.tscn b/addons/cogito/Components/CurrencyCheckComponent.tscn new file mode 100644 index 00000000..9d383b24 --- /dev/null +++ b/addons/cogito/Components/CurrencyCheckComponent.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://6d6is2x1egew"] + +[ext_resource type="Script" path="res://addons/cogito/Components/CurrencyCheckComponent.gd" id="1_5ilxc"] + +[node name="CurrencyCheck" type="Node"] +script = ExtResource("1_5ilxc") +currency_cost = 5 +not_enough_currency_hint = "I can't afford it" diff --git a/addons/cogito/Components/UI/UI_PromptComponent.gd b/addons/cogito/Components/UI/UI_PromptComponent.gd index 079d0f6b..36d1ff37 100644 --- a/addons/cogito/Components/UI/UI_PromptComponent.gd +++ b/addons/cogito/Components/UI/UI_PromptComponent.gd @@ -2,7 +2,7 @@ extends Control class_name UiPromptComponent @onready var interaction_button: Node = $HBoxContainer/Container/InteractionButton -@onready var interaction_text: Label = $HBoxContainer/InteractionText +@onready var interaction_text: RichTextLabel = $HBoxContainer/InteractionText func set_prompt(interaction_name:String,input_map_name:String): interaction_button.action_name = input_map_name diff --git a/addons/cogito/Components/UI/UI_PromptComponent.tscn b/addons/cogito/Components/UI/UI_PromptComponent.tscn index 7071c4cf..8f28497b 100644 --- a/addons/cogito/Components/UI/UI_PromptComponent.tscn +++ b/addons/cogito/Components/UI/UI_PromptComponent.tscn @@ -41,8 +41,12 @@ centered = false frame = 6 action_name = "interact" -[node name="InteractionText" type="Label" parent="HBoxContainer"] +[node name="InteractionText" type="RichTextLabel" parent="HBoxContainer"] layout_mode = 2 theme = ExtResource("2_bet8p") theme_type_variation = &"PromptLabel" +theme_override_font_sizes/normal_font_size = 20 +bbcode_enabled = true text = "Interact" +fit_content = true +autowrap_mode = 0 diff --git a/addons/cogito/DemoScenes/DemoPrefabs/generic_button.tscn b/addons/cogito/DemoScenes/DemoPrefabs/generic_button.tscn index 075b817c..300dde52 100644 --- a/addons/cogito/DemoScenes/DemoPrefabs/generic_button.tscn +++ b/addons/cogito/DemoScenes/DemoPrefabs/generic_button.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=10 format=3 uid="uid://cs6raqlyejqul"] +[gd_scene load_steps=11 format=3 uid="uid://cs6raqlyejqul"] [ext_resource type="Script" path="res://addons/cogito/CogitoObjects/cogito_button.gd" id="1_l32yq"] [ext_resource type="AudioStream" uid="uid://b6wdy2102jymr" path="res://addons/cogito/Assets/Audio/Kenney/UiAudio/switch2.ogg" id="2_awyad"] [ext_resource type="PackedScene" uid="uid://l61jtpfxu5x5" path="res://addons/cogito/Components/Interactions/BasicInteraction.tscn" id="3_v8dn1"] [ext_resource type="PackedScene" uid="uid://k28yrbg3k3pw" path="res://addons/cogito/Components/HitboxComponent.tscn" id="5_dudfk"] +[ext_resource type="PackedScene" uid="uid://6d6is2x1egew" path="res://addons/cogito/Components/CurrencyCheckComponent.tscn" id="6_lhou4"] [ext_resource type="PackedScene" uid="uid://cj0yaeh3yg7tu" path="res://addons/cogito/Components/Properties/CogitoProperties.tscn" id="6_w5n86"] [sub_resource type="CylinderMesh" id="CylinderMesh_clo4d"] @@ -49,4 +50,7 @@ shape = SubResource("BoxShape3D_nti18") material_properties = 1 burn_damage_amount = 1.0 +[node name="CurrencyCheck" parent="." instance=ExtResource("6_lhou4")] +currency_cost = 0 + [connection signal="damage_received" from="." to="." method="_on_damage_received"]