/* * ScoreSummary.cs * Authors: August Zinsser * Shawn Chen * * Copyright Matthew Belmonte 2007 */ #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Nuclex.Fonts; using tAC_Engine; #endregion namespace Astropolis { /// /// Takes the data from a ScoreCard and displays it in a nice window until the user clicks OK. /// This window sits on top of everything and does not stop the game flow, so games can continue /// to run below this window. /// /// TODO: Make this a HUD widget public class ScoreSummary { private Dictionary mIndividualScores; // Raw score data private Dictionary mResourceIcons; // Icons to display next to the scores private Entity mBackgroundImage; // The graphics for the window private Entity mOKButton; // The graphic for the OK button private Texture2D mOKTexture; // Image for the netural OK button private Texture2D mOKGlowTexture; // Image for the highlighted OK button private string mResultsMessage = ""; // Used so the top portion of the window can output the results of the last game private bool mSuccess; // True displays the results message in blue, false in red private bool mVisible = false; // Display the entire window or not /// /// Constructor /// /// public ScoreSummary() { // Initialize variables mIndividualScores = new Dictionary(); mResourceIcons = new Dictionary(); // Create the graphic objects Texture2D bgImageTexture = TextureManager.Load(@"Content\General\ScoreSummaryBackground"); mBackgroundImage = new Entity(bgImageTexture); mBackgroundImage.X = AstroBaseApplication.GameManager.ScreenWidth / 2; mBackgroundImage.Y = AstroBaseApplication.GameManager.ScreenHeight / 2; mBackgroundImage.Width = AstroBaseApplication.GameManager.ScreenWidth; mBackgroundImage.Height = AstroBaseApplication.GameManager.ScreenHeight; mOKTexture = TextureManager.Load(@"Content\General\OKButton"); mOKGlowTexture = TextureManager.Load(@"Content\General\OKButtonGlow"); // Calculate values used to align the results float Width1 = mOKTexture.Width; float Width2 = mBackgroundImage.Texture.Width; float OKWidthRatio = Width1 / Width2; float Height1 = mOKTexture.Height; float Height2 = mBackgroundImage.Texture.Height; float OKHeightRatio = Height1 / Height2; Vector3 OKPosition = new Vector3(.8f * AstroBaseApplication.GameManager.ScreenWidth, .96f * AstroBaseApplication.GameManager.ScreenHeight, 0f); Vector3 OKVolume = new Vector3(AstroBaseApplication.GameManager.ScreenWidth * OKWidthRatio, AstroBaseApplication.GameManager.ScreenHeight * OKHeightRatio, 1f); float startDistance = .30f; float endDistance = .90f; float interval = (endDistance - startDistance) / AstroResources.List.Count; float heightCounter = startDistance; // Make the OK button and fill it with the proper textures Animation butAnim = new Animation(new Vector2(mOKTexture.Width, mOKTexture.Height)); butAnim.AddClip("Neutral", mOKTexture, 1, 1f); butAnim.AddClip("Selected", mOKGlowTexture, 1, 1f); mOKButton = new Entity(butAnim, OKPosition, OKVolume); // Load the resource icon images foreach (string resourceName in AstroResources.List) { Entity icon = new Entity(TextureManager.Load(@"Content\General\" + resourceName)); icon.Width = AstroBaseApplication.GameManager.ScreenWidth * .04f; icon.Height = icon.Width; icon.X = AstroBaseApplication.GameManager.ScreenWidth * .25f; icon.Y = heightCounter * AstroBaseApplication.GameManager.ScreenHeight; icon.Z = 0f; mResourceIcons.Add(resourceName, icon); heightCounter += interval; } } /// /// Displays the score window, allowing games to run behind it /// /// The score to display /// Display this text at the top of the screen /// True colorizes the results message in blue, false in red public void DisplayScores(AstroScoreCard score, bool success, string resultsMessage) { mIndividualScores.Clear(); // Copy the scores so the scorecard can change values without messing up the score summary screen foreach (string resourceName in AstroResources.List) { if (mIndividualScores.ContainsKey(resourceName)) mIndividualScores[resourceName] += score.QueryResource(resourceName); else mIndividualScores.Add(resourceName, score.QueryResource(resourceName)); } mSuccess = success; mResultsMessage = resultsMessage; mVisible = true; } /// /// Hide the window, but keep it in memory /// public void Hide() { mVisible = false; } /// /// Allows the game component to update itself. /// public void Update() { // Check to see if user has clicked the "OK" button if (mOKButton.IsMouseOver()) { if (mOKButton.Texture == mOKTexture) { SoundManager.PlayEffect("Click"); } mOKButton.Play("Selected"); MouseState mouse = Mouse.GetState(); if (mouse.LeftButton == ButtonState.Pressed) { mVisible = false; SoundManager.PlayEffect("Press"); GenericBaseApplication.GameManager.Exit(); } } else { mOKButton.Play("Neutral"); } } /// /// Draws the window to the given Spritebatch /// /// public void Draw(SpriteBatch spriteBatch) { if (mVisible) { // Draw the basic window background first spriteBatch.Begin(SpriteBlendMode.AlphaBlend); mBackgroundImage.Draw(spriteBatch); spriteBatch.End(); // Now draw the scores, using the precomputed values to determine their locations spriteBatch.Begin(SpriteBlendMode.AlphaBlend); mOKButton.Draw(spriteBatch); // Tag the resources that were collected last game Dictionary mNonZeroResourceIcons = new Dictionary(); foreach (KeyValuePair kvp in mResourceIcons) { if (mIndividualScores[kvp.Key] != 0) mNonZeroResourceIcons.Add(kvp.Key, kvp.Value); } foreach (KeyValuePair kvp in mResourceIcons) { if (mNonZeroResourceIcons.ContainsKey(kvp.Key)) { // Display the collected resources at full opacity kvp.Value.Opacity = 1f; kvp.Value.Draw(spriteBatch); GenericBaseApplication.GameManager.StandardFont.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .4f), (kvp.Value.Y)), kvp.Key, Color.Aqua); GenericBaseApplication.GameManager.StandardFont.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .7f), (kvp.Value.Y)), mIndividualScores[kvp.Key] + "", Color.Aqua); } else { // Display the non-collected resources very faintly kvp.Value.Opacity = .2f; kvp.Value.Draw(spriteBatch); GenericBaseApplication.GameManager.StandardFont.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .4f), (kvp.Value.Y)), kvp.Key, new Color(new Vector4(1f, 1f, 1f, .2f))); } } // Draw the top part of the window GenericBaseApplication.GameManager.StandardFontBig.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .4f), (AstroBaseApplication.GameManager.ScreenHeight * .118f)), "RESULTS:", Color.Aqua); if (mSuccess) { GenericBaseApplication.GameManager.StandardFontBig.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .6f), (AstroBaseApplication.GameManager.ScreenHeight * .118f)), mResultsMessage, Color.Chartreuse); } else { GenericBaseApplication.GameManager.StandardFontBig.DrawString(new Vector2((AstroBaseApplication.GameManager.ScreenWidth * .6f), (AstroBaseApplication.GameManager.ScreenHeight * .118f)), mResultsMessage, Color.Orange); } spriteBatch.End(); } } } }