- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/MontyHallGame/Screens/MontyHallLoadingScreen.cs

    00001 //-----------------------------------------------------------------------------
    00002 // MontyHallLoadingScreen.cs
    00003 // (C) A. Schiffler, 2009-2011
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace MontyHallGame
    00007 {
    00008     using System;
    00009     using Microsoft.Xna.Framework;
    00010     using Microsoft.Xna.Framework.Graphics;
    00011     using NewGamePhysics.StateManager;
    00012
    00017     class MontyHallLoadingScreen : GameScreen
    00018     {
    00019         #region Fields
    00020
    00021         bool loadingIsSlow;
    00022         bool otherScreensAreGone;
    00023
    00024         GameScreen[] screensToLoad;
    00025
    00026         #endregion
    00027
    00028         #region Initialization
    00029
    00030
    00035         private MontyHallLoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
    00036                               GameScreen[] screensToLoad)
    00037         {
    00038             this.loadingIsSlow = loadingIsSlow;
    00039             this.screensToLoad = screensToLoad;
    00040
    00041             TransitionOnTime = TimeSpan.FromSeconds(0.5);
    00042         }
    00043
    00044
    00048         public static void Load(ScreenManager screenManager, bool loadingIsSlow,
    00049                                 PlayerIndex? controllingPlayer,
    00050                                 params GameScreen[] screensToLoad)
    00051         {
    00052             // Tell all the current screens to transition off.
    00053             foreach (GameScreen screen in screenManager.GetScreens())
    00054                 screen.ExitScreen();
    00055
    00056             // Create and activate the loading screen.
    00057             MontyHallLoadingScreen loadingScreen = new MontyHallLoadingScreen(screenManager,
    00058                                                             loadingIsSlow,
    00059                                                             screensToLoad);
    00060
    00061             screenManager.AddScreen(loadingScreen, controllingPlayer);
    00062         }
    00063
    00064
    00065         #endregion
    00066
    00067         #region Update and Draw
    00068
    00072         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00073                                                        bool coveredByOtherScreen)
    00074         {
    00075             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
    00076
    00077             // If all the previous screens have finished transitioning
    00078             // off, it is time to actually perform the load.
    00079             if (otherScreensAreGone)
    00080             {
    00081                 ScreenManager.RemoveScreen(this);
    00082
    00083                 foreach (GameScreen screen in screensToLoad)
    00084                 {
    00085                     if (screen != null)
    00086                     {
    00087                         ScreenManager.AddScreen(screen, ControllingPlayer);
    00088                     }
    00089                 }
    00090
    00091                 // Once the load has finished, we use ResetElapsedTime to tell
    00092                 // the  game timing mechanism that we have just finished a very
    00093                 // long frame, and that it should not try to catch up.
    00094                 ScreenManager.Game.ResetElapsedTime();
    00095             }
    00096         }
    00097
    00098
    00102         public override void Draw(GameTime gameTime)
    00103         {
    00104             // If we are the only active screen, that means all the previous screens
    00105             // must have finished transitioning off. We check for this in the Draw
    00106             // method, rather than in Update, because it isn't enough just for the
    00107             // screens to be gone: in order for the transition to look good we must
    00108             // have actually drawn a frame without them before we perform the load.
    00109             if ((ScreenState == ScreenState.Active) &&
    00110                 (ScreenManager.GetScreens().Length == 1))
    00111             {
    00112                 otherScreensAreGone = true;
    00113             }
    00114
    00115             // The gameplay screen takes a while to load, so we display a loading
    00116             // message while that is going on, but the menus load very quickly, and
    00117             // it would look silly if we flashed this up for just a fraction of a
    00118             // second while returning from the game to the menus. This parameter
    00119             // tells us how long the loading is going to take, so we know whether
    00120             // to bother drawing the message.
    00121             if (loadingIsSlow)
    00122             {
    00123                 SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    00124                 SpriteFont font = ScreenManager.Fonts["game"];
    00125
    00126                 const string message = "Loading...";
    00127
    00128                 // Center the text in the viewport.
    00129                 Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
    00130                 Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
    00131                 Vector2 textSize = font.MeasureString(message);
    00132                 Vector2 textPosition = (viewportSize - textSize) / 2;
    00133
    00134                 Color color = new Color(255, 255, 255, TransitionAlpha);
    00135
    00136                 // Draw the text.
    00137                 spriteBatch.Begin();
    00138                 spriteBatch.DrawString(font, message, textPosition, color);
    00139                 spriteBatch.End();
    00140             }
    00141         }
    00142
    00143         #endregion
    00144     }
    00145 }