-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleEffectManager.cs
236 lines (185 loc) · 7.38 KB
/
ParticleEffectManager.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
230
231
232
233
234
235
236
using AssetBundleLoader;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ultramove
{
public class ParticleEffectManager : MonoBehaviour
{
public static ParticleEffectManager Instance
{
get
{
if (_instance == null)
{
_instance = new GameObject("ParticleEffectManager").AddComponent<ParticleEffectManager>();
_instance.Init();
}
return _instance;
}
}
private static ParticleEffectManager _instance;
private Queue<ParticleSystem> particlePool = new Queue<ParticleSystem>();
private const int PoolSize = 10; // Adjust based on expected usage
ParticleSystem particleFall;
ParticleSystem particleDash;
private void Init()
{
particleFall = Instantiate(BundleLoader.LoadAssetBundle(BundleLoader.GetDefaultModAssetBundlePath("ultrakill")).LoadAsset<GameObject>("FallParticle")).GetComponent<ParticleSystem>();
particleDash = Instantiate(BundleLoader.LoadAssetBundle(BundleLoader.GetDefaultModAssetBundlePath("ultrakill")).LoadAsset<GameObject>("DashParticle")).GetComponent<ParticleSystem>();
// Pre-populate the pool with reusable particle systems
for (int i = 0; i < PoolSize; i++)
{
particlePool.Enqueue(CreateParticleSystem());
}
InitGlint();
}
public void PlaySlam(Vector3 pos, bool on)
{
particleFall.transform.position = pos;
if (particleFall.isPlaying != on)
{
particleFall.Clear();
if (on)
particleFall.Play();
else
particleFall.Stop();
}
}
public void PlayDash(Vector3 pos, Vector3 dir, bool on)
{
particleDash.transform.position = pos;
if (dir != Vector3.zero)
particleDash.transform.rotation = Quaternion.LookRotation(dir);
if (particleDash.isPlaying != on)
{
if (on)
particleDash.Play();
else
particleDash.Stop();
}
}
public void PlayEffectCoinRicochet(Transform tr)
{
ParticleSystem particleSystem = GetParticleSystem();
SetEffectCoinRicochet(particleSystem);
particleSystem.transform.position = tr.position;
particleSystem.transform.rotation = tr.rotation;
particleSystem.gameObject.SetActive(true);
particleSystem.Play();
StartCoroutine(RecycleAfterFinish(particleSystem));
}
private void SetEffectCoinRicochet(ParticleSystem ps)
{
var main = ps.main;
main.loop = false;
main.startColor = Color.yellow;
main.startSize = 0.05f;
main.startSpeed = new ParticleSystem.MinMaxCurve(3f, 6f);
main.startLifetime = new ParticleSystem.MinMaxCurve(0.1f, 0.5f);
main.simulationSpace = ParticleSystemSimulationSpace.Local;
main.gravityModifier = 0;
var limit = ps.limitVelocityOverLifetime;
limit.drag = 200;
limit.enabled = true;
var emission = ps.emission;
emission.rateOverTime = 0f; // No continuous emission
var burst = new ParticleSystem.Burst(0f, 20);
emission.SetBursts(new ParticleSystem.Burst[] { burst });
var shape = ps.shape;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = 0.1f;
shape.angle = 50;
shape.scale = Vector3.one;
var trails = ps.trails;
trails.enabled = false;
trails.worldSpace = false;
}
ParticleSystem GetParticleSystem()
{
if (particlePool.Count > 0)
{
return particlePool.Dequeue();
}
else
{
return CreateParticleSystem();
}
}
private ParticleSystem CreateParticleSystem()
{
GameObject bloodEffect = new GameObject("ParticleEffect");
ParticleSystem ps = bloodEffect.AddComponent<ParticleSystem>();
var particleRenderer = bloodEffect.GetComponent<ParticleSystemRenderer>();
Material trailMaterial = new Material(Shader.Find("Sprites/Default"));
particleRenderer.material = trailMaterial;
particleRenderer.trailMaterial = trailMaterial;
particleRenderer.trailMaterial.color = Color.white;
ps.Stop();
return ps;
}
private System.Collections.IEnumerator RecycleAfterFinish(ParticleSystem ps)
{
yield return new WaitForSeconds(ps.main.startLifetime.constantMax);
ps.transform.SetParent(null);
ps.Stop();
particlePool.Enqueue(ps);
}
public void LightExplosion(Vector3 pos)
{
StartCoroutine(AnimLight(pos));
}
IEnumerator AnimLight(Vector3 pos)
{
float t = 0f;
Light newLight = new GameObject("GlintLight").AddComponent<Light>();
newLight.shadows = LightShadows.Hard;
newLight.range = 11f;
newLight.transform.position = pos + new Vector3(0, 0.3f, 0f);
newLight.color = new Color(1f, 0.31f, 0f);
while (t < 1f)
{
t += Time.deltaTime * 4f;
float e = 1f - Mathf.Pow(1f - t / 1f, 3f);
newLight.intensity = Mathf.Lerp(50f, 0f, e);
yield return null;
}
GameObject.Destroy(newLight);
}
void InitGlint()
{
glint = Instantiate(BundleLoader.LoadAssetBundle(BundleLoader.GetDefaultModAssetBundlePath("ultrakill")).LoadAsset<GameObject>("glint")).GetComponentInChildren<MeshRenderer>();
glint.material = new Material(Shader.Find("Sprites/Default"));
glint.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
glintLight = new GameObject("GlintLight").AddComponent<Light>();
}
MeshRenderer glint;
Light glintLight;
Coroutine animGlint;
public void PlayGlint(Vector3 pos, Color color, float duration)
{
if (animGlint != null)
StopCoroutine(animGlint);
animGlint = StartCoroutine(AnimGlint(pos, color, duration));
}
IEnumerator AnimGlint(Vector3 pos, Color color, float duration)
{
float t = 0f;
glint.transform.position = pos;
glint.transform.rotation = Random.rotation;
glint.material.color = color;
glintLight.transform.position = pos;
glintLight.shadows = LightShadows.None;
glintLight.range = 4f;
glintLight.color = color;
while (t < duration)
{
t += Time.deltaTime;
float e = 1f - Mathf.Pow(1f - t / duration, 3f);
glint.transform.localScale = Vector3.Lerp(Vector3.one * 0.08f, Vector3.zero, e);
glintLight.intensity = Mathf.Lerp(7f, 0f, e);
yield return null;
}
}
}
}