brackeys-12/ui.gd
2024-09-15 04:43:56 -04:00

114 lines
4.1 KiB
GDScript

extends CanvasLayer
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var wood_count_label_2: Label = $UI/Background/Control3/WoodCountLabel2
@onready var restart: Control = $Restart
@onready var color_rect_2: ColorRect = $Restart/ColorRect2
@onready var death: Control = $Death
@onready var rich_text_label_lose: RichTextLabel = $Death/RichTextLabelLose
@onready var rich_text_label_2_lose: RichTextLabel = $Death/RichTextLabel2Lose
@onready var rich_text_label_3_lose: RichTextLabel = $Death/RichTextLabel3Lose
@onready var win: Control = $Win
@onready var rich_text_label_win: RichTextLabel = $Win/RichTextLabelWin
@onready var rich_text_label_2_win: RichTextLabel = $Win/RichTextLabel2Win
@onready var rich_text_label_3_win: RichTextLabel = $Win/RichTextLabel3Win
var available_tween
var restart_tween
func _ready() -> void:
Persister.persist_data("wood", 5)
Persister.persist_data("stone", 5)
Persister.persist_data("population", 3)
Persister.persist_data("avail_population", 3)
Persister.persist_data("food", 75)
Persister.persist_data("water", 75)
Persister.persist_data("max_population", 3)
Triggerer.listen("show_info", _on_show_info)
Triggerer.listen("hide_info", _on_hide_info)
Triggerer.listen("lose", _on_lose)
Triggerer.listen("win", _on_win)
func _process(delta: float) -> void:
if Input.is_action_just_pressed("lclick") and not Persister.get_value("game_started"):
Persister.persist_data("game_started", true)
animation_player.play("start")
if Input.is_action_just_pressed("lclick") and Persister.get_value("can_restart"):
Persister.clear_category(PersisterEnums.Scope.RUN)
get_tree().change_scene_to_packed(preload("res://Main.tscn"))
wood_count_label_2.text = "%d available" % [Persister.get_value("avail_population")]
if Input.is_action_just_pressed("restart"):
if restart_tween:
restart_tween.kill()
restart_tween = create_tween()
restart_tween.set_parallel()
restart_tween.tween_property(restart, "modulate", Color.WHITE, 0.5)
restart_tween.tween_property(color_rect_2, "scale:x", 1, 1).set_delay(0.1)
restart_tween.chain()
restart_tween.tween_callback(func():
Persister.clear_category(PersisterEnums.Scope.RUN)
get_tree().change_scene_to_packed(preload("res://Main.tscn"))
)
if Input.is_action_just_released("restart"):
if restart_tween:
restart_tween.kill()
restart_tween = create_tween()
restart_tween.set_parallel()
restart_tween.tween_property(color_rect_2, "scale:x", 0, 1)
restart_tween.tween_property(restart, "modulate", Color.TRANSPARENT, 0.5)
func _on_show_info(_data):
if available_tween:
available_tween.kill()
available_tween = create_tween()
available_tween.set_ease(Tween.EASE_OUT)
available_tween.set_trans(Tween.TRANS_BACK)
available_tween.tween_property(wood_count_label_2, "scale", Vector2.ONE, 0.5)
func _on_hide_info(_data):
if available_tween:
available_tween.kill()
available_tween = create_tween()
available_tween.set_ease(Tween.EASE_OUT)
available_tween.set_trans(Tween.TRANS_QUAD)
available_tween.tween_property(wood_count_label_2, "scale", Vector2.ZERO, 0.5)
func _on_lose(_data):
var tween = create_tween()
rich_text_label_2_lose.text = "[center]Days survived: %d" % [Persister.get_value("days")]
rich_text_label_2_lose.visible_ratio = 0
rich_text_label_3_lose.visible_ratio = 0
tween.tween_property(death, "modulate", Color.WHITE, 1)
tween.tween_property(rich_text_label_2_lose, "visible_ratio", 1.0, 0.5)
tween.tween_callback(func():
Persister.persist_data("can_restart", true)
)
tween.tween_property(rich_text_label_3_lose, "visible_ratio", 1.0, 0.5)
func _on_win(_data):
var tween = create_tween()
rich_text_label_2_win.text = "[center]Days survived: %d" % [Persister.get_value("days")]
rich_text_label_2_win.visible_ratio = 0
rich_text_label_3_win.visible_ratio = 0
tween.tween_property(win, "modulate", Color.WHITE, 1)
tween.tween_property(rich_text_label_2_win, "visible_ratio", 1.0, 0.5)
tween.tween_callback(func():
Persister.persist_data("can_restart", true)
)
tween.tween_property(rich_text_label_3_win, "visible_ratio", 1.0, 0.5)