SupaLidlGame/UI/FloatingText.cs

85 lines
1.8 KiB
C#
Raw Normal View History

2022-11-25 09:11:46 -08:00
using Godot;
using System;
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.UI;
public partial class FloatingText : Node2D
2022-11-25 09:11:46 -08:00
{
2023-08-15 23:05:39 -07:00
private string _text;
2023-06-03 18:21:46 -07:00
[Export]
2023-08-15 23:05:39 -07:00
public string Text
{
get => _text;
set
{
_text = value;
if (_label is not null)
{
_label.Text = value;
}
}
}
public Timer Timer { get; set; }
protected Label _label;
protected Tween _tween;
2023-06-03 18:21:46 -07:00
public override void _Ready()
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
_label = GetNode<Label>("Label");
2023-08-15 23:05:39 -07:00
_label.Text = _text;
Timer = GetNode<Timer>("Timer");
Timer.Timeout += Die;
ShowText();
}
2022-11-25 09:11:46 -08:00
2023-08-15 23:05:39 -07:00
public void Reset()
{
Modulate = Colors.White;
Scale = Vector2.One * 0.5f;
}
public void ShowText()
{
Reset();
if (_tween is not null && _tween.IsRunning())
{
_tween.Kill();
}
_tween = GetTree().CreateTween()
2023-06-03 18:21:46 -07:00
.SetEase(Tween.EaseType.Out)
.SetTrans(Tween.TransitionType.Quint)
.SetParallel();
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
Random rng = new Random();
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
float randomFloat(float min, float max)
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
return (float)rng.NextDouble() * (max - min) + min;
2022-11-25 09:11:46 -08:00
}
2023-06-03 18:21:46 -07:00
Position += new Vector2(randomFloat(-8, 8), 0);
var endPos = Position + new Vector2(0, randomFloat(-16, -8));
var endMod = new Color(1, 1, 1, 0);
var endScale = Scale * 0.5f;
2023-08-15 23:05:39 -07:00
_tween.TweenProperty(this, "position", endPos, 0.5f);
_tween.SetTrans(Tween.TransitionType.Linear);
_tween.TweenProperty(this, "modulate", endMod, 0.5f).SetDelay(1.0f);
_tween.TweenProperty(this, "scale", endScale, 0.5f).SetDelay(1.0f);
_tween.Play();
2023-06-03 18:21:46 -07:00
}
2023-08-15 23:05:39 -07:00
public void Die()
2023-06-03 18:21:46 -07:00
{
QueueFree();
2022-11-25 09:11:46 -08:00
}
}