- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/GravityChooser/Screens/GravityChooserGameplayScreen.cs

    00001 //-----------------------------------------------------------------------------
    00002 // GravityChooserGameplayScreen.cs
    00003 // (A. Schiffler, 2009-2011)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace GravityChooser
    00007 {
    00008     using System;
    00009     using System.Threading;
    00010     using Microsoft.Xna.Framework;
    00011     using Microsoft.Xna.Framework.Content;
    00012     using Microsoft.Xna.Framework.Graphics;
    00013     using Microsoft.Xna.Framework.Input;
    00014
    00015     using NewGamePhysics.StateManager;
    00016     using NewGamePhysics.Physics;
    00017     using NewGamePhysics.GraphicalElements;
    00018     using NewGamePhysics.PhysicalElements;
    00019
    00023     class GravityChooserGameplayScreen : GameScreen
    00024     {
    00025         #region Fields
    00026
    00030         private ContentManager contentManager;
    00031
    00035         private GravitySelector gravitySelector;
    00036
    00040         private double speedStep = 0.1;
    00041
    00045         private double speedMax = 1.5;
    00046
    00050         private double speedDragFactor = 0.95;
    00051
    00055         private double longitudeSpeed = 0.0;
    00056
    00060         private double latitudeSpeed = 0.0;
    00061
    00062         #endregion
    00063
    00064         #region Initialization
    00065
    00069         public GravityChooserGameplayScreen()
    00070         {
    00071             TransitionOnTime = TimeSpan.FromSeconds(1.5);
    00072             TransitionOffTime = TimeSpan.FromSeconds(0.5);
    00073         }
    00074
    00078         public override void LoadContent()
    00079         {
    00080             if (this.contentManager == null)
    00081             {
    00082                 this.contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
    00083             }
    00084
    00085             // Create physical element(s)
    00086             GravityCalculator gravityCalculator =
    00087                 new GravityCalculator(GravityChooser.state.CurrentCelestialObject);
    00088             if (GravityChooser.state.CurrentCelestialObject == CelestialObject.Earth)
    00089             {
    00090                 gravityCalculator.EarthGravityModel = GravityChooser.state.CurrentEarthGravityModel;
    00091             }
    00092
    00093             this.gravitySelector = new GravitySelector(
    00094                 ScreenManager,
    00095                 GravityChooser.state.CurrentCelestialObject,
    00096                 gravityCalculator);
    00097
    00098             // Reset game time
    00099             ScreenManager.Game.ResetElapsedTime();
    00100         }
    00101
    00102
    00106         public override void UnloadContent()
    00107         {
    00108             this.contentManager.Unload();
    00109         }
    00110
    00111         #endregion
    00112
    00113         #region Update and Draw
    00114
    00120         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00121                                                        bool coveredByOtherScreen)
    00122         {
    00123             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    00124
    00125             if (IsActive)
    00126             {
    00127                 // Animate gravity selector
    00128                 gravitySelector.Update(gameTime);
    00129             }
    00130         }
    00131
    00132
    00137         public override void HandleInput(InputState input)
    00138         {
    00139             if (input == null)
    00140             {
    00141                 throw new ArgumentNullException("input");
    00142             }
    00143
    00144             // Look up inputs for the active player profile.
    00145             PlayerIndex playerIndex;
    00146             if (ControllingPlayer != null)
    00147             {
    00148                 playerIndex = ControllingPlayer.Value;
    00149             }
    00150             else
    00151             {
    00152                 playerIndex = 0;
    00153             }
    00154
    00155             KeyboardState keyboardState = input.CurrentKeyboardStates[(int)playerIndex];
    00156             GamePadState gamePadState = input.CurrentGamePadStates[(int)playerIndex];
    00157
    00158             // The game pauses either if the user presses the pause button, or if
    00159             // they unplug the active gamepad. This requires us to keep track of
    00160             // whether a gamepad was ever plugged in, because we don't want to pause
    00161             // on PC if they are playing with a keyboard and have no gamepad at all!
    00162             bool gamePadDisconnected = !gamePadState.IsConnected &&
    00163                                        input.GamePadWasConnected[(int)playerIndex];
    00164
    00165             if (input.IsInputCancel(ControllingPlayer) || gamePadDisconnected)
    00166             {
    00167                 ScreenManager.AddScreen(new GravityChooserPauseMenuScreen(), ControllingPlayer);
    00168             }
    00169             else
    00170             {
    00171                 if (input.IsInputSelect(ControllingPlayer))
    00172                 {
    00173                     // Capture selected value
    00174                     GravityChooser.state.CurrentGravity = gravitySelector.Gravity;
    00175
    00176                     // Quit to main menu
    00177                     GravityChooserLoadingScreen.Load(
    00178                         ScreenManager,
    00179                         false,
    00180                         null,
    00181                         new GravityChooserBackgroundScreen(),
    00182                         new GravityChooserMainMenuScreen());
    00183                 }
    00184
    00185                 // Keyboard speed adjustment
    00186                 if (input.IsInputLeft(null))
    00187                 {
    00188                     longitudeSpeed += speedStep;
    00189                 }
    00190                 else if (input.IsInputRight(null))
    00191
    00192                 {
    00193                     longitudeSpeed -= speedStep;
    00194                 }
    00195
    00196                 if (input.IsInputUp(null))
    00197                 {
    00198                     latitudeSpeed += speedStep;
    00199                 }
    00200                 else if (input.IsInputDown(null))
    00201                 {
    00202                     latitudeSpeed -= speedStep;
    00203                 }
    00204
    00205                 // Speed limiter
    00206                 if (longitudeSpeed < -speedMax)
    00207                 {
    00208                     longitudeSpeed = -speedMax;
    00209                 }
    00210                 else if (longitudeSpeed > speedMax)
    00211                 {
    00212                     longitudeSpeed = speedMax;
    00213                 }
    00214
    00215                 if (latitudeSpeed < -speedMax)
    00216                 {
    00217                     latitudeSpeed = -speedMax;
    00218                 }
    00219                 else if (latitudeSpeed > speedMax)
    00220                 {
    00221                     latitudeSpeed = speedMax;
    00222                 }
    00223
    00224                 // Update rotation
    00225                 gravitySelector.Longitude += longitudeSpeed;
    00226                 gravitySelector.Latitude += latitudeSpeed;
    00227
    00228                 // Drag speed
    00229                 longitudeSpeed *= speedDragFactor;
    00230                 latitudeSpeed *= speedDragFactor;
    00231             }
    00232         }
    00233
    00237         public override void Draw(GameTime gameTime)
    00238         {
    00239             // Match transition to selector size
    00240             gravitySelector.DisplaySize = 1.0f - TransitionPosition;
    00241
    00242             // Draw gravity selector
    00243             gravitySelector.Draw(gameTime);
    00244
    00245             // If the game is transitioning on or off, fade it out to black.
    00246             if (TransitionPosition > 0)
    00247             {
    00248                 ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
    00249             }
    00250         }
    00251
    00252
    00253         #endregion
    00254     }
    00255 }