/// <summary> /// 更新粒子物理属性,如果粒子超过边界,将其回收 /// </summary> /// <param name="elapsedTime"></param> /// <param name="size"></param> public void Update(float elapsedTime, Vector2 size) { { if (surfaceLoaded) { for (int i = ActiveParticles.Count - 1; i >= 0; i--) { var p = ActiveParticles[i]; if (p == null) { ActiveParticles.RemoveAt(i); return; } if (p.Position.X > 0 - size.Y * (float)Math.Tan(1.5708 - (minRotationAngle + maxRotationAngle) / 2) && p.Position.X <= size.X && p.Position.Y <= size.Y) { p.Update(elapsedTime); } else { ActiveParticles.RemoveAt(i); FreeParticles.Push(p); } } } } }
public override void AddParticles(Vector2 size) { if (surfaceLoaded) { if (!inited) { InitSmoke(size); } // 添加的数量 var m = (int)(size.X * minDensity); var n = (int)(size.X * maxDensity); var actualAdd = Tools.Random.Next(m, n) - ActiveParticles.Count; if (actualAdd < 0) { actualAdd = 0; } // 激活这些粒子 for (int i = 0; i < actualAdd; i++) { // 从空闲粒子堆里取粒子,如果没有,那么就 new 一个 Particle particle = (FreeParticles.Count > 0) ? FreeParticles.Pop() : new Particle(); particle.Key = Tools.Random.Next(0, smokeSurfaceName[flag].Length); var where = new Vector2(0 - (float)(surfacesBounds[particle.Key].Width / 2), Tools.RandomBetween(0 - (float)(surfacesBounds[particle.Key].Height / 2), size.Y / 5 - (float)(surfacesBounds[particle.Key].Height / 2))); // 初始化粒子参数 InitializeParticle(particle, where); // 将此粒子加入激活粒子队列 if (ActiveParticles.Capacity <= ActiveParticles.Count) { ActiveParticles.Capacity = ActiveParticles.Count * 2; } ActiveParticles.Add(particle); } } }
internal void ImmersiveOut() { inited = false; for (int i = ActiveParticles.Count - 1; i >= 0; i--) { var p = ActiveParticles[i]; ActiveParticles.RemoveAt(i); FreeParticles.Push(p); } isImmersive = false; }
/// <summary> /// 获得画布尺寸,在画布顶部生成粒子 /// </summary> /// <param name="size"></param> public void AddRainDrop(Vector2 size) { { if (surfaceLoaded) { numParticles += (Tools.RandomBetween(minDensity, maxDensity) * size.X); var actualAdd = (int)numParticles; numParticles %= 1; for (int i = 0; i < actualAdd; i++) { Particle particle = (FreeParticles.Count > 0) ? FreeParticles.Pop() : new Particle(); float x = Tools.RandomBetween(0 - size.Y * (float)Math.Tan(1.5708 - (minRotationAngle + maxRotationAngle) / 2), size.X); InitializeParticle(particle, new Vector2(x, -5)); if (ActiveParticles.Capacity <= ActiveParticles.Count) { ActiveParticles.Capacity = ActiveParticles.Count * 2; } ActiveParticles.Add(particle); } } } }
/// <summary> /// 更新粒子物理属性,如果粒子超过边界,将其回收 /// </summary> /// <param name="elapsedTime"></param> /// <param name="size"></param> public void Update(float elapsedTime, Vector2 size) { if (surfaceLoaded) { for (int i = ActiveParticles.Count - 1; i >= 0; i--) { var p = ActiveParticles[i]; if (p == null) { ActiveParticles.RemoveAt(i); return; } if (p.Position.X < (size.X + (surfacesBounds[p.Key].Width) / 2)) { p.Update(elapsedTime); } else { ActiveParticles.RemoveAt(i); FreeParticles.Push(p); } } } }
private void InitSmoke(Vector2 size) { var m = (int)(size.X * minDensity); var n = (int)(size.X * maxDensity); var add = Tools.Random.Next(m, n); for (int i = 0; i < add; i++) { // 从空闲粒子堆里取粒子,如果没有,那么就 new 一个 Particle particle = (FreeParticles.Count > 0) ? FreeParticles.Pop() : new Particle(); particle.Key = Tools.Random.Next(0, smokeSurfaceName[flag].Length); var where = new Vector2(Tools.RandomBetween(0 - (float)(surfacesBounds[particle.Key].Width / 2), size.X + (float)(surfacesBounds[particle.Key].Width / 2)), Tools.RandomBetween((0 - (float)(surfacesBounds[particle.Key].Height / 2)), size.Y / 5 - (float)(surfacesBounds[particle.Key].Height / 2))); // 初始化粒子参数 InitializeParticle(particle, where); // 将此粒子加入激活粒子队列 if (ActiveParticles.Capacity <= ActiveParticles.Count) { ActiveParticles.Capacity = ActiveParticles.Count * 2; } ActiveParticles.Add(particle); } inited = true; }