您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

53 行
1.7 KiB

using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Netcode;
using UnityEngine;
using VContainer;
namespace Unity.Multiplayer.Samples.BossRoom.Server
{
/// <summary>
/// Server-only component which publishes a message once the LifeState changes.
/// </summary>
[RequireComponent(typeof(NetworkLifeState))]
public class PublishMessageOnLifeChange : NetworkBehaviour
{
[SerializeField]
NetworkLifeState m_NetworkLifeState;
[SerializeField]
string m_CharacterName;
[SerializeField]
CharacterClassContainer m_CharacterClass;
NetworkNameState m_NameState;
[Inject]
IPublisher<LifeStateChangedEventMessage> m_Publisher;
public override void OnNetworkSpawn()
{
if (IsServer)
{
m_NameState = GetComponent<NetworkNameState>();
m_NetworkLifeState.LifeState.OnValueChanged += OnLifeStateChanged;
var gameState = FindObjectOfType<ServerBossRoomState>();
if (gameState != null)
{
gameState.Container.Inject(this);
}
}
}
void OnLifeStateChanged(LifeState previousState, LifeState newState)
{
m_Publisher.Publish(new LifeStateChangedEventMessage()
{
CharacterName = m_NameState != null ? m_NameState.Name.Value : (FixedPlayerName)m_CharacterName,
CharacterType = m_CharacterClass.CharacterClass.CharacterType,
NewLifeState = newState
});
}
}
}