Skip to content

Commit

Permalink
Refactor UpgradeButton and UpgradeMenu
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
NathanLovato committed Aug 21, 2020
1 parent 62911fb commit bee5a67
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
53 changes: 29 additions & 24 deletions project/src/UI/Upgrades/UpgradeButton.gd
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 16 additions & 10 deletions project/src/UI/Upgrades/UpgradeButton.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

[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
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 ) ]
Expand All @@ -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 ]
Expand All @@ -39,22 +41,24 @@ 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 ) ]
}

[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
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 ) ]
Expand All @@ -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 ]
}
Expand All @@ -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 )
Expand Down Expand Up @@ -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"]
19 changes: 9 additions & 10 deletions project/src/UI/Upgrades/UpgradeMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# 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
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:
Expand All @@ -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()

0 comments on commit bee5a67

Please sign in to comment.