Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FireworksParticleSystem.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 public class FireworksParticleSystem : ParticleSystem<FireworksParticleSystem.Particle> {
6 public class Particle : Game.Particle {
8
9 public float TimeToLive;
10
11 public float FadeRate;
12
14
15 public float RotationSpeed;
16
17 public float GenerationFrequency;
18
20
21 public bool HighDamping;
22 }
23
24 public Random m_random = new();
25
26 public Color m_color;
27
28 public float m_flickering;
29
30 public int m_nextParticle;
31
32 public FireworksParticleSystem(Vector3 position, Color color, FireworksBlock.Shape shape, float flickering, float particleSize) : base(300) {
33 Texture = ContentManager.Get<Texture2D>("Textures/FireworksParticle");
34 m_color = color;
35 m_flickering = flickering;
37 if (shape == FireworksBlock.Shape.SmallBurst
38 || shape == FireworksBlock.Shape.LargeBurst) {
39 int num = shape == FireworksBlock.Shape.SmallBurst ? 100 : 200;
40 while (m_nextParticle < num) {
41 Particle particle = Particles[m_nextParticle++];
42 particle.IsActive = true;
43 particle.Position = position;
44 particle.Size = new Vector2(0.2f * particleSize);
45 particle.TimeToLive = shape == FireworksBlock.Shape.SmallBurst ? m_random.Float(0.5f, 2f) : m_random.Float(1f, 3f);
46 particle.Velocity = m_random.Vector3(0.5f, 1f);
47 particle.Velocity *= (shape == FireworksBlock.Shape.SmallBurst ? 16 : 26) * particle.Velocity.LengthSquared();
48 particle.TextureSlot = m_random.Int(0, 3);
49 particle.FadeRate = m_random.Float(1f, 3f);
50 particle.BaseColor = m_color * m_random.Float(0.5f, 1f);
51 particle.RotationSpeed = 0f;
52 }
53 }
54 switch (shape) {
55 case FireworksBlock.Shape.Circle: {
56 float num4 = m_random.Float(0f, (float)Math.PI * 2f);
57 int num5 = 150;
58 for (int j = 0; j < num5; j++) {
59 float x2 = (float)Math.PI * 2f * j / num5 + num4;
60 Vector3 v2 = new(MathF.Sin(x2) + 0.1f * m_random.Float(-1f, 1f), 0f, MathF.Cos(x2) + 0.1f * m_random.Float(-1f, 1f));
62 obj2.IsActive = true;
63 obj2.Position = position;
64 obj2.Size = new Vector2(0.2f * particleSize);
65 obj2.TimeToLive = m_random.Float(1f, 3f);
66 obj2.Velocity = 20f * v2;
67 obj2.TextureSlot = m_random.Int(0, 3);
68 obj2.FadeRate = m_random.Float(1f, 3f);
69 obj2.BaseColor = m_color * m_random.Float(0.5f, 1f);
70 obj2.RotationSpeed = 0f;
71 }
72 break;
73 }
74 case FireworksBlock.Shape.Disc: {
75 float num10 = m_random.Float(0f, (float)Math.PI * 2f);
76 int num11 = 13;
77 for (int m = 0; m <= num11; m++) {
78 float num12 = m / (float)num11;
79 int num13 = (int)MathF.Round(num12 * 2f * num11);
80 for (int n = 0; n < num13; n++) {
81 float x5 = (float)Math.PI * 2f * n / num13 + num10;
82 Vector3 v4 = new(
83 num12 * MathF.Sin(x5) + 0.1f * m_random.Float(-1f, 1f),
84 0f,
85 num12 * MathF.Cos(x5) + 0.1f * m_random.Float(-1f, 1f)
86 );
88 obj4.IsActive = true;
89 obj4.Position = position;
90 obj4.Size = new Vector2(0.2f * particleSize);
91 obj4.TimeToLive = m_random.Float(1f, 3f);
92 obj4.Velocity = 22f * v4;
93 obj4.TextureSlot = m_random.Int(0, 3);
94 obj4.FadeRate = m_random.Float(1f, 3f);
95 obj4.BaseColor = m_color * m_random.Float(0.5f, 1f);
96 obj4.RotationSpeed = 0f;
97 }
98 }
99 break;
100 }
101 case FireworksBlock.Shape.Ball: {
102 float num14 = m_random.Float(0f, (float)Math.PI * 2f);
103 int num15 = 12;
104 Vector3 v5 = default;
105 for (int num16 = 0; num16 <= num15; num16++) {
106 float x6 = (float)Math.PI * num16 / num15;
107 v5.Y = MathF.Cos(x6);
108 float num17 = MathF.Sin(x6);
109 int num18 = (int)MathF.Round(num17 * 2f * num15);
110 for (int num19 = 0; num19 < num18; num19++) {
111 float x7 = (float)Math.PI * 2f * num19 / num18 + num14;
112 v5.X = num17 * MathF.Sin(x7);
113 v5.Z = num17 * MathF.Cos(x7);
115 obj5.IsActive = true;
116 obj5.Position = position;
117 obj5.Size = new Vector2(0.2f * particleSize);
118 obj5.TimeToLive = m_random.Float(1f, 3f);
119 obj5.Velocity = 20f * v5;
120 obj5.TextureSlot = m_random.Int(0, 3);
121 obj5.FadeRate = m_random.Float(1f, 3f);
122 obj5.BaseColor = m_color * m_random.Float(0.5f, 1f);
123 obj5.RotationSpeed = 0f;
124 }
125 }
126 break;
127 }
128 case FireworksBlock.Shape.ShortTrails:
129 case FireworksBlock.Shape.LongTrails: {
130 float num6 = m_random.Float(0f, (float)Math.PI * 2f);
131 int num7 = 3;
132 Vector3 v3 = default;
133 for (int k = 0; k <= num7; k++) {
134 float x3 = (float)Math.PI * k / num7;
135 float num8 = MathF.Sin(x3);
136 int num9 = (int)MathF.Round(num8 * (shape == FireworksBlock.Shape.ShortTrails ? 3 : 2) * num7);
137 for (int l = 0; l < num9; l++) {
138 float x4 = (float)Math.PI * 2f * l / num9 + num6;
139 v3.X = num8 * MathF.Sin(x4) + 0.3f * m_random.Float(-1f, 1f);
140 v3.Y = MathF.Cos(x3) + 0.3f * m_random.Float(-1f, 1f);
141 v3.Z = num8 * MathF.Cos(x4) + 0.3f * m_random.Float(-1f, 1f);
143 obj3.IsActive = true;
144 obj3.Position = position;
145 obj3.Size = new Vector2(0.25f);
146 obj3.TimeToLive = m_random.Float(0.5f, 2.5f);
147 obj3.Velocity = shape == FireworksBlock.Shape.ShortTrails ? 25f * v3 : 35f * v3;
148 obj3.TextureSlot = m_random.Int(0, 3);
149 obj3.FadeRate = m_random.Float(1f, 3f);
150 obj3.BaseColor = m_color * m_random.Float(0.5f, 1f);
151 obj3.GenerationFrequency = shape == FireworksBlock.Shape.ShortTrails ? 1.9f : 2.1f;
152 obj3.RotationSpeed = m_random.Float(-40f, 40f);
153 }
154 }
155 break;
156 }
157 case FireworksBlock.Shape.FlatTrails: {
158 float num2 = m_random.Float(0f, (float)Math.PI * 2f);
159 int num3 = 13;
160 for (int i = 0; i < num3; i++) {
161 float x = (float)Math.PI * 2f * i / num3 + num2;
162 Vector3 v = new(MathF.Sin(x) + 0.1f * m_random.Float(-1f, 1f), 0f, MathF.Cos(x) + 0.1f * m_random.Float(-1f, 1f));
164 obj.IsActive = true;
165 obj.Position = position;
166 obj.Size = new Vector2(0.25f);
167 obj.TimeToLive = m_random.Float(0.5f, 2.5f);
168 obj.Velocity = 25f * v;
169 obj.TextureSlot = m_random.Int(0, 3);
170 obj.FadeRate = m_random.Float(1f, 3f);
171 obj.BaseColor = m_color * m_random.Float(0.5f, 1f);
172 obj.GenerationFrequency = 2.5f;
173 obj.RotationSpeed = m_random.Float(-40f, 40f);
174 }
175 break;
176 }
177 }
178 }
179
180 public override bool Simulate(float dt) {
181 dt = Math.Clamp(dt, 0f, 0.1f);
182 float num = MathF.Pow(0.01f, dt);
183 float num2 = MathF.Pow(0.1f, dt);
184 bool flag = false;
185 for (int i = 0; i < Particles.Length; i++) {
186 Particle particle = Particles[i];
187 if (!particle.IsActive) {
188 continue;
189 }
190 flag = true;
191 particle.TimeToLive -= dt;
192 if (particle.TimeToLive > 0f) {
193 Vector3 position = particle.Position += particle.Velocity * dt;
194 particle.Velocity.Y += -9.81f * dt;
195 particle.Velocity *= particle.HighDamping ? num : num2;
196 particle.Color = particle.BaseColor * MathUtils.Min(particle.FadeRate * particle.TimeToLive, 1f);
197 particle.Rotation += particle.RotationSpeed * dt;
198 if (!particle.HighDamping
199 && m_random.Float(0f, 1f) < m_flickering) {
200 particle.Color = Color.Transparent;
201 }
202 if (m_random.Float(0f, 1f) < 20f * dt) {
203 particle.TextureSlot = m_random.Int(0, 3);
204 }
205 if (particle.GenerationFrequency > 0f) {
206 float num3 = particle.Velocity.Length();
207 particle.GenerationAccumulator += particle.GenerationFrequency * num3 * dt;
208 if (particle.GenerationAccumulator > 1f
209 && m_nextParticle < Particles.Length) {
210 particle.GenerationAccumulator -= 1f;
212 obj.IsActive = true;
213 obj.Position = position;
214 obj.Size = new Vector2(0.2f);
215 obj.TimeToLive = 1f;
216 obj.TextureSlot = m_random.Int(0, 3);
217 obj.FadeRate = 1f;
218 obj.BaseColor = particle.BaseColor;
219 obj.HighDamping = true;
220 obj.RotationSpeed = 0f;
221 }
222 }
223 }
224 else {
225 particle.IsActive = false;
226 }
227 }
228 return !flag;
229 }
230 }
231}
Engine.Vector3 Vector3
static int Min(int x1, int x2)
static object Get(Type type, string name)
FireworksParticleSystem(Vector3 position, Color color, FireworksBlock.Shape shape, float flickering, float particleSize)
ParticleSystem(int particlesCount)
float Float()
static Color Transparent
定义 Color.cs:5