-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathBreakable.cs
More file actions
210 lines (178 loc) · 6.51 KB
/
Breakable.cs
File metadata and controls
210 lines (178 loc) · 6.51 KB
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
208
209
210
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]
GameObject m_BrokenPrefab;
[SerializeField]
[Tooltip("If set, will be used instead of BrokenPrefab when new players join, skipping transition effects.")]
GameObject m_PrebrokenPrefab;
[SerializeField]
[Tooltip("We use this transform's position and rotation when creating the prefab. (Defaults to self)")]
Transform m_BrokenPrefabPos;
[SerializeField]
GameObject[] m_UnbrokenGameObjects;
/// <summary>
/// Is the item broken or not?
/// </summary>
public bool IsBroken => m_NetworkHealthState.HitPoints.Value == 0;
public event Action Broken;
public bool IsNpc => true;
public bool IsValidTarget => !IsBroken;
GameObject m_CurrentBrokenVisualization;
public override void OnNetworkSpawn()
{
if (IsServer)
{
if (m_MaxHealth && m_NetworkHealthState)
{
m_NetworkHealthState.HitPoints.Value = m_MaxHealth.Value;
}
}
if (IsClient)
{
if (m_NetworkHealthState)
{
m_NetworkHealthState.HitPoints.OnValueChanged += OnHPChanged;
}
if (IsBroken)
{
PerformBreakVisualization(true);
}
}
}
public override void OnNetworkDespawn()
{
if (IsClient && m_NetworkHealthState)
{
m_NetworkHealthState.HitPoints.OnValueChanged -= OnHPChanged;
}
}
public void ReceiveHitPoints(ServerCharacter inflicter, int hitPoints)
{
if (hitPoints < 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_MaxHealth)
{
m_NetworkHealthState.HitPoints.Value =
Mathf.Clamp(m_NetworkHealthState.HitPoints.Value + hitPoints, 0, m_MaxHealth.Value);
}
}
}
public int GetTotalDamage()
{
return Math.Max(0, m_MaxHealth.Value - m_NetworkHealthState.HitPoints.Value);
}
public void Break()
{
m_NetworkHealthState.HitPoints.Value = 0;
}
public void Unbreak()
{
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;
}
void OnHPChanged(int previousValue, int newValue)
{
if (IsServer)
{
if (m_Collider)
{
m_Collider.enabled = !IsBroken;
}
}
if (previousValue > 0 && newValue >= 0)
{
Broken?.Invoke();
PerformBreakVisualization(false);
}
else if (previousValue == 0 && newValue > 0)
{
PerformUnbreakVisualization();
}
}
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);
}
}
void PerformUnbreakVisualization()
{
if (m_CurrentBrokenVisualization)
{
Destroy(m_CurrentBrokenVisualization);
}
foreach (var unbrokenGameObject in m_UnbrokenGameObjects)
{
if (unbrokenGameObject)
{
unbrokenGameObject.SetActive(true);
}
}
}
#if UNITY_EDITOR
void OnValidate()
{
if (!m_Collider)
m_Collider = GetComponent<Collider>();
if (!m_NetworkHealthState)
m_NetworkHealthState = GetComponent<NetworkHealthState>();
if (!m_BrokenPrefabPos)
m_BrokenPrefabPos = transform;
}
#endif
}
}