PowerSource.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using RockRain.Core;
  6. namespace RockRain
  7. {
  8. /// <summary>
  9. /// This is a game component that implements Power Source Element.
  10. /// </summary>
  11. public class PowerSource : Sprite
  12. {
  13. protected Texture2D texture;
  14. protected Random random;
  15. public PowerSource(Game game, ref Texture2D theTexture)
  16. : base(game, ref theTexture)
  17. {
  18. texture = theTexture;
  19. Frames = new List<Rectangle>();
  20. Rectangle frame = new Rectangle();
  21. frame.X = 55;
  22. frame.Y = 15;
  23. frame.Width = 14;
  24. frame.Height = 12;
  25. Frames.Add(frame);
  26. frame.Y = 29;
  27. Frames.Add(frame);
  28. frame.Y = 42;
  29. Frames.Add(frame);
  30. frame.Y = 56;
  31. Frames.Add(frame);
  32. frame.Y = 69;
  33. Frames.Add(frame);
  34. frame.Y = 81;
  35. Frames.Add(frame);
  36. frameDelay = 200;
  37. // Initialize the random number generator and put the power source in your
  38. // start position
  39. random = new Random(GetHashCode());
  40. PutinStartPosition();
  41. }
  42. /// <summary>
  43. /// Initialize Position and Velocity
  44. /// </summary>
  45. public void PutinStartPosition()
  46. {
  47. position.X = random.Next(Game.Window.ClientBounds.Width -
  48. currentFrame.Width);
  49. position.Y = -10;
  50. Enabled = false;
  51. }
  52. public override void Update(GameTime gameTime)
  53. {
  54. // Check if the still visible
  55. if (position.Y >= Game.Window.ClientBounds.Height)
  56. {
  57. PutinStartPosition();
  58. }
  59. // Move
  60. position.Y += 1;
  61. base.Update(gameTime);
  62. }
  63. /// <summary>
  64. /// Check if the object intersects with the specified rectangle
  65. /// </summary>
  66. /// <param name="rect">test rectangle</param>
  67. /// <returns>true, if has a collision</returns>
  68. public bool CheckCollision(Rectangle rect)
  69. {
  70. Rectangle spriterect =
  71. new Rectangle((int) position.X, (int) position.Y,
  72. currentFrame.Width, currentFrame.Height);
  73. return spriterect.Intersects(rect);
  74. }
  75. }
  76. }