- Main Page
- Classes
- Files

- File List

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

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // PendulumGameOverScreen.cs
    00003 //-----------------------------------------------------------------------------
    00004
    00005 namespace PendulumGame
    00006 {
    00007     using System;
    00008     using Microsoft.Xna.Framework;
    00009     using Microsoft.Xna.Framework.Content;
    00010     using Microsoft.Xna.Framework.Graphics;
    00011     using Microsoft.Xna.Framework.Media;
    00012
    00013     using NewGamePhysics.StateManager;
    00014     using NewGamePhysics.Utilities;
    00015
    00019     class PendulumGameOverScreen : MenuScreen
    00020     {
    00021         #region fields
    00022
    00026         private ContentManager content;
    00027
    00031         private Video videoFile;
    00032
    00036         private bool videoStarted = false;
    00037
    00041         private Effect effect;
    00042
    00046         private Color referenceColor;
    00047
    00051         Texture2D frameTexture;
    00052
    00053         #endregion
    00054
    00055         #region Initialization
    00056
    00060         public PendulumGameOverScreen()
    00061             : base("Game Over")
    00062         {
    00063             // Flag that there is no need for the game to transition
    00064             // off when the pause menu is on top of it.
    00065             IsPopup = true;
    00066
    00067             // Create our menu entries.
    00068             MenuEntry playGameMenuEntry = new MenuEntry("Play Again");
    00069             MenuEntry returnMenuEntry = new MenuEntry("Return to Main Menu");
    00070
    00071             // Hook up menu event handlers.
    00072             playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
    00073             returnMenuEntry.Selected += OnContinue;
    00074
    00075             // Add entries to the menu.
    00076             MenuEntries.Add(playGameMenuEntry);
    00077             MenuEntries.Add(returnMenuEntry);
    00078         }
    00079
    00087         public override void LoadContent()
    00088         {
    00089             if (content == null)
    00090             {
    00091                 content = new ContentManager(ScreenManager.Game.Services, "Content");
    00092             }
    00093
    00094             frameTexture = content.Load<Texture2D>(@"Sprites\frame");
    00095
    00096             // Pick random video
    00097             int videoNumber = PendulumGame.State.PhysicalRandomNumberGenerator.Next(0, 12);
    00098
    00099             videoFile = content.Load<Video>(@"Videos\Pendulum" + videoNumber);
    00100             effect = content.Load<Effect>(@"Effects\ColorChromaKey");
    00101
    00102             // Determine chromakey reference color by averaging
    00103             // the color over the reference image
    00104             Texture2D backgroundReference =
    00105                 content.Load<Texture2D>(@"Textures\PendulumBackgroundReference");
    00106             Color[] colors = TextureHelpers.TextureToColors(backgroundReference);
    00107             ulong rSum = 0;
    00108             ulong gSum = 0;
    00109             ulong bSum = 0;
    00110             ulong num = (ulong)colors.Length;
    00111             foreach (Color color in colors)
    00112             {
    00113                 rSum += (ulong)color.R;
    00114                 gSum += (ulong)color.G;
    00115                 bSum += (ulong)color.B;
    00116             }
    00117
    00118             this.referenceColor = new Color(
    00119                 (byte)(rSum / num),
    00120                 (byte)(gSum / num),
    00121                 (byte)(bSum / num));
    00122         }
    00123
    00127         public override void UnloadContent()
    00128         {
    00129             content.Unload();
    00130         }
    00131
    00132         #endregion
    00133
    00134         #region Handle Input
    00135
    00139         void OnContinue(object sender, PlayerIndexEventArgs e)
    00140         {
    00141             PendulumLoadingScreen.Load(ScreenManager, false, null, new PendulumBackgroundScreen(),
    00142                                                            new PendulumMainMenuScreen());
    00143         }
    00144
    00148         void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
    00149         {
    00150             PendulumLoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
    00151                                new PendulumGameplayScreen());
    00152         }
    00153
    00154         #endregion
    00155
    00156         #region Draw
    00157
    00165         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00166                                                        bool coveredByOtherScreen)
    00167         {
    00168             if (!videoStarted)
    00169             {
    00170                 ScreenManager.VideoPlayer.Play(videoFile);
    00171                 videoStarted = true;
    00172             }
    00173             else
    00174             {
    00175                 if (ScreenManager.VideoPlayer.State == MediaState.Stopped)
    00176                 {
    00177                     // Return to main menu
    00178                     PendulumLoadingScreen.Load(ScreenManager, false, null, new PendulumBackgroundScreen(),
    00179                                                new PendulumMainMenuScreen());
    00180                 }
    00181             }
    00182
    00183             base.Update(gameTime, otherScreenHasFocus, false);
    00184         }
    00185
    00190         public override void Draw(GameTime gameTime)
    00191         {
    00192             // Dim background
    00193             ScreenManager.FadeBackBufferToBlack(3 * TransitionAlpha / 4);
    00194
    00195             // Messages
    00196             string[] messages = new string[5];
    00197             if (PendulumGame.State.Players[0].Points > PendulumGame.State.Players[1].Points)
    00198             {
    00199                 messages[0] = "Player A wins!";
    00200             }
    00201             else if (PendulumGame.State.Players[0].Points == PendulumGame.State.Players[1].Points)
    00202             {
    00203                 messages[0] = "It is a Tie!";
    00204             }
    00205             else
    00206             {
    00207                 messages[0] = "Player B wins!";
    00208             }
    00209
    00210             messages[0] = "===[ " + messages[0] + " ]===";
    00211             messages[1] = "Player A got " + PendulumGame.State.Players[0].Points + " points";
    00212             messages[2] = "Player B got " + PendulumGame.State.Players[1].Points + " points";
    00213             messages[3] = PendulumGame.State.SubmittedEntropyBits + " bits of Entropy extracted from last round";
    00214             messages[4] =
    00215                 PendulumGame.State.PhysicalRandomNumberGenerator.RetrievedEntropyBits + " bits of " +
    00216                 PendulumGame.State.PhysicalRandomNumberGenerator.EntropySource.ToString() + " Entropy used in total";
    00217
    00218             // Draw score summary
    00219             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    00220             SpriteFont font = ScreenManager.Fonts["retro"];
    00221             Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
    00222             Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
    00223             Color color = Color.White;
    00224             Vector2 origin = new Vector2(0, 0);
    00225             Vector2 position = new Vector2(viewport.Width * 0.3f, viewport.Height * 0.65f);
    00226             spriteBatch.Begin();
    00227             foreach (string message in messages)
    00228             {
    00229                 spriteBatch.DrawString(
    00230                     font,
    00231                     message,
    00232                     position,
    00233                     color,
    00234                     0,
    00235                     origin,
    00236                     1.0f,
    00237                     SpriteEffects.None,
    00238                     0);
    00239                 position.Y += (viewport.Height * 0.05f);
    00240             }
    00241
    00242             spriteBatch.Draw(
    00243                 frameTexture,
    00244                 fullscreen,
    00245                 Color.White);
    00246             spriteBatch.End();
    00247
    00248             // Render the video in it's orginal resolution
    00249             // to the screen using SpriteBatch
    00250             if (ScreenManager.VideoPlayer.State == MediaState.Playing)
    00251             {
    00252                 int frameSize = (int)((float)viewport.Width * 0.3f);
    00253                 Rectangle videoFrame =
    00254                     new Rectangle(
    00255                         viewport.Width - frameSize - 10,
    00256                         viewport.Height / 2 - frameSize / 2 - 30,
    00257                         frameSize,
    00258                         frameSize);
    00259                 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
    00260                 effect.Begin();
    00261                 float threshold = 0.33f;
    00262                 effect.Parameters["referenceColor"].SetValue(this.referenceColor.ToVector3());
    00263                 effect.Parameters["threshold"].SetValue(threshold);
    00264                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    00265                 {
    00266                     pass.Begin();
    00267                     spriteBatch.Draw(
    00268                         ScreenManager.VideoPlayer.GetTexture(),
    00269                         videoFrame,
    00270                         Color.White);
    00271                     pass.End();
    00272                 }
    00273                 effect.End();
    00274                 spriteBatch.End();
    00275             }
    00276
    00277             // If the game is transitioning on or off, fade it out to black.
    00278             if (TransitionPosition > 0)
    00279             {
    00280                 ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
    00281             }
    00282
    00283             base.Draw(gameTime);
    00284         }
    00285
    00286         #endregion
    00287     }
    00288 }
    00289