-
Notifications
You must be signed in to change notification settings - Fork 503
/
Breakable.cs
208 lines (177 loc) · 6.55 KB
/
Breakable.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.BossRoom.Infrastructure;
using Unity.Netcode;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects
{
/// <summary>
/// This script handles the logic for a simple "single-shot" breakable object like a pot, or
/// other stationary items with arbitrary amounts of HP, like spawner-portal crystals.
/// Visualization for these objects works by swapping a "broken" prefab at the moment of breakage. The broken prefab
/// then handles the pesky details of actually falling apart.
/// </summary>
public class Breakable : NetworkBehaviour, IDamageable, ITargetable
{
[Header("Server Logic")]
[SerializeField]
[Tooltip("If left blank, this breakable effectively has 1 hit point")]
IntVariable m_MaxHealth;
[SerializeField]
[Tooltip("If this breakable will have hit points, add a NetworkHealthState component to this GameObject")]
NetworkHealthState m_NetworkHealthState;
[SerializeField]
Collider m_Collider;
[SerializeField]
[Tooltip("Indicate which special interaction behaviors are needed for this breakable")]
IDamageable.SpecialDamageFlags m_SpecialDamageFlags;
[Header("Visualization")]
[SerializeField]
private GameObject m_BrokenPrefab;
[SerializeField]
[Tooltip("If set, will be used instead of BrokenPrefab when new players join, skipping transition effects.")]
private GameObject m_PrebrokenPrefab;
[SerializeField]
[Tooltip("We use this transform's position and rotation when creating the prefab. (Defaults to self)")]
private Transform m_BrokenPrefabPos;
[SerializeField]
private GameObject[] m_UnbrokenGameObjects;
/// <summary>
/// Is the item broken or not?
/// </summary>
public NetworkVariable<bool> IsBroken;
public bool IsNpc { get { return true; } }
public bool IsValidTarget { get { return !IsBroken.Value; } }
private GameObject m_CurrentBrokenVisualization;
public override void OnNetworkSpawn()
{
if (IsServer)
{
if (m_MaxHealth && m_NetworkHealthState)
{
m_NetworkHealthState.HitPoints.Value = m_MaxHealth.Value;
}
}
if (IsClient)
{
IsBroken.OnValueChanged += OnBreakableStateChanged;
if (IsBroken.Value == true)
{
PerformBreakVisualization(true);
}
}
}
public override void OnNetworkDespawn()
{
if (IsClient)
{
IsBroken.OnValueChanged -= OnBreakableStateChanged;
}
}
public void ReceiveHP(ServerCharacter inflicter, int HP)
{
if (HP < 0)
{
if (inflicter && !inflicter.IsNpc)
{
bool isNotDamagedByPlayers = (GetSpecialDamageFlags() & IDamageable.SpecialDamageFlags.NotDamagedByPlayers) == IDamageable.SpecialDamageFlags.NotDamagedByPlayers;
if (isNotDamagedByPlayers)
{
// a player tried to damage us, but we are immune to player damage!
return;
}
}
if (m_NetworkHealthState)
{
m_NetworkHealthState.HitPoints.Value =
Mathf.Clamp(m_NetworkHealthState.HitPoints.Value + HP, 0, m_MaxHealth.Value);
if (m_NetworkHealthState.HitPoints.Value <= 0)
{
Break();
}
}
else
{
//any damage at all is enough to slay me.
Break();
}
}
}
private void Break()
{
IsBroken.Value = true;
if (m_Collider)
m_Collider.enabled = false;
}
public void Unbreak()
{
IsBroken.Value = false;
if (m_Collider)
m_Collider.enabled = true;
if (m_MaxHealth && m_NetworkHealthState)
m_NetworkHealthState.HitPoints.Value = m_MaxHealth.Value;
}
public IDamageable.SpecialDamageFlags GetSpecialDamageFlags()
{
return m_SpecialDamageFlags;
}
public bool IsDamageable()
{
// you can damage this breakable until it's broken!
return !IsBroken.Value;
}
private void OnBreakableStateChanged(bool wasBroken, bool isBroken)
{
if (!wasBroken && isBroken)
{
PerformBreakVisualization(false);
}
else if (wasBroken && !isBroken)
{
PerformUnbreakVisualization();
}
}
private void PerformBreakVisualization(bool onStart)
{
foreach (var unbrokenGameObject in m_UnbrokenGameObjects)
{
if (unbrokenGameObject)
{
unbrokenGameObject.SetActive(false);
}
}
if (m_CurrentBrokenVisualization)
Destroy(m_CurrentBrokenVisualization); // just a safety check, should be null when we get here
GameObject brokenPrefab = (onStart && m_PrebrokenPrefab != null) ? m_PrebrokenPrefab : m_BrokenPrefab;
if (brokenPrefab)
{
m_CurrentBrokenVisualization = Instantiate(brokenPrefab, m_BrokenPrefabPos.position, m_BrokenPrefabPos.rotation, transform);
}
}
private void PerformUnbreakVisualization()
{
if (m_CurrentBrokenVisualization)
{
Destroy(m_CurrentBrokenVisualization);
}
foreach (var unbrokenGameObject in m_UnbrokenGameObjects)
{
if (unbrokenGameObject)
{
unbrokenGameObject.SetActive(true);
}
}
}
#if UNITY_EDITOR
private void OnValidate()
{
if (!m_Collider)
m_Collider = GetComponent<Collider>();
if (!m_NetworkHealthState)
m_NetworkHealthState = GetComponent<NetworkHealthState>();
if (!m_BrokenPrefabPos)
m_BrokenPrefabPos = transform;
}
#endif
}
}