From b18b011133ed07fdd7624c3ca33c90fd1fb937d8 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Tue, 15 Aug 2023 23:05:39 -0700 Subject: [PATCH] batching damage text --- Characters/Character.cs | 27 ++++++++++++------ UI/DamageText.cs | 18 ++++++++++++ UI/DamageText.tscn | 8 ++++++ UI/FloatingText.cs | 62 +++++++++++++++++++++++++++++++---------- UI/FloatingText.tscn | 11 ++++++-- 5 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 UI/DamageText.cs create mode 100644 UI/DamageText.tscn diff --git a/Characters/Character.cs b/Characters/Character.cs index eb82f37..7546533 100644 --- a/Characters/Character.cs +++ b/Characters/Character.cs @@ -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("res://UI/DamageText.tscn"); + if (_curDamageText is null || !IsInstanceValid(_curDamageText)) + { + _curDamageText = textScene.Instantiate(); + 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("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("res://UI/FloatingText.tscn"); - var instance = textScene.Instantiate(); - instance.Text = Mathf.Round(damage).ToString(); - instance.GlobalPosition = GlobalPosition; - this.GetAncestor().AddChild(instance); + CreateDamageText(damage); // apply knockback - ApplyImpulse(knockbackDir.Normalized() * knockback); // play damage animation diff --git a/UI/DamageText.cs b/UI/DamageText.cs new file mode 100644 index 0000000..4800ab7 --- /dev/null +++ b/UI/DamageText.cs @@ -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(); + } + } +} diff --git a/UI/DamageText.tscn b/UI/DamageText.tscn new file mode 100644 index 0000000..d684740 --- /dev/null +++ b/UI/DamageText.tscn @@ -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") diff --git a/UI/FloatingText.cs b/UI/FloatingText.cs index 6ccb235..8c4d73e 100644 --- a/UI/FloatingText.cs +++ b/UI/FloatingText.cs @@ -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