-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHealth.cs
229 lines (178 loc) · 6.47 KB
/
Health.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using Grubs.Equipment.Gadgets.Ground;
using Grubs.Equipment.Gadgets.Projectiles;
using Grubs.Gamemodes;
using Grubs.Helpers;
using Grubs.Pawn;
using Grubs.Systems.GameMode;
using Grubs.Systems.Pawn;
using Grubs.Systems.Pawn.Grubs;
namespace Grubs.Common;
public partial class Health : Component
{
[Property] public float MaxHealth { get; set; }
[Sync] public float CurrentHealth { get; set; }
[Sync] public bool DeathInvoked { get; set; } = false;
[Sync] public bool HasBeenDamaged { get; set; }
public delegate void Death();
public event Death ObjectDied;
public delegate void Damaged( GrubsDamageInfo damageInfo );
public event Damaged ObjectDamaged;
private Queue<GrubsDamageInfo> DamageQueue { get; set; } = new();
private DeathReason _deathReason;
protected override void OnStart()
{
CurrentHealth = MaxHealth;
}
public void TakeDamage( GrubsDamageInfo damageInfo, bool immediate = false )
{
if ( Components.TryGet( out Grub grub ) )
{
if ( !grub.IsValid() )
return;
if ( BaseGameMode.Current.IsValid() && BaseGameMode.Current.IsGrubActive( grub ) )
BaseGameMode.Current.GrubDamaged( grub );
if ( !immediate )
{
QueueDamage( damageInfo, grub );
return;
}
}
CurrentHealth -= damageInfo.Damage;
ObjectDamaged?.Invoke( damageInfo );
if ( CurrentHealth <= 0 && !DeathInvoked )
{
var isKillZoneDeath = damageInfo.Tags.Has( "killzone" );
if ( isKillZoneDeath )
{
var damageInfos = new List<GrubsDamageInfo>();
var lastReason = DamageQueue.LastOrDefault();
if ( !lastReason.Equals( default( GrubsDamageInfo ) ) ) // Get previous damage context if it exists.
damageInfos.Add( lastReason );
damageInfos.Add( damageInfo );
_deathReason = DeathReason.FindReason( grub, damageInfos );
if ( grub.IsValid() )
WorldPopupHelper.Instance.CreateKillZoneDeathIndicator( damageInfo.WorldPosition );
}
if ( grub.IsValid() )
{
Log.Info( $"{grub.Name} died, handling death." );
GrubDiedRpc( grub );
}
_ = OnDeath( isKillZoneDeath );
}
}
private void QueueDamage( GrubsDamageInfo damageInfo, Grub grub )
{
if ( Networking.IsHost && BaseGameMode.Current.IsValid() )
BaseGameMode.Current?.GrubDamaged( grub );
HasBeenDamaged = true;
DamageQueue.Enqueue( damageInfo );
}
[Rpc.Host]
private void GrubDamagedRpc( Grub grub )
{
BaseGameMode.Current.GrubDamaged( grub );
}
[Rpc.Host]
private void GrubDiedRpc( Grub grub )
{
BaseGameMode.Current.GrubDied( grub );
}
/// <summary>
/// Will dequeue DamageQueue until empty and apply any damage to CurrentHealth.
/// </summary>
[Rpc.Owner]
public void ApplyDamage()
{
if ( DamageQueue.Count == 0 )
{
Log.Info( "Nothing in DamageQueue." );
return;
}
HasBeenDamaged = false;
var totalDamage = 0f;
var damageInfos = new List<GrubsDamageInfo>();
while ( DamageQueue.TryDequeue( out var info ) )
{
totalDamage += info.Damage;
damageInfos.Add( info );
}
if ( totalDamage >= CurrentHealth && Components.TryGet( out Grub grub ) )
_deathReason = DeathReason.FindReason( grub, damageInfos );
WorldPopupHelper.Instance.CreateDamagePopup( GameObject.Id, totalDamage );
TakeDamage( new GrubsDamageInfo( totalDamage, Guid.Empty ), true );
}
public void Heal( float heal )
{
CurrentHealth += heal;
WorldPopupHelper.Instance.CreateDamagePopup( GameObject.Id, -heal );
}
private async Task OnDeath( bool deleteImmediately = false )
{
if ( Components.TryGet( out Grub grub ) )
{
if ( !grub.IsValid() )
return;
if ( !deleteImmediately )
{
await GameTask.Delay( 500 ); // Give clients some time to update GrubTag healthbar to 0 before we play death animation.
DeathInvoked = true;
// Double check that grub is still valid, since we have waited 500ms since fetching the component.
if ( !grub.IsValid() )
return;
// This is shit, especially since we want a variety of death animations in the future.
// Just don't know where to put this right now.
var plungerPrefab = ResourceLibrary.Get<PrefabFile>( "prefabs/world/dynamite_plunger.prefab" );
var position = grub.WorldPosition;
var plunger = SceneUtility.GetPrefabScene( plungerPrefab ).Clone();
plunger.NetworkSpawn();
plunger.WorldPosition = grub.PlayerController.Facing == -1 ? position - new Vector3( 30, 0, 0 ) : position;
await GameTask.Delay( 750 );
if ( !grub.IsValid() )
return;
// Same as above.
DeathEffects( position );
if ( _deathReason.FromDisconnect )
ExplosionHelper.Instance.Explode( this, position, 40f, 15f, grub.Id, grub.Name );
else
ExplosionHelper.Instance.Explode( this, position, 75f, 25f, grub.Id, grub.Name );
plunger?.Destroy();
var gravePrefab = ResourceLibrary.Get<PrefabFile>( "prefabs/world/drops/gravestone.prefab" );
var grave = SceneUtility.GetPrefabScene( gravePrefab ).Clone();
grave.NetworkSpawn();
grave.WorldPosition = position.WithY( 536f ).WithZ( position.z + 24 );
}
ChatHelper.Instance.SendInfoMessage( _deathReason.ToString() );
var attackerGuid = _deathReason.SecondInfo.AttackerGuid;
if ( _deathReason.FromKillTrigger ) // If we've died to a kill trigger, check if we have additional damage to credit, otherwise we've attacked ourselves.
attackerGuid = _deathReason.FirstReason != DamageType.None ? _deathReason.FirstInfo.AttackerGuid : grub.Id;
// var attacker = Scene.GetAllComponents<Player>().FirstOrDefault( p => p.Grubs.Contains( attackerGuid ) );
// var connection = attacker?.Network.Owner;
// using ( Rpc.FilterInclude( connection ) )
// {
// if ( grub.Owner.IsValid() )
// Stats.IncrementGrubsKilled( grub.Owner.Id );
// }
if ( grub.Owner?.Inventory.IsValid() ?? false )
grub.Owner.Inventory.HolsterActive();
grub.GameObject.Destroy();
}
ObjectDied?.Invoke();
DeathInvoked = true;
if ( Components.TryGet( out ExplosiveProjectile explosive ) && explosive.ExplodeOnDeath )
{
explosive.Explode();
}
if ( Components.TryGet( out ProximityExplosive proximity, FindMode.EverythingInSelfAndChildren ) && proximity.DetonateOnDeath && !proximity.IsDetonating )
{
proximity.StartDetonating();
}
}
[Rpc.Broadcast]
private void DeathEffects( Vector3 position )
{
var sceneParticles = ParticleHelper.Instance.PlayInstantaneous( ParticleSystem.Load( "particles/explosion/grubs_explosion_base.vpcf" ), Transform.World );
sceneParticles.SetControlPoint( 1, new Vector3( 100f / 2f, 0, 0 ) );
Sound.Play( "explosion_short_tail", position );
}
}