Files
zeling_v2/Assets/Scripts/Core/DeathRespawnService.cs
2026-05-13 09:19:54 +08:00

84 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using BaseGames.Core.Events;
namespace BaseGames.Core
{
/// <summary>
/// 死亡/复活流程服务接口。
/// </summary>
public interface IDeathRespawnService
{
/// <summary>玩家死亡时由 GameManager 调用,启动死亡演出流程。</summary>
IEnumerator StartDeathSequenceCoroutine();
/// <summary>DeathScreen 确认按钮点击后调用,执行复活流程。</summary>
IEnumerator StartRespawnCoroutine();
/// <summary>SteelSoul 模式HP 归零后直接清档并返回主菜单。</summary>
IEnumerator StartGameOverCoroutine();
}
/// <summary>
/// 死亡/复活流程独立服务。
/// </summary>
public class DeathRespawnService : MonoBehaviour, IDeathRespawnService
{
[Header("Config")]
[SerializeField] private float _deathAnimDuration = 1.2f;
[SerializeField] private float _deathScreenDelay = 0.5f;
[SerializeField] private float _respawnFadeDuration = 0.4f;
[Header("Event Channels - Raise")]
[SerializeField] private VoidEventChannelSO _onRespawnStarted;
[SerializeField] private VoidEventChannelSO _onRespawnCompleted;
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
[Header("Event Channels - Listen")]
[SerializeField] private VoidEventChannelSO _onDeathScreenConfirmed;
public IEnumerator StartDeathSequenceCoroutine()
{
yield return new WaitForSeconds(_deathAnimDuration);
yield return new WaitForSeconds(_deathScreenDelay);
// 确认等待由 GameManager.DeathFlow 统一处理,此处仅负责动画延迟
}
public IEnumerator StartRespawnCoroutine()
{
_onRespawnStarted?.Raise();
yield return new WaitForSeconds(_respawnFadeDuration);
// 通过 SceneLoadRequest 频道触发场景重载,复用 SceneService / RoomTransition 路径
var sm = ServiceLocator.GetOrDefault<ISaveService>();
_onSceneLoadRequest?.Raise(new SceneLoadRequest
{
SceneName = sm?.LastCheckpointScene,
EntryTransitionId = sm?.LastCheckpointSpawnId,
ShowLoadingScreen = true,
IsRespawn = true,
});
yield return new WaitForSeconds(_respawnFadeDuration);
_onRespawnCompleted?.Raise();
}
public IEnumerator StartGameOverCoroutine()
{
// 1. 删除当前存档槽
var saveManager = ServiceLocator.GetOrDefault<ISaveService>();
if (saveManager != null)
{
var task = saveManager.DeleteSlotAsync(saveManager.ActiveSlot);
yield return new WaitUntil(() => task.IsCompleted);
}
// 2. 返回主菜单
var sceneService = ServiceLocator.GetOrDefault<ISceneService>();
if (sceneService != null)
yield return sceneService.LoadMainMenuCoroutine();
}
}
}