Component for players that listens to a Vida component death event, plays an optional death sound, teleports the entity to a random SpawnPoint in the scene, and then calls Revivir on the Vida component.
namespace SBH;
using Sandbox;
using System.Linq;
/// <summary>
/// Al morir la Vida de este GameObject, lo teletransporta a un
/// SpawnPoint aleatorio y lo revive. Para jugadores.
/// </summary>
public sealed class RespawnAlMorir : Component
{
[RequireComponent] Vida Vida { get; set; }
[Property, Description( "Sonido al morir (opcional)" )]
public SoundEvent SonidoMuerte { get; set; }
protected override void OnEnabled()
{
Vida.AlMorir += Respawnear;
}
protected override void OnDisabled()
{
Vida.AlMorir -= Respawnear;
}
void Respawnear()
{
if ( SonidoMuerte != null )
Sound.Play( SonidoMuerte, WorldPosition );
var spawns = Scene.GetAllComponents<SpawnPoint>().ToList();
if ( spawns.Count > 0 )
{
var spawn = Game.Random.FromList( spawns );
WorldPosition = spawn.WorldPosition;
}
Vida.Revivir();
Log.Info( "💀 Jugador muerto → respawn con vida completa" );
}
}