Skip to content

Commit

Permalink
Currency Check component
Browse files Browse the repository at this point in the history
Rework of Phazorknight#313 to comply with new structure
  • Loading branch information
niefia committed Oct 15, 2024
1 parent 8b524cd commit b8054cd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
18 changes: 16 additions & 2 deletions addons/cogito/CogitoObjects/cogito_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ 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:
self.add_to_group("interactable")
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
Expand All @@ -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
Expand Down Expand Up @@ -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)


Expand Down
49 changes: 49 additions & 0 deletions addons/cogito/Components/CurrencyCheckComponent.gd
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions addons/cogito/Components/CurrencyCheckComponent.tscn
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion addons/cogito/Components/UI/UI_PromptComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion addons/cogito/Components/UI/UI_PromptComponent.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion addons/cogito/DemoScenes/DemoPrefabs/generic_button.tscn
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -49,4 +50,6 @@ shape = SubResource("BoxShape3D_nti18")
material_properties = 1
burn_damage_amount = 1.0

[node name="CurrencyCheck" parent="." instance=ExtResource("6_lhou4")]

[connection signal="damage_received" from="." to="." method="_on_damage_received"]

0 comments on commit b8054cd

Please sign in to comment.