- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/PendulumGame/Screens/PendulumGravityChooserScreen.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // PendulumGravityChooserScreen.cs
    00003 //-----------------------------------------------------------------------------
    00004
    00005 namespace PendulumGame
    00006 {
    00007     using System;
    00008     using System.Threading;
    00009     using Microsoft.Xna.Framework;
    00010     using Microsoft.Xna.Framework.Audio;
    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 PendulumGravityChooserScreen : 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
    00065         private bool zoom;
    00066
    00070         private Cue cue;
    00071
    00072         #endregion
    00073
    00074         #region Initialization
    00075
    00079         public PendulumGravityChooserScreen()
    00080         {
    00081             TransitionOnTime = TimeSpan.FromSeconds(1.5);
    00082             TransitionOffTime = TimeSpan.FromSeconds(1.5);
    00083
    00084             this.zoom = false;
    00085         }
    00086
    00090         public override void LoadContent()
    00091         {
    00092             if (this.contentManager == null)
    00093             {
    00094                 this.contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
    00095             }
    00096
    00097             // Create physical element(s)
    00098             GravityCalculator gravityCalculator =
    00099                 new GravityCalculator(PendulumGame.State.CurrentCelestialObject);
    00100
    00101             // Per-object settings and sound
    00102             this.cue = null;
    00103             switch (PendulumGame.State.CurrentCelestialObject)
    00104             {
    00105                 case CelestialObject.Earth:
    00106                     gravityCalculator.EarthGravityModel =
    00107                         PendulumGame.State.CurrentEarthGravityModel;
    00108                     this.cue = this.ScreenManager.SoundBank.GetCue("earth");
    00109                     break;
    00110                 case CelestialObject.Mars:
    00111                     gravityCalculator.MarsGravityModel =
    00112                         PendulumGame.State.CurrentMarsGravityModel;
    00113                     break;
    00114                 case CelestialObject.Sun:
    00115                     this.cue = this.ScreenManager.SoundBank.GetCue("sun");
    00116                     break;
    00117                 case CelestialObject.Venus:
    00118                     this.cue = this.ScreenManager.SoundBank.GetCue("venus");
    00119                     break;
    00120                 case CelestialObject.Jupiter:
    00121                     this.cue = this.ScreenManager.SoundBank.GetCue("jupiter");
    00122                     break;
    00123                 case CelestialObject.Callisto:
    00124                     this.cue = this.ScreenManager.SoundBank.GetCue("callisto");
    00125                     break;
    00126                 case CelestialObject.Ganymede:
    00127                     this.cue = this.ScreenManager.SoundBank.GetCue("ganymede");
    00128                     break;
    00129                 case CelestialObject.Europa:
    00130                     this.cue = this.ScreenManager.SoundBank.GetCue("europa");
    00131                     break;
    00132                 case CelestialObject.Saturn:
    00133                     this.cue = this.ScreenManager.SoundBank.GetCue("saturn");
    00134                     break;
    00135             }
    00136
    00137             this.gravitySelector = new GravitySelector(
    00138                 ScreenManager,
    00139                 PendulumGame.State.CurrentCelestialObject,
    00140                 gravityCalculator);
    00141
    00142             // Lower volume of sound
    00143             this.ScreenManager.SetVolume("Default", 1.0f);
    00144
    00145             // Start sound
    00146             if (null != this.cue)
    00147             {
    00148                 this.cue.Play();
    00149             }
    00150
    00151             // Reset game time
    00152             ScreenManager.Game.ResetElapsedTime();
    00153         }
    00154
    00158         public override void UnloadContent()
    00159         {
    00160             if (null != this.cue)
    00161             {
    00162                 this.cue.Stop(AudioStopOptions.Immediate);
    00163             }
    00164
    00165             this.contentManager.Unload();
    00166         }
    00167
    00168         #endregion
    00169
    00170         #region Update and Draw
    00171
    00177         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00178                                                        bool coveredByOtherScreen)
    00179         {
    00180             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    00181
    00182             if (IsActive)
    00183             {
    00184                 // Maybe start sound
    00185                 if ((null != this.cue) && (!this.cue.IsPlaying))
    00186                 {
    00187                     this.cue.Resume();
    00188                 }
    00189
    00190                 // Animate gravity selector
    00191                 gravitySelector.Update(gameTime);
    00192             }
    00193             else
    00194             {
    00195                 // Maybe stop sound
    00196                 if ((null != this.cue) && (this.cue.IsPlaying))
    00197                 {
    00198                     this.cue.Pause();
    00199                 }
    00200             }
    00201         }
    00202
    00207         public override void HandleInput(InputState input)
    00208         {
    00209             if (input == null)
    00210             {
    00211                 throw new ArgumentNullException("input");
    00212             }
    00213
    00214             if (input.IsInputCancel(null) || input.IsDisconnected(null))
    00215             {
    00216                 ScreenManager.AddScreen(new PendulumPauseMenuScreen(), null);
    00217             }
    00218             else
    00219             {
    00220                 if (input.IsInputSelect(null))
    00221                 {
    00222                     // Capture selected value
    00223                     PendulumGame.State.CurrentGravity = gravitySelector.Gravity;
    00224                     this.zoom = true;
    00225
    00226                     // Quit to options menu
    00227                     PendulumLoadingScreen.Load(
    00228                         ScreenManager,
    00229                         true,
    00230                         null,
    00231                         new PendulumGameplayScreen());
    00232                 }
    00233
    00234                 // Keyboard speed adjustment
    00235                 if (input.IsInputLeft(null))
    00236                 {
    00237                     longitudeSpeed += speedStep;
    00238                 }
    00239                 else if (input.IsInputRight(null))
    00240                 {
    00241                     longitudeSpeed -= speedStep;
    00242                 }
    00243
    00244                 if (input.IsInputUp(null))
    00245                 {
    00246                     latitudeSpeed += speedStep;
    00247                 }
    00248                 else if (input.IsInputDown(null))
    00249                 {
    00250                     latitudeSpeed -= speedStep;
    00251                 }
    00252
    00253                 // Speed limiter
    00254                 if (longitudeSpeed < -speedMax)
    00255                 {
    00256                     longitudeSpeed = -speedMax;
    00257                 }
    00258                 else if (longitudeSpeed > speedMax)
    00259                 {
    00260                     longitudeSpeed = speedMax;
    00261                 }
    00262
    00263                 if (latitudeSpeed < -speedMax)
    00264                 {
    00265                     latitudeSpeed = -speedMax;
    00266                 }
    00267                 else if (latitudeSpeed > speedMax)
    00268                 {
    00269                     latitudeSpeed = speedMax;
    00270                 }
    00271
    00272                 // Update rotation
    00273                 gravitySelector.Longitude += longitudeSpeed;
    00274                 gravitySelector.Latitude += latitudeSpeed;
    00275
    00276                 // Drag speed
    00277                 longitudeSpeed *= speedDragFactor;
    00278                 latitudeSpeed *= speedDragFactor;
    00279             }
    00280         }
    00281
    00285         public override void Draw(GameTime gameTime)
    00286         {
    00287             // Match transition to selector size
    00288             if (this.zoom)
    00289             {
    00290                 gravitySelector.DisplaySize = 1.0f + TransitionPosition;
    00291             }
    00292             else
    00293             {
    00294                 gravitySelector.DisplaySize = 1.0f - TransitionPosition;
    00295             }
    00296
    00297             // Draw gravity selector
    00298             gravitySelector.Draw(gameTime);
    00299
    00300             // If the game is transitioning on or off, fade it out to black.
    00301             if (TransitionPosition > 0)
    00302             {
    00303                 ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
    00304             }
    00305         }
    00306
    00307         #endregion
    00308     }
    00309 }
    00310