Game/GameText.cs

A GameText component that wraps a TextRenderer to set text and scale, orienting the text to face the local player's camera during gameplay. Includes a generic factory CreateTextObject<T> that clones a text prefab and initializes it. Also defines DamageText, a GameText subclass that sets red color, moves upward each fixed update, and self-destructs after a short lifetime.

File Access
using Forp.Object;
using Forp.Util;
using Sandbox.Diagnostics;
using System;
using System.Collections.Generic;
using System.Text;

namespace Forp.Game;

public class GameText : Component
{
	[RequireComponent] protected TextRenderer TextRenderer { get; set; }

	public void SetText(string Text)
	{
		TextRenderer.Text = Text;
	}

	public void SetScale(float Scale)
	{
		TextRenderer.Scale = Scale;
	}

	protected override void OnStart()
	{
		base.OnStart();
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		if (GameManager.Instance.Mode != EGameManagerMode.Menu) // meh
		{
			Vector3 Direction = WorldPosition - GamePlayer.Local.Camera.WorldPosition;
			TextRenderer.WorldRotation = Rotation.LookAt(Direction);
		}
	}

	public static T CreateTextObject<T>(Transform Transform, string Text = "") where T : GameText
	{
		var TextPrefab = GameManager.Instance.GetTextPrefab<T>();
		Assert.NotNull(TextPrefab);

		var ConstructionClone = TextPrefab.Clone();
		ConstructionClone.WorldTransform = Transform;

		var TextObject = ConstructionClone.GetComponent<T>();
		TextObject.SetText($"{Text}");

		return TextObject;
	}
}

public sealed class DamageText : GameText
{
	private int TicksAlive = 0;

	protected override void OnStart()
	{
		base.OnStart();

		TextRenderer.Color = Color.Red;
	}

	protected override void OnFixedUpdate()
	{
		base.OnFixedUpdate();

		WorldPosition += Vector3.Up * 5;
		TicksAlive++;

		if (TicksAlive > 50)
		{
			DestroyGameObject();
		}
	}
}