brackeys-12/ui/building_icon.gd
2024-09-17 16:37:19 -04:00

112 lines
3.5 KiB
GDScript

extends ColorRect
var key
var clicked = false
@export var left = false
@onready var texture_rect: TextureRect = $TextureRect
@onready var rich_text_label: RichTextLabel = $ColorRect/RichTextLabel
@onready var rich_text_label_2: RichTextLabel = $ColorRect/RichTextLabel2
@onready var rich_text_label_3: RichTextLabel = $ColorRect/RichTextLabel3
@onready var rich_text_label_4: RichTextLabel = $ColorRect/RichTextLabel4
@onready var color_rect: ColorRect = $ColorRect
var hover_tween
func _ready() -> void:
if left:
color_rect.position += Vector2(20, 0)
func _created():
var data = Data.data.buildings[key]
rich_text_label.text = "[center][smallwave]%s" % [data.name]
rich_text_label_2.text = "[center]%s" % [data.short]
if data.cost.has("wood") and data.cost.has("stone"):
rich_text_label_3.text = "[center]%s stone" % [data.cost.stone]
rich_text_label_3.modulate = Color.html("#425a67")
rich_text_label_4.modulate = Color.html("#884244")
rich_text_label_4.text = "[center]%s wood" % [data.cost.wood]
elif data.cost.has("wood"):
rich_text_label_3.text = "[center]%s wood" % [data.cost.wood]
rich_text_label_4.text = ""
rich_text_label_3.modulate = Color.html("#884244")
else:
rich_text_label_3.text = "[center]%s stone" % [data.cost.stone]
rich_text_label_4.text = ""
rich_text_label_3.modulate = Color.html("#425a67")
func _on_mouse_handler_clicked() -> void:
if Persister.get_value("win") or Persister.get_value("lose"):
return
var data = Data.data.buildings[key]
if (not data.cost.has("wood") or int(data.cost.wood) <= Persister.get_value("wood")) and (not data.cost.has("stone") or int(data.cost.stone) <= Persister.get_value("stone")):
clicked = true
if hover_tween:
hover_tween.kill()
hover_tween = create_tween()
hover_tween.set_ease(Tween.EASE_OUT)
hover_tween.set_trans(Tween.TRANS_BACK)
hover_tween.set_parallel()
hover_tween.tween_property(self, "scale", Vector2.ONE * 0.95, 0.25)
hover_tween.tween_property(color_rect, "scale", Vector2.ONE, 0.25)
func _on_mouse_handler_hovered() -> void:
if hover_tween:
hover_tween.kill()
hover_tween = create_tween()
hover_tween.set_ease(Tween.EASE_OUT)
hover_tween.set_trans(Tween.TRANS_BACK)
hover_tween.set_parallel()
hover_tween.tween_property(self, "scale", Vector2.ONE * 1.05, 0.25)
hover_tween.tween_property(color_rect, "scale", Vector2.ONE, 0.25)
func _on_mouse_handler_unhovered() -> void:
if clicked:
#Triggerer.trigger("drag_building", { "key": key })
Persister.persist_data("drag_mode", true)
Persister.persist_data("building_mode", true)
Persister.persist_data("building_key", key)
clicked = false
if hover_tween:
hover_tween.kill()
hover_tween = create_tween()
hover_tween.set_ease(Tween.EASE_OUT)
hover_tween.set_trans(Tween.TRANS_QUAD)
hover_tween.set_parallel()
hover_tween.tween_property(self, "scale", Vector2.ONE, 0.25)
hover_tween.tween_property(color_rect, "scale", Vector2.ZERO, 0.25)
func _on_mouse_handler_released() -> void:
if Persister.get_value("win") or Persister.get_value("lose"):
return
if clicked:
#Triggerer.trigger("select_building", { "key": key })
Persister.persist_data("drag_mode", false)
Persister.persist_data("building_mode", true)
Persister.persist_data("building_key", key)
clicked = false
if hover_tween:
hover_tween.kill()
hover_tween = create_tween()
hover_tween.set_ease(Tween.EASE_OUT)
hover_tween.set_trans(Tween.TRANS_BACK)
hover_tween.set_parallel()
hover_tween.tween_property(self, "scale", Vector2.ONE, 0.25)
hover_tween.tween_property(color_rect, "scale", Vector2.ZERO, 0.25)