SnowEmitter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Tutorial024.Sprites;
  9. namespace Tutorial024.Emitters
  10. {
  11. public class SnowEmitter : Emitter
  12. {
  13. public SnowEmitter(Sprite particle)
  14. : base(particle)
  15. {
  16. }
  17. protected override void ApplyGlobalVelocity()
  18. {
  19. var xSway = (float)Game1.Random.Next(-2, 2);
  20. foreach (var particle in _particles)
  21. particle.Velocity.X = (xSway * particle.Scale) / 50;
  22. }
  23. protected override Sprite GenerateParticle()
  24. {
  25. var sprite = _particlePrefab.Clone() as Sprite;
  26. var xPosition = Game1.Random.Next(0, Game1.ScreenWidth);
  27. var ySpeed = Game1.Random.Next(10, 100) / 100f;
  28. sprite.Position = new Vector2(xPosition, -sprite.Rectangle.Height);
  29. sprite.Opacity = (float)Game1.Random.NextDouble();
  30. sprite.Rotation = MathHelper.ToRadians(Game1.Random.Next(0, 360));
  31. sprite.Scale = (float)Game1.Random.NextDouble() + Game1.Random.Next(0, 3);
  32. sprite.Velocity = new Vector2(0, ySpeed);
  33. return sprite;
  34. }
  35. }
  36. }