batching damage text

controller-support
HumanoidSandvichDispenser 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; }
private UI.DamageText _curDamageText;
public override void _Ready()
{
// TODO: 80+ char line
@ -268,6 +270,20 @@ public partial class Character : CharacterBody2D, IFaction
float knockback,
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(
float damage,
@ -281,7 +297,8 @@ public partial class Character : CharacterBody2D, IFaction
}
float oldHealth = Health;
Health -= ReceiveDamage(damage, inflictor, knockback, knockbackDir);
damage = ReceiveDamage(damage, inflictor, knockback, knockbackDir);
Health -= damage;
var hurtParticles = GetNode<GpuParticles2D>("Effects/HurtParticles");
if (hurtParticles is not null)
@ -289,15 +306,9 @@ public partial class Character : CharacterBody2D, IFaction
hurtParticles.SetDirection(knockbackDir);
}
// create damage text
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);
CreateDamageText(damage);
// apply knockback
ApplyImpulse(knockbackDir.Normalized() * knockback);
// 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
{
private Label _label;
private string _text;
[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()
{
_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)
.SetTrans(Tween.TransitionType.Quint)
.SetParallel();
@ -27,23 +65,19 @@ public partial class FloatingText : Node2D
return (float)rng.NextDouble() * (max - min) + min;
}
GD.Print(GlobalPosition);
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;
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.TweenCallback(new Callable(this, nameof(OnTweenFinished)))
.SetDelay(2.5f);
base._Ready();
_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();
}
public void OnTweenFinished()
public void Die()
{
QueueFree();
}

View File

@ -5,10 +5,11 @@
[sub_resource type="LabelSettings" id="LabelSettings_gs6ch"]
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_color = Color(0.635294, 0.415686, 0, 1)
shadow_color = Color(0, 0, 0, 1)
outline_color = Color(0.713726, 0.372549, 0.105882, 1)
shadow_size = 2
shadow_color = Color(0.176471, 0.0901961, 0.172549, 1)
[node name="FloatingText" type="Node2D"]
scale = Vector2(0.5, 0.5)
@ -24,3 +25,7 @@ text = "gruppa krovi"
label_settings = SubResource("LabelSettings_gs6ch")
horizontal_alignment = 1
vertical_alignment = 1
[node name="Timer" type="Timer" parent="."]
wait_time = 2.5
autostart = true