Files
2026-06-22 16:18:34 +02:00

79 lines
2.1 KiB
C#

using UnityEngine;
namespace Ashwild.Player
{
[DisallowMultipleComponent]
public class PlayerLifecycle : MonoBehaviour
{
[Header("Spawn point (auto-captured at scene start if left empty)")]
[SerializeField] private Transform spawnPoint;
private PlayerLocomotion locomotion;
private Rigidbody rb;
private Vector3 spawnPosition;
private Quaternion spawnRotation;
private bool isDead;
private void Awake()
{
locomotion = GetComponent<PlayerLocomotion>();
rb = GetComponent<Rigidbody>();
}
private void Start()
{
if (spawnPoint != null)
{
spawnPosition = spawnPoint.position;
spawnRotation = spawnPoint.rotation;
}
else
{
spawnPosition = transform.position;
spawnRotation = transform.rotation;
}
}
private void OnEnable() { PlayerEvents.Died += OnDied; }
private void OnDisable() { PlayerEvents.Died -= OnDied; }
private void OnDied()
{
if (isDead) return;
isDead = true;
if (locomotion != null) locomotion.SetDead(true);
}
public void Respawn()
{
if (rb != null)
{
rb.linearVelocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
transform.position = spawnPosition;
transform.rotation = spawnRotation;
if (PlayerStats.Instance != null) PlayerStats.Instance.Revive();
if (locomotion != null) locomotion.SetDead(false);
isDead = false;
}
public void SetSpawnPoint(Vector3 position, Quaternion rotation)
{
spawnPosition = position;
spawnRotation = rotation;
}
public void SetSpawnPoint(Transform t)
{
if (t == null) return;
spawnPoint = t;
spawnPosition = t.position;
spawnRotation = t.rotation;
}
}
}