ParticleState.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //---------------------------------------------------------------------------------
  2. // Written by Michael Hoffman
  3. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  4. //----------------------------------------------------------------------------------
  5. using System;
  6. using AtomicEngine;
  7. namespace AtomicBlaster
  8. {
  9. public enum ParticleType { None, Enemy, Bullet, IgnoreGravity }
  10. public struct ParticleState
  11. {
  12. public Vector2 Velocity;
  13. public ParticleType Type;
  14. public float LengthMultiplier;
  15. private static Random rand = new Random();
  16. public ParticleState(Vector2 velocity, ParticleType type, float lengthMultiplier = 1f)
  17. {
  18. Velocity = velocity;
  19. Type = type;
  20. LengthMultiplier = lengthMultiplier;
  21. }
  22. public static ParticleState GetRandom(float minVel, float maxVel)
  23. {
  24. var state = new ParticleState();
  25. state.Velocity = rand.NextVector2(minVel, maxVel);
  26. state.Type = ParticleType.None;
  27. state.LengthMultiplier = 1;
  28. return state;
  29. }
  30. public static void UpdateParticle(ParticleManager<ParticleState>.Particle particle)
  31. {
  32. var vel = particle.State.Velocity;
  33. float speed = vel.Length;
  34. // using Vector2.Add() should be slightly faster than doing "x.Position += vel;" because the Vector2s
  35. // are passed by reference and don't need to be copied. Since we may have to update a very large
  36. // number of particles, this method is a good candidate for optimizations.
  37. Vector2.Add(ref particle.Position, ref vel, out particle.Position);
  38. // fade the particle if its PercentLife or speed is low.
  39. float alpha = Math.Min(1, Math.Min(particle.PercentLife * 2, speed * 1f));
  40. alpha *= alpha;
  41. particle.Tint.A = alpha;
  42. // the length of bullet particles will be less dependent on their speed than other particles
  43. if (particle.State.Type == ParticleType.Bullet)
  44. particle.Scale.X = particle.State.LengthMultiplier * Math.Min(Math.Min(1f, 0.1f * speed + 0.1f), alpha);
  45. else
  46. particle.Scale.X = particle.State.LengthMultiplier * Math.Min(Math.Min(1f, 0.2f * speed + 0.1f), alpha);
  47. particle.Orientation = vel.ToAngle();
  48. var pos = particle.Position;
  49. int width = (int)GameRoot.ScreenSize.X;
  50. int height = (int)GameRoot.ScreenSize.Y;
  51. // collide with the edges of the screen
  52. if (pos.X < 0)
  53. vel.X = Math.Abs(vel.X);
  54. else if (pos.X > width)
  55. vel.X = -Math.Abs(vel.X);
  56. if (pos.Y < 0)
  57. vel.Y = Math.Abs(vel.Y);
  58. else if (pos.Y > height)
  59. vel.Y = -Math.Abs(vel.Y);
  60. if (particle.State.Type != ParticleType.IgnoreGravity)
  61. {
  62. foreach (var blackHole in EntityManager.BlackHoles)
  63. {
  64. var dPos = blackHole.Position - pos;
  65. float distance = dPos.Length;
  66. var n = dPos / distance;
  67. vel += 10000 * n / (distance * distance + 10000);
  68. // add tangential acceleration for nearby particles
  69. if (distance < 400)
  70. vel += 45 * new Vector2(n.Y, -n.X) / (distance + 100);
  71. }
  72. }
  73. if (Math.Abs(vel.X) + Math.Abs(vel.Y) < 0.00000000001f) // denormalized floats cause significant performance issues
  74. vel = Vector2.Zero;
  75. else if (particle.State.Type == ParticleType.Enemy)
  76. vel *= 0.94f;
  77. else
  78. vel *= 0.96f + Math.Abs(pos.X) % 0.04f; // rand.Next() isn't thread-safe, so use the position for pseudo-randomness
  79. particle.State.Velocity = vel;
  80. }
  81. }
  82. }