PowerSource.cs 2.5 KB

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