From e6a888acc3ebe60e81b43dd1b2b78ed165f86283 Mon Sep 17 00:00:00 2001 From: Kvel2D Date: Sun, 5 Mar 2023 17:55:54 +0100 Subject: [PATCH] Update tweens to new API Modify FloatingText tweens slightly --- Scenes/FloatingText.gd | 72 ++++++++++++++++++---------------------- Scenes/FloatingText.tscn | 3 +- Scenes/Mobs/Mob.gd | 27 ++++++++------- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/Scenes/FloatingText.gd b/Scenes/FloatingText.gd index f6ee69a5f..1f377751e 100644 --- a/Scenes/FloatingText.gd +++ b/Scenes/FloatingText.gd @@ -3,42 +3,36 @@ extends Node2D # TODO: update to new tween API -#@onready var tween: Tween = $Tween -#@onready var label: Label = $Label -# -#var text: String = "placeholder" -#var duration: float = 1.0 -#var color: Color = Color.WHITE -# -# -#func _ready(): -# label.text = text -# modulate = color -# -## Text pops out at the start -# tween.interpolate_property(self, "scale", -# Vector2(0, 0), -# Vector2(1.0, 1.0), -# 0.3 * duration, Tween.TRANS_QUART, Tween.EASE_OUT) -# -## Text fades to transparent after popping out -# tween.interpolate_property(self, "modulate", -# color, -# Color(color.r, color.g, color.b, 0), -# 0.3 * duration, Tween.TRANS_LINEAR, Tween.EASE_OUT, 0.7 * duration) -# -## Text shrinks while fading -# tween.interpolate_property(self, "scale", -# Vector2(1.0, 1.0), -# Vector2(0.4, 0.4), -# 1.0 * duration, Tween.TRANS_LINEAR, Tween.EASE_OUT, 0.6 * duration) -# -## Text bounces -# tween.interpolate_property(self, "position", -# Vector2(0, 0), -# Vector2(50, -50), -# 1.0 * duration, Tween.TRANS_SINE, Tween.EASE_OUT) -# -# tween.interpolate_callback(self, 1.0 * duration, "queue_free") -# -# tween.start() +@onready var label: Label = $Label +var text: String = "placeholder" +var duration: float = 1.0 +var color: Color = Color.WHITE + +func _ready(): + label.text = text + modulate = color + +# Text bounces up and right + var pos_tween = create_tween() + pos_tween.tween_property(self, "position", + Vector2(50, -50), + 1.0 * duration).set_trans(Tween.TRANS_SINE) + +# Text pops out of nothing then shrinks + var scale_tween = create_tween() + scale = Vector2(0, 0) + scale_tween.tween_property(self, "scale", + Vector2(1.0, 1.0), + 0.3 * duration).set_trans(Tween.TRANS_QUART) + scale_tween.tween_property(self, "scale", + Vector2(0.4, 0.4), + 0.7 * duration).set_trans(Tween.TRANS_LINEAR) + +# Text fades away to nothing at the end + var modulate_tween = create_tween() + modulate_tween.tween_property(self, "modulate", + Color(modulate.r, modulate.g, modulate.b, 0), + 0.3 * duration).set_trans(Tween.TRANS_LINEAR).set_delay(0.7 * duration) + + var queue_free_tween = create_tween() + queue_free_tween.tween_callback(queue_free).set_delay(1.0) diff --git a/Scenes/FloatingText.tscn b/Scenes/FloatingText.tscn index 39fc9fca4..4d119764e 100644 --- a/Scenes/FloatingText.tscn +++ b/Scenes/FloatingText.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=2 format=3 uid="uid://b6jet2t80ftgv"] +[gd_scene load_steps=2 format=3 uid="uid://vgeirnu7ramn"] [ext_resource type="Script" path="res://Scenes/FloatingText.gd" id="2"] [node name="FloatingText" type="Node2D"] -scale = Vector2(1e-05, 1e-05) script = ExtResource("2") [node name="Label" type="Label" parent="."] diff --git a/Scenes/Mobs/Mob.gd b/Scenes/Mobs/Mob.gd index c50d3729e..3b9f7f013 100644 --- a/Scenes/Mobs/Mob.gd +++ b/Scenes/Mobs/Mob.gd @@ -12,6 +12,7 @@ const MOVE_SPEED_MIN: float = 100.0 const MOVE_SPEED_MAX: float = 500.0 const DEFAULT_MOVE_SPEED: float = MOVE_SPEED_MAX const SELECTION_SIZE: int = 64 +const HEIGHT_TWEEN_FAST_FORWARD_DELTA: float = 100.0 var _path_curve: Curve2D var _current_path_index: int = 0 @@ -19,6 +20,7 @@ var _size: int = Unit.MobSize.NORMAL var _category: int = Unit.MobCategory.HUMANOID var movement_enabled: bool = true var _facing_angle: float = 0.0 +var _height_tween: Tween = null @onready var _visual = $Visual @@ -103,22 +105,23 @@ func on_damaged(_event: Event): _health_bar.set_as_ratio(_health / MOB_HEALTH_MAX) -# TODO: update to new tween API func adjust_height(height: float, speed: float): - pass - # if _height_tween.is_active(): - # var tween_runtime: float = _height_tween.get_runtime() - # _height_tween.seek(tween_runtime) - # _height_tween.remove_all() +# If a tween is already running, complete it instantly +# before starting new one. + if _height_tween != null: + if _height_tween.is_running(): + _height_tween.custom_step(HEIGHT_TWEEN_FAST_FORWARD_DELTA) - # var duration: float = abs(height / speed) + _height_tween.kill() + _height_tween = null - # _height_tween.interpolate_property(_visual, "position", - # _visual.position, - # Vector2(_visual.position.x, _visual.position.y - height), - # duration, Tween.TRANS_LINEAR, Tween.EASE_OUT) + _height_tween = create_tween() - # _height_tween.start() + var duration: float = abs(height / speed) + + _height_tween.tween_property(_visual, "position", + Vector2(_visual.position.x, _visual.position.y - height), + duration).set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_OUT) func _get_mob_animation() -> String: