Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region File Description
- //-----------------------------------------------------------------------------
- // ScreenManager.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System.Diagnostics;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input.Touch;
- #endregion
- namespace SumoGame.Screens
- {
- /// <summary>
- /// The screen manager is a component which manages one or more GameScreen
- /// instances. It maintains a stack of screens, calls their Update and Draw
- /// methods at the appropriate times, and automatically routes input to the
- /// topmost active screen.
- /// </summary>
- public class ScreenManager : DrawableGameComponent
- {
- #region Fields
- List<GameScreen> screens = new List<GameScreen>();
- List<GameScreen> screensToUpdate = new List<GameScreen>();
- InputState input = new InputState();
- AnimatedAsset[] animatedAssets = new AnimatedAsset[15];
- SumoPlayer[] players = new SumoPlayer[2];
- Rectangle gameRect = new Rectangle(0, 0, 1920, 1080);
- SpriteBatch spriteBatch;
- SpriteFont font;
- Texture2D blankTexture;
- bool isInitialized;
- bool traceEnabled;
- #endregion
- #region Properties
- /// <summary>
- /// A default SpriteBatch shared by all the screens. This saves
- /// each screen having to bother creating their own local instance.
- /// </summary>
- public SpriteBatch SpriteBatch
- {
- get { return spriteBatch; }
- }
- public InputState InputState
- {
- get { return input; }
- }
- public Rectangle GameRect
- {
- get { return gameRect; }
- }
- /// <summary>
- /// Return the asset descriptions
- /// </summary>
- public AnimatedAsset[] Assets
- {
- get { return animatedAssets; }
- }
- /// <summary>
- /// Current players
- /// </summary>
- public SumoPlayer[] Players
- {
- get { return players; }
- }
- /// <summary>
- /// A default font shared by all the screens. This saves
- /// each screen having to bother loading their own local copy.
- /// </summary>
- public SpriteFont Font
- {
- get { return font; }
- }
- /// <summary>
- /// If true, the manager prints out a list of all the screens
- /// each time it is updated. This can be useful for making sure
- /// everything is being added and removed at the right times.
- /// </summary>
- public bool TraceEnabled
- {
- get { return traceEnabled; }
- set { traceEnabled = value; }
- }
- #endregion
- #region Initialization
- /// <summary>
- /// Constructs a new screen manager component.
- /// </summary>
- public ScreenManager(Game game)
- : base(game)
- {
- // we must set EnabledGestures before we can query for them, but
- // we don't assume the game wants to read them.
- // TouchPanel.EnabledGestures = GestureType.None;
- }
- /// <summary>
- /// Initializes the screen manager component.
- /// </summary>
- public override void Initialize()
- {
- base.Initialize();
- isInitialized = true;
- }
- /// <summary>
- /// Load your graphics content.
- /// </summary>
- protected override void LoadContent()
- {
- // Load content belonging to the screen manager.
- ContentManager content = Game.Content;
- spriteBatch = new SpriteBatch(GraphicsDevice);
- font = content.Load<SpriteFont>("UIFont");
- blankTexture = content.Load<Texture2D>("blank");
- // Define AnimatedAssets[] := [name, frames]
- animatedAssets[0] = new AnimatedAsset("bout", 4);
- animatedAssets[1] = new AnimatedAsset("bowOpponent", 12);
- animatedAssets[2] = new AnimatedAsset("bowUser", 12);
- animatedAssets[3] = new AnimatedAsset("bowWin", 40);
- animatedAssets[4] = new AnimatedAsset("clapUp", 20);
- animatedAssets[5] = new AnimatedAsset("crouch", 30);
- animatedAssets[6] = new AnimatedAsset("fists", 10);
- animatedAssets[7] = new AnimatedAsset("loss1", 12);
- animatedAssets[8] = new AnimatedAsset("loss2", 10);
- animatedAssets[9] = new AnimatedAsset("salt", 18);
- animatedAssets[10] = new AnimatedAsset("stomp", 55);
- animatedAssets[11] = new AnimatedAsset("win1", 10);
- animatedAssets[12] = new AnimatedAsset("win2", 12);
- animatedAssets[13] = new AnimatedAsset("confetti", 2);
- animatedAssets[14] = new AnimatedAsset("break", 8);
- players[0] = new SumoPlayer(SumoPlayer.LEFT,
- "Mitko", Color.Red); // left
- players[0].PlayerFlagName = new string[] { "Bulgaria" };
- players[1] = new SumoPlayer(SumoPlayer.RIGHT,
- "Mitko", Color.Blue); // right
- players[1].PlayerFlagName = new string[] { "Sofia" };
- // Tell each of the screens to load their content.
- foreach (GameScreen screen in screens)
- {
- screen.LoadContent();
- }
- }
- /// <summary>
- /// Unload your graphics content.
- /// </summary>
- protected override void UnloadContent()
- {
- // Tell each of the screens to unload their content.
- foreach (GameScreen screen in screens)
- {
- screen.UnloadContent();
- }
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// Allows each screen to run logic.
- /// </summary>
- public override void Update(GameTime gameTime)
- {
- // Read the keyboard and gamepad.
- input.Update(gameTime);
- // Make a copy of the master screen list, to avoid confusion if
- // the process of updating one screen adds or removes others.
- screensToUpdate.Clear();
- foreach (GameScreen screen in screens)
- {
- if (screen.KeepAlive == false)
- {
- screensToUpdate.Add(screen);
- }
- }
- bool otherScreenHasFocus = !Game.IsActive;
- bool coveredByOtherScreen = false;
- // Loop as long as there are screens waiting to be updated.
- while (screensToUpdate.Count > 0)
- {
- // Pop the topmost screen off the waiting list.
- GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
- screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
- // Update the screen.
- screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
- if (screen.ScreenState == ScreenState.TransitionOn ||
- screen.ScreenState == ScreenState.Active)
- {
- // If this is the first active screen we came across,
- // give it a chance to handle input.
- if (!otherScreenHasFocus)
- {
- screen.HandleInput(input);
- otherScreenHasFocus = true;
- }
- // If this is an active non-popup, inform any subsequent
- // screens that they are covered by it.
- if (!screen.IsPopup)
- coveredByOtherScreen = true;
- }
- }
- // Print debug trace?
- if (traceEnabled)
- TraceScreens();
- }
- /// <summary>
- /// Prints a list of all the screens, for debugging.
- /// </summary>
- void TraceScreens()
- {
- List<string> screenNames = new List<string>();
- foreach (GameScreen screen in screens)
- screenNames.Add(screen.GetType().Name);
- Debug.WriteLine(string.Join(", ", screenNames.ToArray()));
- }
- /// <summary>
- /// Tells each screen to draw itself.
- /// </summary>
- public override void Draw(GameTime gameTime)
- {
- foreach (GameScreen screen in screens)
- {
- if (screen.ScreenState == ScreenState.Hidden || screen.KeepAlive == true)
- continue;
- screen.Draw(gameTime);
- }
- }
- #endregion
- #region Public Methods
- public AnimatedAsset GetAnimatedAsset(string name)
- {
- for (int i = 0; i < animatedAssets.Length; i++)
- if (animatedAssets[i].Name.Equals(name))
- return animatedAssets[i];
- return null;
- }
- /// <summary>
- /// Adds a new screen to the screen manager.
- /// </summary>
- public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
- {
- bool same = false;
- foreach(GameScreen one in screens) {
- if (one.GetType().Name.Equals(screen.GetType().Name))
- {
- same = true;
- }
- }
- if (same == false)
- {
- screen.ControllingPlayer = controllingPlayer;
- screen.ScreenManager = this;
- screen.IsExiting = false;
- // If we have a graphics device, tell the screen to load content.
- if (isInitialized)
- {
- screen.LoadContent();
- }
- screens.Add(screen);
- // update the TouchPanel to respond to gestures this screen is interested in
- // TouchPanel.EnabledGestures = screen.EnabledGestures;
- }
- }
- public void hideScreenAtIndex(int index)
- {
- //ScreenState screenState = ScreenState.TransitionOn;
- screens[index].KeepAlive = true;
- }
- /// <summary>
- /// Removes a screen from the screen manager. You should normally
- /// use GameScreen.ExitScreen instead of calling this directly, so
- /// the screen can gradually transition off rather than just being
- /// instantly removed.
- /// </summary>
- public void RemoveScreen(GameScreen screen)
- {
- // If we have a graphics device, tell the screen to unload content.
- if (isInitialized)
- {
- screen.UnloadContent();
- }
- screens.Remove(screen);
- screensToUpdate.Remove(screen);
- // if there is a screen still in the manager, update TouchPanel
- // to respond to gestures that screen is interested in.
- if (screens.Count > 0)
- {
- // TouchPanel.EnabledGestures = screens[screens.Count - 1].EnabledGestures;
- }
- }
- /// <summary>
- /// Expose an array holding all the screens. We return a copy rather
- /// than the real master list, because screens should only ever be added
- /// or removed using the AddScreen and RemoveScreen methods.
- /// </summary>
- public GameScreen[] GetScreens()
- {
- return screens.ToArray();
- }
- /// <summary>
- /// Helper draws a translucent black fullscreen sprite, used for fading
- /// screens in and out, and for darkening the background behind popups.
- /// </summary>
- public void FadeBackBufferToBlack(float alpha)
- {
- Viewport viewport = GraphicsDevice.Viewport;
- spriteBatch.Begin();
- spriteBatch.Draw(blankTexture,
- new Rectangle(0, 0, viewport.Width, viewport.Height),
- Color.Black * alpha);
- spriteBatch.End();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment