File size: 4,461 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class DestructionEffectBase
{
	EntityAI 		m_Entity;
	
	bool 			m_EntityIsTakeable;
	
	ParticleSource 	m_POneTime;
	ParticleSource 	m_PPersistent;
	
	int 			m_ParticleOneTime;
	int 			m_ParticlePersistent;
	
	EffectSound 	m_SOneTime;
	EffectSound 	m_SPersistent;
	
	string 			m_SoundSetOneTime;
	string 			m_SoundSetPersistent;

	bool 			m_KeepHealthOnReplace;
	string 			m_ReplaceWithEntity;
	int 			m_ReplaceDelay;
		
	bool 			m_HasExplosionDamage;
	DamageType		m_DamageType;
	string 			m_AmmoType;
	
	
	void ~DestructionEffectBase()
	{
		if (m_POneTime)
		{
			m_POneTime.Stop();
		}
		if (m_PPersistent)
		{
			m_PPersistent.Stop();
		}
		
		SEffectManager.DestroyEffect(m_SOneTime);
		SEffectManager.DestroyEffect(m_SPersistent);
	}
	
	private void Init();
	
	bool HasExplosionDamage()
	{
		return (m_HasExplosionDamage && m_AmmoType);
	}
	
	private void DealExplosionDamage()
	{
		DamageSystem.ExplosionDamage(m_Entity, null, m_AmmoType, m_Entity.GetPosition(), m_DamageType);
	}
	
	void OnHealthLevelChanged(notnull EntityAI entity, int oldLevel, int newLevel, string zone)
	{
		m_Entity = entity;
		Init();
		
		if (GetGame().IsServer())
		{
			entity.SetTakeable(m_EntityIsTakeable);
			
			if (oldLevel != -1 || entity.m_Initialized)
			{
				if (m_ReplaceWithEntity)
				{
					GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(ReplaceEntityServer, m_ReplaceDelay, false);
				}
				if (HasExplosionDamage())
				{
					DealExplosionDamage();
				}
				OnEntityDestroyedOneTimeServer(entity, oldLevel, zone);
			}
			OnEntityDestroyedPersistentServer(entity, zone);
		}
		#ifndef SERVER//client OR single
		{
			if (oldLevel != -1 || entity.m_Initialized)//one time
			{
				m_POneTime = PlayParticle(m_ParticleOneTime);
				
				if (m_POneTime)
				{
					m_POneTime.SetOwner(this);
				}
								
				OnEntityDestroyedOneTimeClient(entity, oldLevel, zone);
				
				m_Entity.PlaySoundSet(m_SOneTime, m_SoundSetOneTime, 0, 0 );
				m_Entity.PlaySoundSetLoop(m_SPersistent, m_SoundSetPersistent, 0, 0 );
			}
			else//Persistent
			{
				OnEntityDestroyedPersistentClient(entity, zone);
				m_Entity.PlaySoundSetLoop(m_SPersistent, m_SoundSetPersistent, 0, 0 );
			}
			
			m_PPersistent = PlayParticle(m_ParticlePersistent, true);
			
			if (m_PPersistent)
			{
				m_PPersistent.SetOwner(this);
			}
		}
		#endif
	}
	
	private void ReplaceEntityServer()
	{
		EntityAI dead_entity = EntityAI.Cast(GetGame().CreateObjectEx(m_ReplaceWithEntity, m_Entity.GetPosition(), ECE_OBJECT_SWAP, RF_ORIGINAL));
		dead_entity.SetOrientation(m_Entity.GetOrientation());
		if (m_KeepHealthOnReplace)
		{
			dead_entity.SetHealth(m_Entity.GetHealth());
		}
		m_Entity.Delete();
	}
	
	private ParticleSource PlayParticle(int particleType, bool attach = false)
	{
		if (!m_Entity)
		{
			ErrorEx("Missing entity - something went wrong");
			return null;
		}
		if (particleType)
		{
			ParticleSource p = ParticleManager.GetInstance().PlayInWorld(particleType, m_Entity.GetPosition());
			if (attach && p)
			{
				p.AddAsChild(m_Entity);//Note: it's also possible to directly play on object: Particle.PlayOnObject
			}
			return p;
		}
		return null;
	}
	
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	// !!!!!!!!!! Override methods bellow !!!!!!!!!!!!!
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
	// ! Client Event called the moment the entity is destroyed, best for explosion and other one-time effects
	void OnEntityDestroyedOneTimeClient(EntityAI entity, int oldLevel,string zone);

	// ! Server Event called the moment the entity is destroyed
	void OnEntityDestroyedOneTimeServer(EntityAI entity, int oldLevel, string zone);

	// ! Client Event called at the same moment as the one-time event, but also when the entity is loaded/spawned on client, typically as the player connects to the server or moves close enough so that previously non-existent client entity is now spawned in
	void OnEntityDestroyedPersistentClient(EntityAI entity, string zone);

	// ! Server Event called at the same moment as the one time event, but also when the entity is loaded/spawned on Server, typically as the server is starting up
	void OnEntityDestroyedPersistentServer(EntityAI entity, string zone);

	// !Relayed from entity when explosion happens
	void OnExplosionEffects(Object source, Object directHit, int componentIndex, string surface, vector pos, vector surfNormal, float energyFactor, float explosionFactor, bool isWater, string ammoType);

}