SpacewarSceneItem.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpacewarSceneItem.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. using System.Diagnostics;
  15. #endregion
  16. namespace Spacewar
  17. {
  18. /// <summary>
  19. /// SpacewarSceneItem is a SceneItem that adds gravity and screen wrapping to the item automatically
  20. /// </summary>
  21. public class SpacewarSceneItem : SceneItem
  22. {
  23. /// <summary>
  24. /// Default Constructor - does nothing special
  25. /// </summary>
  26. public SpacewarSceneItem(Game game)
  27. : base(game)
  28. {
  29. }
  30. /// <summary>
  31. /// This constructor creates an item with a particular shape at a particular point
  32. /// </summary>
  33. /// <param name="shape">A shape that represents the item</param>
  34. /// <param name="initialPosition">The world space position</param>
  35. public SpacewarSceneItem(Game game, Shape shape, Vector3 initialPosition)
  36. : base(game, shape, initialPosition)
  37. {
  38. }
  39. /// <summary>
  40. /// SpacewarSceneItem handles all the gravity and wrapping calculations
  41. /// </summary>
  42. /// <param name="time"></param>
  43. /// <param name="elapsedTime"></param>
  44. public override void Update(TimeSpan time, TimeSpan elapsedTime)
  45. {
  46. if (!Paused)
  47. {
  48. //Add in gravity
  49. Vector3 forceDirection = Position - new Vector3(SpacewarGame.Settings.SunPosition, 0.0f);
  50. double distancePower = Math.Pow(forceDirection.Length(), SpacewarGame.Settings.GravityPower);
  51. double factor = Math.Min(SpacewarGame.Settings.GravityStrength / distancePower, 100.0); //stops insane accelerations at the sun since we have no collisions
  52. Vector3 gravityAcceleration = Vector3.Multiply(Vector3.Normalize(forceDirection), (float)factor);
  53. acceleration -= gravityAcceleration;
  54. }
  55. //Call the base to update velocity and position
  56. base.Update(time, elapsedTime);
  57. //Zero out acceleration - will be reset on next update
  58. acceleration = Vector3.Zero;
  59. //Wrap around the screen to the correct position.
  60. if (SpacewarGame.GameState == GameState.PlayEvolved)
  61. {
  62. if (position.X > 400)
  63. position.X = -400;
  64. else if (position.X < -400)
  65. position.X = 400;
  66. }
  67. else
  68. {
  69. if (position.X > 300)
  70. position.X = -300;
  71. else if (position.X < -300)
  72. position.X = 300;
  73. }
  74. if (position.Y > 250)
  75. position.Y = -250;
  76. else if (position.Y < -250)
  77. position.Y = 250;
  78. }
  79. }
  80. }