Asteroid.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Asteroid.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. #endregion
  15. namespace Spacewar
  16. {
  17. /// <summary>
  18. /// The types of asteroid
  19. /// </summary>
  20. public enum AsteroidType
  21. {
  22. /// <summary>
  23. /// Large asteroid
  24. /// </summary>
  25. Large,
  26. /// <summary>
  27. /// Small asteroid
  28. /// </summary>
  29. Small,
  30. }
  31. class Asteroid : SpacewarSceneItem
  32. {
  33. private float roll;
  34. private float pitch;
  35. private float yaw;
  36. private bool destroyed;
  37. private float rollIncrement;
  38. private float pitchIncrement;
  39. private float yawIncrement;
  40. private static Random random = new Random();
  41. #region Properties
  42. public bool Destroyed
  43. {
  44. get
  45. {
  46. return destroyed;
  47. }
  48. set
  49. {
  50. destroyed = value;
  51. }
  52. }
  53. #endregion
  54. public Asteroid(Game game, AsteroidType asteroidType, Vector3 position)
  55. : base(game, new BasicEffectShape(game, BasicEffectShapes.Asteroid, (int)asteroidType, LightingType.InGame), position)
  56. {
  57. //Random spin increments on all 3 axis
  58. rollIncrement = (float)random.NextDouble() - .5f;
  59. pitchIncrement = (float)random.NextDouble() - .5f;
  60. yawIncrement = (float)random.NextDouble() - .5f;
  61. if (asteroidType == AsteroidType.Large)
  62. Radius = 15;
  63. if (asteroidType == AsteroidType.Small)
  64. Radius = 6;
  65. }
  66. public override void Update(TimeSpan time, TimeSpan elapsedTime)
  67. {
  68. //Random rotation
  69. roll += rollIncrement * (float)elapsedTime.TotalSeconds;
  70. yaw += yawIncrement * (float)elapsedTime.TotalSeconds;
  71. pitch += pitchIncrement * (float)elapsedTime.TotalSeconds;
  72. rotation = new Vector3(roll, pitch, yaw);
  73. base.Update(time, elapsedTime);
  74. }
  75. }
  76. }