From bee5a67f1244feaf0fe5bc0c1c53bb1984c05b6c Mon Sep 17 00:00:00 2001 From: Nathan Lovato Date: Thu, 20 Aug 2020 22:21:49 -0600 Subject: [PATCH] Refactor UpgradeButton and UpgradeMenu - Rename signals and variables - Make animations shorter: from 30 frames to ~10 frames - Make some functions and variables pseudo-private - In UpgradeMenu, store references to buttons directly instead of their count --- project/src/UI/Upgrades/UpgradeButton.gd | 53 ++++++++++++---------- project/src/UI/Upgrades/UpgradeButton.tscn | 26 +++++++---- project/src/UI/Upgrades/UpgradeMenu.gd | 19 ++++---- 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/project/src/UI/Upgrades/UpgradeButton.gd b/project/src/UI/Upgrades/UpgradeButton.gd index 5da4747..cacd3cd 100644 --- a/project/src/UI/Upgrades/UpgradeButton.gd +++ b/project/src/UI/Upgrades/UpgradeButton.gd @@ -1,47 +1,52 @@ -# Sets and controls the icon and label of the upgrade buttons. The tool keyword +# Sets and controls the icon and _label of the upgrade buttons. The tool keyword # makes sure we can see the result in the editor. tool extends TextureButton -signal on_hide_complete; -signal on_show_complete; +signal appeared; +signal disappeared; export var texture: Texture setget set_texture export var text := "" setget set_text -onready var texture_rect := $VBoxContainer/TextureRect -onready var label := $VBoxContainer/Label -onready var animation_player := $AnimationPlayer +onready var _texture_rect := $VBoxContainer/TextureRect +onready var _label := $VBoxContainer/Label +onready var _animation_player := $AnimationPlayer -func show_delayed(delay : float = 0) -> void: - play_animation("show", delay) - yield(animation_player, 'animation_finished') - emit_signal("on_show_complete") +func appear(delay : float = 0) -> void: + _play_animation("show", delay) -func hide_delayed(delay : float = 0) -> void: - play_animation("hide", delay) - yield(animation_player, 'animation_finished') - emit_signal("on_hide_complete") +func disappear(delay : float = 0) -> void: + _play_animation("hide", delay) -func play_animation(animation, delay) -> void: - animation_player.set_assigned_animation(animation) - animation_player.seek(0, true) +func _play_animation(animation, delay) -> void: + _animation_player.set_assigned_animation(animation) + _animation_player.seek(0, true) yield(get_tree().create_timer(delay), "timeout") - animation_player.play() - + _animation_player.play() + + func set_texture(value: Texture) -> void: texture = value - if not texture_rect: + if not _texture_rect: yield(self, "ready") - texture_rect.texture = value + _texture_rect.texture = value func set_text(value: String) -> void: text = value - if not texture_rect: + if not _texture_rect: yield(self, "ready") - label.text = value - label.visible = text != "" + _label.text = value + _label.visible = text != "" + + +func _on_AnimationPlayer_animation_finished(anim_name: String) -> void: + match anim_name: + "show": + emit_signal("appeared") + "hide": + emit_signal("disappeared") diff --git a/project/src/UI/Upgrades/UpgradeButton.tscn b/project/src/UI/Upgrades/UpgradeButton.tscn index ccb1c76..ff78921 100644 --- a/project/src/UI/Upgrades/UpgradeButton.tscn +++ b/project/src/UI/Upgrades/UpgradeButton.tscn @@ -8,6 +8,8 @@ [sub_resource type="Animation" id=1] resource_name = "hide" +length = 0.266667 +step = 0.0333333 tracks/0/type = "value" tracks/0/path = NodePath(".:modulate") tracks/0/interp = 1 @@ -15,7 +17,7 @@ tracks/0/loop_wrap = true tracks/0/imported = false tracks/0/enabled = true tracks/0/keys = { -"times": PoolRealArray( 0, 0.6 ), +"times": PoolRealArray( 0.0666667, 0.266667 ), "transitions": PoolRealArray( 1, 1 ), "update": 0, "values": [ Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ] @@ -25,9 +27,9 @@ tracks/1/path = NodePath(".:margin_top") tracks/1/interp = 1 tracks/1/loop_wrap = true tracks/1/imported = false -tracks/1/enabled = true +tracks/1/enabled = false tracks/1/keys = { -"times": PoolRealArray( 0, 0.7 ), +"times": PoolRealArray( 0, 0.266667 ), "transitions": PoolRealArray( 2.46229, 1 ), "update": 0, "values": [ 0.0, 46.0 ] @@ -39,7 +41,7 @@ tracks/2/loop_wrap = true tracks/2/imported = false tracks/2/enabled = true tracks/2/keys = { -"times": PoolRealArray( 0, 0.7 ), +"times": PoolRealArray( 0, 0.266667 ), "transitions": PoolRealArray( 2.14355, 1 ), "update": 0, "values": [ Vector2( 1, 1 ), Vector2( 0.2, 0.2 ) ] @@ -47,6 +49,8 @@ tracks/2/keys = { [sub_resource type="Animation" id=2] resource_name = "show" +length = 0.366667 +step = 0.0333333 tracks/0/type = "value" tracks/0/path = NodePath(".:modulate") tracks/0/interp = 1 @@ -54,7 +58,7 @@ tracks/0/loop_wrap = true tracks/0/imported = false tracks/0/enabled = true tracks/0/keys = { -"times": PoolRealArray( 0, 0.7 ), +"times": PoolRealArray( 0, 0.3 ), "transitions": PoolRealArray( 1, 1 ), "update": 0, "values": [ Color( 1, 1, 1, 0 ), Color( 1, 1, 1, 1 ) ] @@ -66,8 +70,8 @@ tracks/1/loop_wrap = true tracks/1/imported = false tracks/1/enabled = true tracks/1/keys = { -"times": PoolRealArray( 0, 0.7 ), -"transitions": PoolRealArray( 0.217638, 0.329877 ), +"times": PoolRealArray( 0, 0.366667 ), +"transitions": PoolRealArray( 0.170755, 0.329877 ), "update": 0, "values": [ 108.0, 0.0 ] } @@ -78,13 +82,14 @@ tracks/2/loop_wrap = true tracks/2/imported = false tracks/2/enabled = true tracks/2/keys = { -"times": PoolRealArray( 0, 0.1, 0.2, 0.4, 0.7 ), -"transitions": PoolRealArray( 0.435275, 1, 1, 1, 1 ), +"times": PoolRealArray( 0, 0.266667 ), +"transitions": PoolRealArray( 0.435275, 1 ), "update": 0, -"values": [ Vector2( 0.2, 0.2 ), Vector2( 0.3, 0.7 ), Vector2( 1.1, 1.05 ), Vector2( 0.95, 0.93 ), Vector2( 1, 1 ) ] +"values": [ Vector2( 0.2, 0.2 ), Vector2( 1, 1 ) ] } [node name="UpgradeButton" type="TextureButton"] +modulate = Color( 1, 1, 1, 0 ) margin_right = 128.0 margin_bottom = 128.0 rect_pivot_offset = Vector2( 64, 64 ) @@ -130,3 +135,4 @@ align = 1 [node name="AnimationPlayer" type="AnimationPlayer" parent="."] anims/hide = SubResource( 1 ) anims/show = SubResource( 2 ) +[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_AnimationPlayer_animation_finished"] diff --git a/project/src/UI/Upgrades/UpgradeMenu.gd b/project/src/UI/Upgrades/UpgradeMenu.gd index d23ea61..c171b7d 100644 --- a/project/src/UI/Upgrades/UpgradeMenu.gd +++ b/project/src/UI/Upgrades/UpgradeMenu.gd @@ -2,7 +2,6 @@ # indicates an upgrade - the player reacts by improving what was selected, # and pirates spawn. extends Control -# TODO: in the future, build the menu from available upgrades and related data? onready var health_button := $HBoxContainer/HealthUpgrade onready var speed_button := $HBoxContainer/SpeedUpgrade @@ -10,7 +9,8 @@ onready var cargo_button := $HBoxContainer/CargoUpgrade onready var mine_button := $HBoxContainer/MiningUpgrade onready var weapon_button := $HBoxContainer/WeaponUpgrade onready var hbox_container := $HBoxContainer -onready var button_count := $HBoxContainer.get_child_count(); + +onready var buttons := hbox_container.get_children() func _ready() -> void: @@ -24,17 +24,16 @@ func _ready() -> void: func open() -> void: get_tree().paused = true health_button.grab_focus() - for i in range(button_count): - var button = hbox_container.get_child(i) - button.show_delayed(i * 0.1) + for button in buttons: + button.appear(button.get_index() * 0.1) show() -# Emit a signal through the Events signal bus to transfer the upgrade selected by the player. + +# Emit a signal through the Events signal bus to unlock the upgrade selected by the player. func select_upgrade(type: int) -> void: get_tree().paused = false Events.emit_signal("upgrade_chosen", type) - for i in range(button_count): - var button = hbox_container.get_child(i) - button.hide_delayed(i * 0.1) - yield(hbox_container.get_child(button_count - 1), "on_hide_complete") + for button in buttons: + button.disappear(button.get_index() * 0.1) + yield(buttons[-1], "disappeared") hide()