BlackHole.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //---------------------------------------------------------------------------------
  2. // Ported to the Atomic Game Engine
  3. // Originally written for XNA by Michael Hoffman
  4. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  5. //----------------------------------------------------------------------------------
  6. using System;
  7. using AtomicEngine;
  8. namespace AtomicBlaster
  9. {
  10. class BlackHole : Entity
  11. {
  12. private static Random rand = new Random();
  13. private int hitpoints = 10;
  14. private float sprayAngle = 0;
  15. public BlackHole(Vector2 position)
  16. {
  17. image = Art.BlackHole;
  18. Position = position;
  19. Radius = image.Width / 2f;
  20. }
  21. public override void Update()
  22. {
  23. var entities = EntityManager.GetNearbyEntities(Position, 250);
  24. foreach (var entity in entities)
  25. {
  26. if (entity is Enemy && !(entity as Enemy).IsActive)
  27. continue;
  28. // bullets are repelled by black holes and everything else is attracted
  29. if (entity is Bullet)
  30. entity.Velocity += (entity.Position - Position).ScaleTo(0.3f);
  31. else
  32. {
  33. var dPos = Position - entity.Position;
  34. var length = dPos.Length;
  35. entity.Velocity += dPos.ScaleTo(MathHelper.Lerp(2, 0, length / 250f));
  36. }
  37. }
  38. // The black holes spray some orbiting particles. The spray toggles on and off every quarter second.
  39. if ((GameRoot.ElapsedTime * 1000 / 250) % 2 == 0)
  40. {
  41. Vector2 sprayVel = MathUtil.FromPolar(sprayAngle, rand.NextFloat(12, 15));
  42. Color color = ColorUtil.HSVToColor(5, 0.5f, 0.8f); // light purple
  43. Vector2 pos = Position + 2f * new Vector2(sprayVel.Y, -sprayVel.X) + rand.NextVector2(4, 8);
  44. var state = new ParticleState()
  45. {
  46. Velocity = sprayVel,
  47. LengthMultiplier = 1,
  48. Type = ParticleType.Enemy
  49. };
  50. GameRoot.ParticleManager.CreateParticle(Art.LineParticle, pos, color, 190, 1.5f, state);
  51. }
  52. // rotate the spray direction
  53. sprayAngle -= MathHelper.TwoPi / 50f;
  54. GameRoot.Grid.ApplyImplosiveForce((float)Math.Sin(sprayAngle / 2) * 10 + 20, Position, 200);
  55. }
  56. public void WasShot()
  57. {
  58. hitpoints--;
  59. if (hitpoints <= 0)
  60. {
  61. IsExpired = true;
  62. PlayerStatus.AddPoints(5);
  63. PlayerStatus.IncreaseMultiplier();
  64. }
  65. float hue = (float)((3 * GameRoot.ElapsedTime) % 6);
  66. Color color = ColorUtil.HSVToColor(hue, 0.25f, 1);
  67. const int numParticles = 150;
  68. float startOffset = rand.NextFloat(0, MathHelper.TwoPi / numParticles);
  69. for (int i = 0; i < numParticles; i++)
  70. {
  71. Vector2 sprayVel = MathUtil.FromPolar(MathHelper.TwoPi * i / numParticles + startOffset, rand.NextFloat(8, 16));
  72. Vector2 pos = Position + 2f * sprayVel;
  73. var state = new ParticleState()
  74. {
  75. Velocity = sprayVel,
  76. LengthMultiplier = 1,
  77. Type = ParticleType.IgnoreGravity
  78. };
  79. GameRoot.ParticleManager.CreateParticle(Art.LineParticle, pos, color, 90, 1.5f, state);
  80. }
  81. //Sound.Explosion.Play(0.5f, rand.NextFloat(-0.2f, 0.2f), 0);
  82. }
  83. public void Kill()
  84. {
  85. hitpoints = 0;
  86. WasShot();
  87. }
  88. public override void Draw(/*SpriteBatch spriteBatch*/)
  89. {
  90. // make the size of the black hole pulsate
  91. float scale = 1 + 0.1f * (float)Math.Sin(10 * GameRoot.ElapsedTime);
  92. CustomRenderer.Draw(image, Position, color, Orientation, Size / 2f, scale, 0);
  93. }
  94. }
  95. }