/******************************************************************************/ /*! \file Game1.cs \author Kevin Carey Core XNA game class Copyright © 2007 DigiPen(USA) Corporation. All rights reserved. */ /******************************************************************************/ #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion namespace Shooting_Range { /// /// This is the main type for your game /// Called Game1 by default in XNA. Never changed it. /// public class Game1 : Microsoft.Xna.Framework.Game { // Game1 Members GameFunctions gameFunctions; public KeyboardState previousKeyboardState; public MouseState previousMouseState; // Game1 constructor public Game1() { // Initialize members (stored in a class to make this file prettier!) gameFunctions = new GameFunctions(this); } /// /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { // Set the title Window.Title = "Egnar 85521C"; // Set the mouse position Mouse.SetPosition(gameFunctions.Graphics.DeviceManager.GraphicsDevice.Viewport.Width / 2, gameFunctions.Graphics.DeviceManager.GraphicsDevice.Viewport.Height / 2); // Set the input states previousMouseState = Mouse.GetState(); previousKeyboardState = Keyboard.GetState(); // Initialize the game members gameFunctions.Initialize(); base.Initialize(); } /// /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode. Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { gameFunctions.LoadGraphicsContent(); } } /// /// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { // Unload game content gameFunctions.Graphics.Content.Unload(); // Shutdown sounds gameFunctions.Sounds.Shutdown(); } } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Update based on input UpdateInput(gameTime.ElapsedGameTime); // Update the game gameFunctions.Update(gameTime.ElapsedGameTime); // Update the input states previousKeyboardState = Keyboard.GetState(); previousMouseState = Mouse.GetState(); base.Update(gameTime); } private void UpdateInput(TimeSpan deltaTime) { // Get the current input states KeyboardState currentKeyboardState = Keyboard.GetState(); MouseState currentMouseState = Mouse.GetState(); // If the Back button of Esc is pressed if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || (previousKeyboardState.IsKeyUp(Keys.Escape) && currentKeyboardState.IsKeyDown(Keys.Escape))) { switch (gameFunctions.State) { // In the GAME state case GameState.GAME: // Shutdown the game state gameFunctions.ShutdownGameState(); // Initialize the stats state gameFunctions.InitStatsState(); break; // In the STATS state case GameState.STATS: // Shutdon the stats state gameFunctions.ShutdownStatsState(); // Initialize the menu state gameFunctions.InitMenuState(); break; // In the CREDITS state case GameState.CREDITS: // Shutdown the credits state gameFunctions.ShutdownCreditsState(); // Initialize the menu state gameFunctions.InitMenuState(); break; // In the MENU state case GameState.MENU: // Exit the game this.Exit(); break; } } // If the left mouse button is pressed if ((previousMouseState.LeftButton == ButtonState.Released) && (currentMouseState.LeftButton == ButtonState.Pressed)) { switch (gameFunctions.State) { // In the GAME state case GameState.GAME: // If able to shoot a bullet if (gameFunctions.FailTimer == TimeSpan.MinValue) { // Spawn a bullet gameFunctions.SpawnBullet(); } break; // In the other states case GameState.MENU: case GameState.STATS: case GameState.CREDITS: // Do nothing break; } } // If the mouse position has changed if (previousMouseState.X != currentMouseState.X || previousMouseState.Y != currentMouseState.Y) { // Adjust the camera gameFunctions.Graphics.CameraData.LookAtPositionX += currentMouseState.X - previousMouseState.X; gameFunctions.Graphics.CameraData.LookAtPositionY += previousMouseState.Y - currentMouseState.Y; // If not in the game state if (gameFunctions.State != GameState.GAME) { // Move the cursor sprite gameFunctions.CursorSprite.PositionX = currentMouseState.X; gameFunctions.CursorSprite.PositionY = currentMouseState.Y; } } } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { // Clear the screen to black gameFunctions.Graphics.DeviceManager.GraphicsDevice.Clear(Color.Black); // Draw! gameFunctions.Draw(); base.Draw(gameTime); } } }