batching damage text

pull/6/head
John Montagu, the 4th Earl of Sandvich 2023-08-15 23:05:39 -07:00
parent 673bebcabf
commit b18b011133
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
5 changed files with 101 additions and 25 deletions

View File

@ -104,6 +104,8 @@ public partial class Character : CharacterBody2D, IFaction
public AnimationPlayer AttackAnimation { get; set; } public AnimationPlayer AttackAnimation { get; set; }
private UI.DamageText _curDamageText;
public override void _Ready() public override void _Ready()
{ {
// TODO: 80+ char line // TODO: 80+ char line
@ -268,6 +270,20 @@ public partial class Character : CharacterBody2D, IFaction
float knockback, float knockback,
Vector2 knockbackDir = default) => damage; Vector2 knockbackDir = default) => damage;
protected void CreateDamageText(float damage)
{
// create damage text
var textScene = GD.Load<PackedScene>("res://UI/DamageText.tscn");
if (_curDamageText is null || !IsInstanceValid(_curDamageText))
{
_curDamageText = textScene.Instantiate<UI.DamageText>();
this.GetWorld().CurrentMap.AddChild(_curDamageText);
}
_curDamageText.Damage += damage;
_curDamageText.Timer.Start();
_curDamageText.GlobalPosition = GlobalPosition;
_curDamageText.ShowText();
}
protected virtual void OnReceivedDamage( protected virtual void OnReceivedDamage(
float damage, float damage,
@ -281,7 +297,8 @@ public partial class Character : CharacterBody2D, IFaction
} }
float oldHealth = Health; float oldHealth = Health;
Health -= ReceiveDamage(damage, inflictor, knockback, knockbackDir); damage = ReceiveDamage(damage, inflictor, knockback, knockbackDir);
Health -= damage;
var hurtParticles = GetNode<GpuParticles2D>("Effects/HurtParticles"); var hurtParticles = GetNode<GpuParticles2D>("Effects/HurtParticles");
if (hurtParticles is not null) if (hurtParticles is not null)
@ -289,15 +306,9 @@ public partial class Character : CharacterBody2D, IFaction
hurtParticles.SetDirection(knockbackDir); hurtParticles.SetDirection(knockbackDir);
} }
// create damage text CreateDamageText(damage);
var textScene = GD.Load<PackedScene>("res://UI/FloatingText.tscn");
var instance = textScene.Instantiate<UI.FloatingText>();
instance.Text = Mathf.Round(damage).ToString();
instance.GlobalPosition = GlobalPosition;
this.GetAncestor<TileMap>().AddChild(instance);
// apply knockback // apply knockback
ApplyImpulse(knockbackDir.Normalized() * knockback); ApplyImpulse(knockbackDir.Normalized() * knockback);
// play damage animation // play damage animation

18
UI/DamageText.cs 100644
View File

@ -0,0 +1,18 @@
using Godot;
namespace SupaLidlGame.UI;
public partial class DamageText : FloatingText
{
private float _damage = 0;
public float Damage
{
get => _damage;
set
{
_damage = value;
Text = Mathf.Round(value).ToString();
}
}
}

View File

@ -0,0 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://b05ormmtofbw1"]
[ext_resource type="PackedScene" uid="uid://be80n7mxp62jd" path="res://UI/FloatingText.tscn" id="1_dggpv"]
[ext_resource type="Script" path="res://UI/DamageText.cs" id="2_psgc1"]
[node name="FloatingText" instance=ExtResource("1_dggpv")]
z_index = 16
script = ExtResource("2_psgc1")

View File

@ -5,17 +5,55 @@ namespace SupaLidlGame.UI;
public partial class FloatingText : Node2D public partial class FloatingText : Node2D
{ {
private Label _label; private string _text;
[Export] [Export]
public string Text { get; set; } 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;
public override void _Ready() public override void _Ready()
{ {
_label = GetNode<Label>("Label"); _label = GetNode<Label>("Label");
_label.Text = Text; _label.Text = _text;
Tween tween = GetTree().CreateTween() Timer = GetNode<Timer>("Timer");
Timer.Timeout += Die;
ShowText();
}
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()
.SetEase(Tween.EaseType.Out) .SetEase(Tween.EaseType.Out)
.SetTrans(Tween.TransitionType.Quint) .SetTrans(Tween.TransitionType.Quint)
.SetParallel(); .SetParallel();
@ -27,23 +65,19 @@ public partial class FloatingText : Node2D
return (float)rng.NextDouble() * (max - min) + min; return (float)rng.NextDouble() * (max - min) + min;
} }
GD.Print(GlobalPosition);
Position += new Vector2(randomFloat(-8, 8), 0); Position += new Vector2(randomFloat(-8, 8), 0);
var endPos = Position + new Vector2(0, randomFloat(-16, -8)); var endPos = Position + new Vector2(0, randomFloat(-16, -8));
var endMod = new Color(1, 1, 1, 0); var endMod = new Color(1, 1, 1, 0);
var endScale = Scale * 0.5f; var endScale = Scale * 0.5f;
tween.TweenProperty(this, "position", endPos, 0.5f); _tween.TweenProperty(this, "position", endPos, 0.5f);
tween.SetTrans(Tween.TransitionType.Linear); _tween.SetTrans(Tween.TransitionType.Linear);
tween.TweenProperty(this, "modulate", endMod, 0.5f).SetDelay(1.0f); _tween.TweenProperty(this, "modulate", endMod, 0.5f).SetDelay(1.0f);
tween.TweenProperty(this, "scale", endScale, 0.5f).SetDelay(1.0f); _tween.TweenProperty(this, "scale", endScale, 0.5f).SetDelay(1.0f);
tween.TweenCallback(new Callable(this, nameof(OnTweenFinished))) _tween.Play();
.SetDelay(2.5f);
base._Ready();
} }
public void OnTweenFinished() public void Die()
{ {
QueueFree(); QueueFree();
} }

View File

@ -5,10 +5,11 @@
[sub_resource type="LabelSettings" id="LabelSettings_gs6ch"] [sub_resource type="LabelSettings" id="LabelSettings_gs6ch"]
font = ExtResource("1_yns15") font = ExtResource("1_yns15")
font_color = Color(1, 0.792157, 0, 1) font_color = Color(0.941176, 0.843137, 0.470588, 1)
outline_size = 2 outline_size = 2
outline_color = Color(0.635294, 0.415686, 0, 1) outline_color = Color(0.713726, 0.372549, 0.105882, 1)
shadow_color = Color(0, 0, 0, 1) shadow_size = 2
shadow_color = Color(0.176471, 0.0901961, 0.172549, 1)
[node name="FloatingText" type="Node2D"] [node name="FloatingText" type="Node2D"]
scale = Vector2(0.5, 0.5) scale = Vector2(0.5, 0.5)
@ -24,3 +25,7 @@ text = "gruppa krovi"
label_settings = SubResource("LabelSettings_gs6ch") label_settings = SubResource("LabelSettings_gs6ch")
horizontal_alignment = 1 horizontal_alignment = 1
vertical_alignment = 1 vertical_alignment = 1
[node name="Timer" type="Timer" parent="."]
wait_time = 2.5
autostart = true