summaryrefslogtreecommitdiff
path: root/ROUNDS/GeneralParticleSystem.cs
blob: 8f3203a60b4451e0a6a14cb4799794afcd7f8a65 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System.Collections;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class GeneralParticleSystem : MonoBehaviour
{
	public GameObject particleObject;

	public bool useTimeScale;

	private ObjectPool particlePool;

	[FoldoutGroup("Spawn settings", 0)]
	public float randomXPos;

	[FoldoutGroup("Spawn settings", 0)]
	public float randomYPos;

	[FoldoutGroup("Spawn settings", 0)]
	public float rate = 10f;

	[FoldoutGroup("Spawn settings", 0)]
	public bool playOnAwake = true;

	[FoldoutGroup("Spawn settings", 0)]
	public bool playOnEnablee;

	[FoldoutGroup("Spawn settings", 0)]
	public bool loop;

	[FoldoutGroup("Spawn settings", 0)]
	public float duration = 2f;

	[FoldoutGroup("Spawn settings", 0)]
	public AnimationCurve sizeMultiplierOverTime = AnimationCurve.Linear(0f, 1f, 1f, 1f);

	public AnimationCurve emissionMultiplierOverTime = AnimationCurve.Linear(0f, 1f, 1f, 1f);

	public ObjectParticle particleSettings;

	[FoldoutGroup("Global settings", 0)]
	public float simulationSpeed = 1f;

	[HideInInspector]
	public float simulationSpeedMultiplier = 1f;

	[FoldoutGroup("Global settings", 0)]
	public float saturationMultiplier = 1f;

	[FoldoutGroup("Events", 0)]
	public UnityEvent startEvent;

	[FoldoutGroup("Events", 0)]
	public float startEventDelay;

	[FoldoutGroup("Events", 0)]
	public UnityEvent endEvent;

	[FoldoutGroup("Events", 0)]
	public float endEventDelay;

	private float sizeOverTimeAnimationCurveLength;

	private float sizeMultiplierOverTimeAnimationCurveLength;

	private float alphaOverTimeAnimationCurveLength;

	private float emissionOverTimeAnimationCurveLength;

	private bool inited;

	private Coroutine emissionLoop;

	private float lastEmissionTime;

	private bool isPlaying;

	private void OnEnable()
	{
		isPlaying = false;
		if (playOnEnablee)
		{
			Play();
		}
	}

	private void Start()
	{
		if (playOnAwake)
		{
			Play();
		}
		Mask component = base.transform.parent.GetComponent<Mask>();
		if ((bool)component)
		{
			component.showMaskGraphic = false;
		}
	}

	private void Init()
	{
		if (!inited)
		{
			inited = true;
			if (particleSettings.sizeOverTime.keys.Length > 1)
			{
				sizeOverTimeAnimationCurveLength = particleSettings.sizeOverTime.keys[particleSettings.sizeOverTime.keys.Length - 1].time - particleSettings.sizeOverTime.keys[0].time;
			}
			if (particleSettings.alphaOverTime.keys.Length > 1)
			{
				alphaOverTimeAnimationCurveLength = particleSettings.alphaOverTime.keys[particleSettings.alphaOverTime.keys.Length - 1].time - particleSettings.alphaOverTime.keys[0].time;
			}
			if (sizeMultiplierOverTime.keys.Length > 1)
			{
				sizeMultiplierOverTimeAnimationCurveLength = sizeMultiplierOverTime.keys[sizeMultiplierOverTime.keys.Length - 1].time - sizeMultiplierOverTime.keys[0].time;
			}
			if (emissionMultiplierOverTime.keys.Length > 1)
			{
				emissionOverTimeAnimationCurveLength = emissionMultiplierOverTime.keys[emissionMultiplierOverTime.keys.Length - 1].time - emissionMultiplierOverTime.keys[0].time;
			}
			particleObject.SetActive(value: false);
			particlePool = new ObjectPool(particleObject, 100, base.transform);
		}
	}

	public void StartLooping()
	{
		loop = true;
		Play();
	}

	public void EndLooping()
	{
		loop = false;
		if (emissionLoop != null)
		{
			StopCoroutine(emissionLoop);
		}
	}

	[Button]
	public void Play()
	{
		Init();
		if (!isPlaying)
		{
			emissionLoop = StartCoroutine(DoPlay());
		}
	}

	private void OnDisable()
	{
		Stop();
	}

	public void Stop()
	{
		isPlaying = false;
		DisableAllParticles();
		if (emissionLoop != null)
		{
			StopCoroutine(emissionLoop);
		}
	}

	private void DisableAllParticles()
	{
		if (base.transform.childCount > 0)
		{
			for (int i = 0; i < base.transform.GetChild(0).childCount; i++)
			{
				base.transform.GetChild(0).GetChild(i).gameObject.SetActive(value: false);
			}
		}
	}

	private IEnumerator DoPlay()
	{
		isPlaying = true;
		if (startEvent != null)
		{
			if (startEventDelay != 0f)
			{
				StartCoroutine(DelayEvent(startEvent, startEventDelay));
			}
			else
			{
				startEvent.Invoke();
			}
		}
		float counter = 0f;
		while (counter < duration)
		{
			CheckIfShouldEmit(counter / duration);
			counter += (useTimeScale ? TimeHandler.deltaTime : Time.unscaledDeltaTime) * (simulationSpeed * simulationSpeedMultiplier);
			yield return null;
		}
		isPlaying = false;
		if (loop)
		{
			Play();
		}
		else if (endEvent != null)
		{
			if (endEventDelay != 0f)
			{
				StartCoroutine(DelayEvent(endEvent, endEventDelay));
			}
			else
			{
				endEvent.Invoke();
			}
		}
	}

	private void CheckIfShouldEmit(float currentAnimationTime)
	{
		if ((useTimeScale ? Time.time : Time.unscaledTime) > lastEmissionTime + 1f / rate / (simulationSpeed * simulationSpeedMultiplier) / emissionMultiplierOverTime.Evaluate(currentAnimationTime * emissionOverTimeAnimationCurveLength) * Time.timeScale)
		{
			StartCoroutine(DoPlarticleLife(currentAnimationTime));
			lastEmissionTime = Time.time;
		}
	}

	private IEnumerator DoPlarticleLife(float currentAnimationTime)
	{
		GameObject spawned = particlePool.GetObject();
		float counter = 0f;
		float t = particleSettings.lifetime;
		Vector3 startSize = spawned.transform.localScale;
		Vector3 modifiedStartSize = spawned.transform.localScale * particleSettings.size * sizeMultiplierOverTime.Evaluate(currentAnimationTime * sizeMultiplierOverTimeAnimationCurveLength);
		Image img = spawned.GetComponent<Image>();
		Color startColor = Color.magenta;
		if ((bool)img)
		{
			startColor = img.color;
		}
		if ((bool)img)
		{
			float value = Random.value;
			if (particleSettings.color != Color.magenta)
			{
				img.color = particleSettings.color;
			}
			if (particleSettings.randomColor != Color.magenta)
			{
				img.color = Color.Lerp(img.color, particleSettings.randomColor, value);
			}
			if (!particleSettings.singleRandomValueColor)
			{
				value = Random.value;
			}
			if (particleSettings.randomAddedColor != Color.black)
			{
				img.color += Color.Lerp(Color.black, particleSettings.randomAddedColor, value);
			}
			if (!particleSettings.singleRandomValueColor)
			{
				value = Random.value;
			}
			if (particleSettings.randomAddedSaturation != 0f || saturationMultiplier != 1f)
			{
				Color.RGBToHSV(img.color, out var H, out var S, out var V);
				S += value * particleSettings.randomAddedSaturation;
				S *= saturationMultiplier;
				img.color = Color.HSVToRGB(H, S, V);
			}
		}
		spawned.transform.Rotate(base.transform.forward * particleSettings.rotation);
		spawned.transform.Rotate(base.transform.forward * Random.Range(0f - particleSettings.randomRotation, particleSettings.randomRotation));
		spawned.transform.localPosition = Vector3.zero;
		spawned.transform.position += base.transform.up * Random.Range(0f - randomYPos, randomYPos);
		spawned.transform.position += base.transform.right * Random.Range(0f - randomXPos, randomXPos);
		spawned.transform.position += base.transform.forward * Random.Range(-0.1f, 0.1f);
		while (counter < t)
		{
			if (particleSettings.sizeOverTime.keys.Length > 1)
			{
				spawned.transform.localScale = modifiedStartSize * particleSettings.sizeOverTime.Evaluate(counter / t * sizeOverTimeAnimationCurveLength);
			}
			float num = particleSettings.alphaOverTime.Evaluate(counter / t * alphaOverTimeAnimationCurveLength);
			if ((bool)img && img.color.a != num)
			{
				img.color = new Color(img.color.r, img.color.g, img.color.b, num);
			}
			counter += (useTimeScale ? TimeHandler.deltaTime : Time.unscaledDeltaTime) * (simulationSpeed * simulationSpeedMultiplier);
			yield return null;
		}
		if ((bool)img)
		{
			img.color = startColor;
		}
		spawned.transform.localScale = startSize;
		particlePool.ReleaseObject(spawned);
	}

	private IEnumerator DelayEvent(UnityEvent e, float t)
	{
		yield return new WaitForSeconds(t);
		e?.Invoke();
	}
}