2
0

Meteor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using RockRainIphone.Core;
  6. namespace RockRainIphone
  7. {
  8. /// <summary>
  9. /// This class is the Animated Sprite for a Meteor
  10. /// </summary>
  11. public class Meteor : Sprite
  12. {
  13. // Vertical velocity
  14. protected int Yspeed;
  15. // Horizontal velocity
  16. protected int Xspeed;
  17. protected Random random;
  18. // Unique id for this meteor
  19. private int index;
  20. public Meteor(Game game, ref Texture2D theTexture) :
  21. base(game, ref theTexture)
  22. {
  23. Frames = new List<Rectangle>();
  24. Rectangle frame = new Rectangle();
  25. frame.X = 15;
  26. frame.Y = 10;
  27. frame.Width = 23;
  28. frame.Height = 24;
  29. Frames.Add(frame);
  30. frame.Y = 37;
  31. Frames.Add(frame);
  32. frame.Y = 63;
  33. frame.Height = 24;
  34. Frames.Add(frame);
  35. frame.Y = 89;
  36. frame.Height = 27;
  37. Frames.Add(frame);
  38. frame.Y = 119;
  39. frame.Height = 24;
  40. Frames.Add(frame);
  41. frame.Y = 145;
  42. Frames.Add(frame);
  43. frame.Y = 171;
  44. Frames.Add(frame);
  45. frame.Y = 199;
  46. frame.Height = 26;
  47. Frames.Add(frame);
  48. // Initialize the random number generator and put the meteor in your
  49. // start position
  50. random = new Random(GetHashCode());
  51. PutinStartPosition();
  52. }
  53. /// <summary>
  54. /// Initialize Meteor Position and Velocity
  55. /// </summary>
  56. public void PutinStartPosition()
  57. {
  58. position.X = random.Next(Game.Window.ClientBounds.Width -
  59. currentFrame.Width);
  60. position.Y = 0;
  61. YSpeed = 1 + random.Next(3);
  62. XSpeed = random.Next(3) - 1;
  63. }
  64. /// Vertical velocity
  65. /// </summary>
  66. public int YSpeed
  67. {
  68. get { return Yspeed; }
  69. set
  70. {
  71. Yspeed = value;
  72. frameDelay = 200 - (Yspeed * 2);
  73. }
  74. }
  75. /// <summary>
  76. /// Horizontal Velocity
  77. /// </summary>
  78. public int XSpeed
  79. {
  80. get { return Xspeed; }
  81. set { Xspeed = value; }
  82. }
  83. /// <summary>
  84. /// Meteor Identifier
  85. /// </summary>
  86. public int Index
  87. {
  88. get { return index; }
  89. set { index = value; }
  90. }
  91. /// <summary>
  92. /// Update the Meteor Position
  93. /// </summary>
  94. public override void Update(GameTime gameTime)
  95. {
  96. // Check if the meteor still visible
  97. if ((position.Y >= Game.Window.ClientBounds.Height) ||
  98. (position.X >= Game.Window.ClientBounds.Width) ||
  99. (position.X <= 0))
  100. {
  101. PutinStartPosition();
  102. }
  103. // Move meteor
  104. position.Y += Yspeed;
  105. position.X += Xspeed;
  106. base.Update(gameTime);
  107. }
  108. /// <summary>
  109. /// Check if the meteor intersects with the specified rectangle
  110. /// </summary>
  111. /// <param name="rect">test rectangle</param>
  112. /// <returns>true, if has a collision</returns>
  113. public bool CheckCollision(Rectangle rect)
  114. {
  115. Rectangle spriterect =new Rectangle((int) position.X, (int) position.Y,
  116. currentFrame.Width, currentFrame.Height);
  117. return spriterect.Intersects(rect);
  118. }
  119. }
  120. }