File size: 4,915 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
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
class FlashbangEffect
{
	protected const float 						ALPHA_MIN = 0.0;
	protected const float 						ALPHA_MAX = 1.0;
	
	protected const float 						SOUND_DEFER_TIME = 0.4;			//! SFX will be played ~0.5s AFTER VFX 

	protected float 							m_HitDuration;
	protected float 							m_BreakPoint;
	protected float 							m_TimeActive;
	protected float								m_DayTimeToggle;
	
	protected float								m_AlphaMaxActual; //actual max alpha of the effect
	protected float								m_SoundMaxActual; //actual max volume of the sound
	protected float								m_ProgressMultiplier; 
	
	protected bool								m_Visual;
	protected bool 								m_Initialized;
	
	protected PlayerBase 						m_Player;
	protected EffectSound 						m_FlashbangEffectSound;
	protected float 							m_SoundStopTime;
	
	protected ref Timer							m_DeferAttenuation;
	
	protected PPERequester_FlashbangEffects 	m_Requester;
	
	void FlashbangEffect(PlayerBase player, bool visual = true)
	{
		m_Player = player;
		m_Visual = visual;
		m_Initialized = false;

		m_HitDuration = 8.0;
		m_BreakPoint = 2.5;
		m_AlphaMaxActual = ALPHA_MAX;
		m_SoundMaxActual = 1.0;
		m_ProgressMultiplier = 1.0;
		
		m_FlashbangEffectSound = null;
		
		if (m_Visual)
		{
			Class.CastTo(m_Requester,PPERequesterBank.GetRequester(PPERequester_FlashbangEffects));
			m_Requester.Start();
		}
		
		m_DeferAttenuation = new ref Timer();
		m_DeferAttenuation.Run(SOUND_DEFER_TIME, this, "PlaySound", null, false);
		
		//! naive time of the day selector
		m_DayTimeToggle = 5; //! -1: night; 1: day
		if ( g_Game.GetDayTime() >= 22.0 || g_Game.GetDayTime() < 7.0 )
		{
			m_DayTimeToggle = 10;
		}
	}
	
	void ~FlashbangEffect()
	{
		if ( m_Visual )
		{
			ClearVisual();
		}
		
		if ( m_Player )
		{
			m_Player.OnPlayerReceiveFlashbangHitEnd();
		}

		if ( m_DeferAttenuation.IsRunning() )
		{
			m_DeferAttenuation.Stop();
		}
		
		m_DeferAttenuation = null;
		SEffectManager.DestroyEffect(m_FlashbangEffectSound);
	}
	
	void SetupFlashbangValues(float progress_mult = 1.0, float visual_value_max = 1.0, float sound_value_max = 1.0)
	{
		if ( !m_Initialized )
		{
			m_Initialized = true;
			m_ProgressMultiplier = progress_mult;
			m_AlphaMaxActual = visual_value_max;
			m_SoundMaxActual = sound_value_max;
			
			m_HitDuration *= m_ProgressMultiplier;
			m_BreakPoint *= m_ProgressMultiplier;
		}
	}
	
	protected void PlaySound()
	{
		if ( !m_Initialized )
		{
			Error("" + this + " not initialized");
			return;
		}
		
		vector pos;
		MiscGameplayFunctions.GetHeadBonePos(m_Player, pos);
		
		if (!m_FlashbangEffectSound)
		{
			m_FlashbangEffectSound = SEffectManager.CreateSound("Tinnitus_SoundSet", pos);
		}
		
		if (!m_FlashbangEffectSound.IsPlaying())
		{
			m_FlashbangEffectSound.SetParent(m_Player);
			m_FlashbangEffectSound.SetAttachedLocalPos(m_Player.WorldToModel(pos));
			m_FlashbangEffectSound.SetSoundWaveKind(WaveKind.WAVEEFFECTEX);
			m_FlashbangEffectSound.SetSoundFadeIn(4 * Math.Clamp(m_ProgressMultiplier,0.5,1.0)); //TODO
			m_SoundStopTime = 2 * Math.Clamp(m_ProgressMultiplier,0.5,1.0);
			m_FlashbangEffectSound.SetSoundFadeOut(m_SoundStopTime); //TODO
			m_FlashbangEffectSound.SetSoundMaxVolume(Math.Clamp(m_SoundMaxActual,0.1,1.0)); //TODO
			m_FlashbangEffectSound.SetSoundLoop(true);
			m_FlashbangEffectSound.SoundPlay();
			m_FlashbangEffectSound.SetAutodestroy(true);
			
			SetAttenuationFilter();
		}
	}

	protected void SetAttenuationFilter()
	{
		if ( !m_DeferAttenuation.IsRunning() || m_Player.GetMasterAttenuation() != "FlashbangAttenuation" )
		{
			m_Player.SetMasterAttenuation("FlashbangAttenuation");
		}
	}
	
	protected void ResetAttenuationFilter()
	{
		m_Player.SetMasterAttenuation("");
	}
	
	protected void StopSound()
	{
		if (m_FlashbangEffectSound)
		{
			m_FlashbangEffectSound.SoundStop();
			SEffectManager.DestroyEffect(m_FlashbangEffectSound);
		}
	}
	
	protected void ClearVisual()
	{
		if (m_Requester)
		{
			m_Requester.Stop();
		}
	}
	
	protected void SetVisual(float val)
	{
		if (m_Requester && m_Requester.IsRequesterRunning())
		{
			m_Requester.SetFlashbangIntensity(val, m_DayTimeToggle);
		}
	}
	
	void Stop()
	{
		StopSound();
	}
	
	void Update(float deltatime)
	{
		if ( !m_Initialized )
		{
			Error("" + this + " not initialized");
		}
		else if ( m_Visual )
		{
			float value;

			if ( m_TimeActive <= m_BreakPoint )
			{
				value = m_AlphaMaxActual;
				//Print("Flashbango | m_AlphaMaxActual: " + value);
			}
			else
			{
				value = Math.InverseLerp(m_HitDuration - m_BreakPoint, m_HitDuration, m_TimeActive);
				value = Math.Clamp(value,0.0,1.0);
				value = m_AlphaMaxActual - value * m_AlphaMaxActual;
				//Print("Flashbango | tmp_value: " + value);
			}
			SetVisual(value);
		}

		m_TimeActive += deltatime;

		if (m_TimeActive >= m_HitDuration - m_SoundStopTime)
		{
			StopSound();
		}
		
		if (m_TimeActive >= m_HitDuration)
		{
			ResetAttenuationFilter();
			delete this;
		}
	}
}