-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerAnimationHandler.cs
68 lines (60 loc) · 2.22 KB
/
ServerAnimationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections;
using Unity.BossRoom.Gameplay.Configuration;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
{
public class ServerAnimationHandler : NetworkBehaviour
{
[SerializeField]
NetworkAnimator m_NetworkAnimator;
[SerializeField]
VisualizationConfiguration m_VisualizationConfiguration;
[SerializeField]
NetworkLifeState m_NetworkLifeState;
public NetworkAnimator NetworkAnimator => m_NetworkAnimator;
public override void OnNetworkSpawn()
{
if (IsServer)
{
// Wait until next frame before registering on OnValueChanged to make sure NetworkAnimator has spawned before.
StartCoroutine(WaitToRegisterOnLifeStateChanged());
}
}
IEnumerator WaitToRegisterOnLifeStateChanged()
{
yield return new WaitForEndOfFrame();
m_NetworkLifeState.LifeState.OnValueChanged += OnLifeStateChanged;
if (m_NetworkLifeState.LifeState.Value != LifeState.Alive)
{
OnLifeStateChanged(LifeState.Alive, m_NetworkLifeState.LifeState.Value);
}
}
void OnLifeStateChanged(LifeState previousValue, LifeState newValue)
{
switch (newValue)
{
case LifeState.Alive:
NetworkAnimator.SetTrigger(m_VisualizationConfiguration.AliveStateTriggerID);
break;
case LifeState.Fainted:
NetworkAnimator.SetTrigger(m_VisualizationConfiguration.FaintedStateTriggerID);
break;
case LifeState.Dead:
NetworkAnimator.SetTrigger(m_VisualizationConfiguration.DeadStateTriggerID);
break;
default:
throw new ArgumentOutOfRangeException(nameof(newValue), newValue, null);
}
}
public override void OnNetworkDespawn()
{
if (IsServer)
{
m_NetworkLifeState.LifeState.OnValueChanged -= OnLifeStateChanged;
}
}
}
}