From 04c03d9a234571dbfb8a9671f6ab1e7cc5b15387 Mon Sep 17 00:00:00 2001 From: Nathan Lovato Date: Tue, 15 Jan 2019 12:58:34 +0900 Subject: [PATCH] Refactor PopupLabel and PopupLabelBuilder to simplify their API --- godot/combat/interface/PopupLabel.gd | 30 ++++++++++----------- godot/combat/interface/PopupLabelBuilder.gd | 30 ++++++++++++--------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/godot/combat/interface/PopupLabel.gd b/godot/combat/interface/PopupLabel.gd index e0c3dfa..6948d66 100644 --- a/godot/combat/interface/PopupLabel.gd +++ b/godot/combat/interface/PopupLabel.gd @@ -1,24 +1,24 @@ extends Control -onready var anim_player = get_node("AnimationPlayer") -var _animation_name : String +onready var anim_player : AnimationPlayer = $AnimationPlayer +onready var label : Label = $Label -export var offset : Vector2 = Vector2(0.0, -40.0) +export var offset : = Vector2(0.0, -40.0) -func initialize(battler : Battler, type : String, message : String): +func start(battler : Battler, type : String, message : String) -> void: """ + Initializes the node and starts its animation @type: either health, mana, missed. Determines the animation the label will use """ - assert type in ["health", "mana", "missed"] - var battler_extents : RectExtents = battler.skin.get_extents() - get_node("Label").text = message + assert type in ['miss', 'mana', 'health'] + var extents : RectExtents = battler.skin.get_extents() + label.text = message + + var animation_name : = "" if type == "missed": - offset = offset * 2 - _animation_name = type + offset *= 2 + animation_name = type elif type == "health" or type == "mana": - _animation_name = type + "_loss" if int(message) <= 0 else type + "_gain" - rect_global_position = battler.global_position - Vector2(0.0, battler_extents.size.y) + offset - -func play() -> void: - anim_player.play(_animation_name) - + animation_name = type + "_loss" if int(message) <= 0 else type + "_gain" + rect_global_position = battler.global_position - Vector2(0.0, extents.size.y) + offset + anim_player.play(animation_name) diff --git a/godot/combat/interface/PopupLabelBuilder.gd b/godot/combat/interface/PopupLabelBuilder.gd index 63bddf2..dbbbbb6 100644 --- a/godot/combat/interface/PopupLabelBuilder.gd +++ b/godot/combat/interface/PopupLabelBuilder.gd @@ -1,20 +1,26 @@ extends Control -const label_scene = preload("PopupLabel.tscn") +const PopupLabel : = preload("PopupLabel.tscn") func initialize(battlers : Array) -> void: for battler in battlers: - battler.stats.connect("health_changed", self, "convert_numbers", [battler, "health"]) - battler.stats.connect("mana_changed", self, "convert_numbers", [battler, "mana"]) + battler.stats.connect("health_changed", self, "spawn_label_number", [battler, "health"]) + battler.stats.connect("mana_changed", self, "spawn_label_number", [battler, "mana"]) for skill in battler.skills.get_children(): skill.connect("missed", self, "spawn_label", [battler, "missed"]) -func convert_numbers(new_value : int, old_value : int, battler : Battler, signal_type : String) -> void: - var difference = str(new_value - old_value) - spawn_label(difference, battler, signal_type) - -func spawn_label(signal_message : String, battler : Battler, signal_type : String) -> void: - var damage_label = label_scene.instance() - damage_label.initialize(battler, signal_type, signal_message) - add_child(damage_label) - damage_label.play() \ No newline at end of file +func spawn_label_number(new_value : int, old_value : int, battler : Battler, type : String) -> void: + """ + Spawns a damage or a mana cost animated label + Converts a difference to a string and delegates to spawn_label + """ + var message : = str(new_value - old_value) + spawn_label(message, battler, type) + +func spawn_label(message : String, battler : Battler, type : String) -> void: + """ + Spawns an animated PopupLabel + """ + var l : = PopupLabel.instance() + add_child(l) + l.start(battler, type, message) -- GitLab