Add DamageNumberManager and DamageNumber for displaying damage feedback
This commit is contained in:
41
scripts/UI/DamageNumber.cs
Normal file
41
scripts/UI/DamageNumber.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Globalization;
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class DamageNumber : Label
|
||||
{
|
||||
[Export] public float Duration { get; set; } = 0.8f;
|
||||
[Export] public float FallDistance { get; set; } = 40f;
|
||||
[Export] public float HorizontalDrift { get; set; } = 15f;
|
||||
|
||||
public void ShowDamage(float damageAmount, Vector2 position)
|
||||
{
|
||||
Text = Mathf.Round(damageAmount * 100f).ToString(CultureInfo.InvariantCulture);
|
||||
GlobalPosition = position;
|
||||
|
||||
var rng = new RandomNumberGenerator();
|
||||
var horizontalOffset = rng.RandfRange(-HorizontalDrift, HorizontalDrift);
|
||||
|
||||
var startPosition = GlobalPosition;
|
||||
var endPosition = GlobalPosition + new Vector2(horizontalOffset, FallDistance);
|
||||
|
||||
var startColor = Colors.White;
|
||||
startColor.A = 1f;
|
||||
Modulate = startColor;
|
||||
|
||||
var tween = CreateTween();
|
||||
tween.SetParallel();
|
||||
|
||||
tween.TweenProperty(this, "global_position", endPosition, Duration)
|
||||
.SetTrans(Tween.TransitionType.Quad)
|
||||
.SetEase(Tween.EaseType.In);
|
||||
|
||||
tween.Chain().TweenProperty(this, "modulate:a", 0f, Duration * 0.5f)
|
||||
.SetTrans(Tween.TransitionType.Sine)
|
||||
.SetEase(Tween.EaseType.Out);
|
||||
|
||||
tween.TweenCallback(Callable.From(QueueFree));
|
||||
}
|
||||
}
|
1
scripts/UI/DamageNumber.cs.uid
Normal file
1
scripts/UI/DamageNumber.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bbupymh6krrgx
|
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -12,7 +13,15 @@ public partial class HealthComponent : Node2D
|
||||
|
||||
[Signal] public delegate void HealthChangedEventHandler(float delta, float totalHealth);
|
||||
[Signal] public delegate void DeathEventHandler();
|
||||
|
||||
private DamageNumberManager _damageNumberManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_damageNumberManager = GetNode<DamageNumberManager>("/root/DamageNumberManager");
|
||||
_damageNumberManager?.Register(Owner);
|
||||
}
|
||||
|
||||
public void SetHealth(float newValue)
|
||||
{
|
||||
_ = ApplyHealthChange(newValue);
|
||||
|
Reference in New Issue
Block a user