From: Charles Date: Sat, 20 Mar 2010 04:45:48 +0000 (+0000) Subject: initial network code with partial lobby integration X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=bc3db44f8f717780cce18d5384272f501984e446;p=chaz%2Fcarfire initial network code with partial lobby integration git-svn-id: https://bd85.net/svn/cs3505_group@13 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505 Project 06.csproj b/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505 Project 06.csproj index 41013cd..d0949de 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505 Project 06.csproj +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505 Project 06.csproj @@ -86,6 +86,9 @@ + + + diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/Content.contentproj b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/Content.contentproj index 04a7389..e766d8f 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/Content.contentproj +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/Content.contentproj @@ -55,6 +55,55 @@ FontDescriptionProcessor + + + background + TextureImporter + TextureProcessor + + + checkedBox + TextureImporter + TextureProcessor + + + cs + TextureImporter + TextureProcessor + + + deselectBox + TextureImporter + TextureProcessor + + + emptySelectBox + TextureImporter + TextureProcessor + + + menuItem + TextureImporter + TextureProcessor + + + selectGameScreen + TextureImporter + TextureProcessor + + + spotlight + TextureImporter + TextureProcessor + + + + + menuFont + FontDescriptionImporter + FontDescriptionProcessor + + + + + + + Times New Roman + + + 18 + + + 0 + + + true + + + + + + + + + + + + ~ + + + + diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/menuItem.png b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/menuItem.png new file mode 100644 index 0000000..cf45ebd Binary files /dev/null and b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/menuItem.png differ diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/selectGameScreen.png b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/selectGameScreen.png new file mode 100644 index 0000000..af04c0c Binary files /dev/null and b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/selectGameScreen.png differ diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/spotlight.png b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/spotlight.png new file mode 100644 index 0000000..d8c3381 Binary files /dev/null and b/Project06/CS 3505 Project 06/CS 3505 Project 06/Content/spotlight.png differ diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs index 0a4a228..2d8fb75 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/Game06.cs @@ -21,19 +21,12 @@ namespace CS_3505_Project_06 { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; - SpriteFont font; - - IDeterministicGame deterministicGame; - TimeSpan targetTimeSpan; - Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug. - // For debugging + NetworkGame networkGame; + + ILobby lobby; + IDeterministicGame deterministicGame; - List lastPressedKeys; - bool lastButtonPressed; - Object activePlayer; - bool paused; - long lastAutoPause; // Constructor @@ -44,13 +37,9 @@ namespace CS_3505_Project_06 // Make the game object. The game is currently called 'testHarness'. + lobby = new lobbyGUI(); deterministicGame = new TestHarness(); - - // Debugging setup - - lastPressedKeys = new List(); - activePlayer = playerIdentifiers[0]; - paused = false; + networkGame = new NetworkGame(lobby, deterministicGame); } /// @@ -63,13 +52,8 @@ namespace CS_3505_Project_06 { // Set a fixed time span of 1/60th of a second. - targetTimeSpan = new TimeSpan(166666); // In 100 nanosecond units = 16 666 600 nanoseconds IsFixedTimeStep = true; - TargetElapsedTime = targetTimeSpan; - - // Reset the game - indicate that player #1 (player 0) owns this instance of the game. - - deterministicGame.ResetGame(playerIdentifiers, playerIdentifiers[0]); + TargetElapsedTime = networkGame.TargetTimeSpan; // For debugging - reset the mouse position to the center of the window. @@ -90,10 +74,9 @@ namespace CS_3505_Project_06 spriteBatch = new SpriteBatch(GraphicsDevice); - // Let the game load its content. - - font = Content.Load("InstructionFont"); + networkGame.font = Content.Load("InstructionFont"); + lobby.LoadContent(Content, graphics); deterministicGame.LoadContent(Content); } @@ -103,6 +86,7 @@ namespace CS_3505_Project_06 /// protected override void UnloadContent() { + lobby.UnloadContent(); deterministicGame.UnloadContent(); } @@ -113,89 +97,9 @@ namespace CS_3505_Project_06 /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { - // Get user's input state. - - KeyboardState keyState = Keyboard.GetState(); - MouseState mouseState = Mouse.GetState(); - - // Make a list of the keys pressed or released this frame. - - List pressedKeys = new List(); - List releasedKeys = new List(); - - Keys[] pressedKeysArray = keyState.GetPressedKeys(); - foreach (Keys k in pressedKeysArray) - if (!lastPressedKeys.Contains(k)) - pressedKeys.Add(k); - else - lastPressedKeys.Remove(k); - - releasedKeys = lastPressedKeys; - lastPressedKeys = new List(pressedKeysArray); - - // Get mouse button state. - - bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed; - - /***** Begining of game logic. *****/ - - // Debug - allow user to exit. - - if (pressedKeys.Contains(Keys.Escape)) - this.Exit(); - - // Debug - allow user on this machine to direct input to any player's state in the game. - - if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0]; - if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1]; - if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2]; - if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3]; - - // Debug - allow user on this machine to pause/resume game state advances. - - if (pressedKeys.Contains(Keys.F12) || - pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl))) - { - paused = !paused; - return; // Don't update on pause start or stop - } - - // Debug - automatically pause every 1000 frames. - - if (deterministicGame.CurrentFrameNumber % 1000 == 0 && deterministicGame.CurrentFrameNumber != lastAutoPause) - { - paused = true; - lastAutoPause = deterministicGame.CurrentFrameNumber; - } - - // Game update - - // Direct inputs to the game engine - only report changes. - - foreach (Keys k in pressedKeys) - deterministicGame.ApplyKeyInput(activePlayer, k, true); - - foreach (Keys k in releasedKeys) - deterministicGame.ApplyKeyInput(activePlayer, k, false); - - deterministicGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y); - - if (lastButtonPressed != buttonPressed) - deterministicGame.ApplyMouseButtonInput(activePlayer, buttonPressed); - - lastButtonPressed = buttonPressed; - - if (!paused) - { - // Advance the game engine. - - deterministicGame.Update(targetTimeSpan); - } - - /***** End of game logic. *****/ + networkGame.Update(gameTime); // Allow the superclass to do any needed updates (unknown purpose). - base.Update(gameTime); } @@ -209,18 +113,8 @@ namespace CS_3505_Project_06 spriteBatch.Begin(); - // Draw a few instructions. - - if (paused && gameTime.TotalRealTime.Milliseconds < 500) - spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White); - - spriteBatch.DrawString(font, "Press [F1]...[F4] to simulate input for each player. Click X's to end game or terminate player.", new Vector2(10, 540), Color.White); - spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White); - // Let the game draw itself. - - deterministicGame.Draw(spriteBatch); - + networkGame.Draw(gameTime, spriteBatch); spriteBatch.End(); diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/ILobby.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/ILobby.cs new file mode 100644 index 0000000..3b0f6c6 --- /dev/null +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/ILobby.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Content; + +namespace CS_3505_Project_06 +{ + public interface ILobby + { + void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics); + void UnloadContent(); + long Update(GameTime gameTime, NetworkGame networkGame); + long Draw(SpriteBatch spriteBatch); + } +} diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs new file mode 100644 index 0000000..b902822 --- /dev/null +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs @@ -0,0 +1,297 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace CS_3505_Project_06 +{ + public class lobbyGUI : ILobby + { + float scale; + + Texture2D background; + Texture2D spotLight; + Texture2D cs; + Texture2D selectGameScreen; + + Vector2 backgroundPos; + Vector2 spotLightPos; + Vector2 spotLightCenter; + Vector2 csPos; + + Vector2 zero; + Vector2 spotLightVelocity; + + int MaxX; + int MinX; + int MaxY; + int MinY; + + Texture2D checkedBox; + Texture2D deselectBox; + Texture2D emptySelectBox; + Texture2D menuItem; + + Boolean ready; + + SpriteFont menuFont; + string selected; + Vector2 createGamePos; + string createGameText; + Vector2 findGamePos; + string findGameText; + + Vector2 returnToMainPos; + string returnToMainText; + + KeyboardState previousKeyboardState; + KeyboardState currentKeyboardState; + + private enum lobbyState + { + Welcome, + CreateGame, + FindGame, + Connected + } + + lobbyState currentState; + + public lobbyGUI() + { + currentState = lobbyState.Welcome; + } + + public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics) + { + background = contentManager.Load("background"); + spotLight = contentManager.Load("spotlight"); + cs = contentManager.Load("cs"); + selectGameScreen = contentManager.Load("selectGameScreen"); + backgroundPos = new Vector2(0f, 0f); + spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98); + spotLightCenter = new Vector2(800f, 800f); + spotLightVelocity = new Vector2(-100, 33); + csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98); + + zero = new Vector2(0, 0); + + MaxX = graphics.GraphicsDevice.Viewport.Width; + MinX = 0; + MaxY = graphics.GraphicsDevice.Viewport.Height; + MinY = 100; + + scale = MaxX / 1600f; + //playerlist stuff + checkedBox = contentManager.Load("checkedBox"); + deselectBox = contentManager.Load("deselectBox"); + emptySelectBox = contentManager.Load("emptySelectBox"); + menuItem = contentManager.Load("menuItem"); + + ready = false; + + //menu fonts + menuFont = contentManager.Load("menuFont"); + createGamePos = new Vector2(100f, MaxY / 3); + createGameText = "Create Game"; + selected = createGameText; + + findGamePos = new Vector2(100f, (MaxY / 3) + 60); + findGameText = "Find Game"; + + returnToMainPos = new Vector2(MaxX / 2, MaxY - 120); + returnToMainText = "press [ X ] to return to main menu"; + + + } + + public void UnloadContent() + { + + } + + public long Update(GameTime gameTime, NetworkGame networkGame) + { + UpdateSpotLight(gameTime); + currentKeyboardState = Keyboard.GetState(); + //check inputs + switch (currentState) + { + case lobbyState.Welcome: + if (selected == createGameText) + { + if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter)) + currentState = lobbyState.CreateGame; + if (currentKeyboardState.IsKeyDown(Keys.Down)) + selected = findGameText; + } + else + { + if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter)) + currentState = lobbyState.FindGame; + if (currentKeyboardState.IsKeyDown(Keys.Up)) + selected = createGameText; + } + break; + case lobbyState.CreateGame: + if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X)) + { + currentState = lobbyState.Welcome; + ready = false; + } + if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R)) + ready = true; + break; + case lobbyState.FindGame: + if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X)) + { + currentState = lobbyState.Welcome; + ready = false; + } + + break; + case lobbyState.Connected: + if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X)) + currentState = lobbyState.Welcome; + if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R)) + ready = true; + break; + + } + previousKeyboardState = Keyboard.GetState(); + + return 1; + } + + /// + /// Draws the lobby GUI. Has different states for difference menu configurations + /// + public long Draw(SpriteBatch spriteBatch) + { + spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0); + spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0); + spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0); + switch (currentState) + { + case lobbyState.Welcome: + spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f); + if (selected == createGameText) + spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f); + else + spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f); + if (selected == findGameText) + spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f); + else + spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f); + break; + + case lobbyState.CreateGame: + DrawPlayerList(spriteBatch); + + break; + case lobbyState.FindGame: + spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0); + //spriteBatch.DrawString(menuFont, "Select Game", new Vector2(100, 100), Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f); + + //spriteBatch.DrawString(menuFont, returnToMainText, returnToMainPos, Color.Gray, 0, new Vector2(180 , 0), .6f, SpriteEffects.None, 0.5f); + break; + case lobbyState.Connected: + DrawPlayerList(spriteBatch); + break; + } + + return 1; + } + + private void UpdateSpotLight(GameTime gameTime) + { + spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, + spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds); + + if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall + { + spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y); + } + else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall + { + spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1); + } + } + + + private void DrawPlayerList(SpriteBatch spriteBatch) + { + + Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4); + spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White); + + //top player + spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "****Player 1****", new Vector2(topOfList.X + 10, topOfList.Y + 10), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + if(!ready) + spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player + if (!chatwith) + spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + //player 2 + spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "****Player 1****", new Vector2(topOfList.X + 10, topOfList.Y + 75), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + if (!ready) + spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + // change to reflect info from network + if (!chatwith) + spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + //player 3 + spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "****Player 1****", new Vector2(topOfList.X + 10, topOfList.Y + 140), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + if (!ready) + spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + + if (!chatwith) + spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + //player 4 + spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "****Player 1****", new Vector2(topOfList.X + 10, topOfList.Y + 205), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + if (!ready) + spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); + if (!chatwith) + spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + else + spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); + + } + } +} diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs new file mode 100644 index 0000000..abe36ba --- /dev/null +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs @@ -0,0 +1,258 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Net; +using System.Diagnostics; +using Microsoft.Xna.Framework.GamerServices; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace CS_3505_Project_06 +{ + public class NetworkGame + { + NetworkSession mNetworkSession; + + ILobby mLobby; + IDeterministicGame mGame; + + TimeSpan mTargetTimeSpan = new TimeSpan(166666); + public TimeSpan TargetTimeSpan + { + get + { + return mTargetTimeSpan; + } + } + + List lastPressedKeys; + bool lastButtonPressed; + + Object[] playerIdentifiers = { "One", "Two", "Three", "Four" }; // Any objects will do, strings are easy to debug. + + // For debugging + + Object activePlayer; + bool paused; + long lastAutoPause; + + public SpriteFont font; + + + public NetworkGame(ILobby lobby, IDeterministicGame game) + { + Debug.Assert(lobby != null && game != null); + + mLobby = lobby; + mGame = game; + + // Begin: Test harness stuff + lastPressedKeys = new List(); + activePlayer = playerIdentifiers[0]; + paused = false; + + // Reset the game - indicate that player #1 (player 0) owns this instance of the game. + + mGame.ResetGame(playerIdentifiers, playerIdentifiers[0]); + } + + + LocalNetworkGamer LocalGamer + { + get + { + return mNetworkSession.LocalGamers[0]; + } + } + + + NetworkSession CreateSession() + { + return CreateSession(mGame.MaximumSupportedPlayers); + } + + public NetworkSession CreateSession(int maxGamers) + { + Debug.Assert(mNetworkSession == null); + + mNetworkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 1, maxGamers); + mNetworkSession.AllowHostMigration = true; + mNetworkSession.AllowJoinInProgress = false; + + return mNetworkSession; + } + + + public AvailableNetworkSessionCollection FindSessions() + { + return NetworkSession.Find(NetworkSessionType.SystemLink, 1, new NetworkSessionProperties()); + } + + public NetworkSession JoinSession(AvailableNetworkSession availableSession) + { + Debug.Assert(mNetworkSession == null); + + mNetworkSession = NetworkSession.Join(availableSession); + + return mNetworkSession; + } + + + public void LeaveSession() + { + Debug.Assert(mNetworkSession != null); + + mNetworkSession.Dispose(); + mNetworkSession = null; + } + + + public void SimulateBadNetwork() + { + Debug.Assert(mNetworkSession != null); + + mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200); + mNetworkSession.SimulatedPacketLoss = 0.1f; + } + + + public void Update(GameTime gameTime) + { + if (mNetworkSession == null) + { + mLobby.Update(gameTime, this); + } + else + { + mNetworkSession.Update(); + + if (mNetworkSession.SessionState == NetworkSessionState.Lobby) + { + if (mNetworkSession.IsHost && + mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers && + mNetworkSession.IsEveryoneReady) + { + mNetworkSession.StartGame(); + mNetworkSession.ResetReady(); + } + else + { + mLobby.Update(gameTime, this); + } + } + else if (mNetworkSession.SessionState == NetworkSessionState.Playing) + { + // TODO: in-game update stuff + UpdateTestHarness(gameTime); + + mGame.Update(mTargetTimeSpan); + } + } + } + + public void Draw(GameTime gameTime, SpriteBatch spriteBatch) + { + mLobby.Draw(spriteBatch); + DrawTestHarness(gameTime, spriteBatch); + } + + + + + void UpdateTestHarness(GameTime gameTime) + { + // Get user's input state. + + KeyboardState keyState = Keyboard.GetState(); + MouseState mouseState = Mouse.GetState(); + + // Make a list of the keys pressed or released this frame. + + List pressedKeys = new List(); + List releasedKeys = new List(); + + Keys[] pressedKeysArray = keyState.GetPressedKeys(); + foreach (Keys k in pressedKeysArray) + if (!lastPressedKeys.Contains(k)) + pressedKeys.Add(k); + else + lastPressedKeys.Remove(k); + + releasedKeys = lastPressedKeys; + lastPressedKeys = new List(pressedKeysArray); + + // Get mouse button state. + + bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed; + + /***** Begining of game logic. *****/ + + // Debug - allow user on this machine to direct input to any player's state in the game. + + if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0]; + if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1]; + if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2]; + if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3]; + + // Debug - allow user on this machine to pause/resume game state advances. + + if (pressedKeys.Contains(Keys.F12) || + pressedKeys.Contains(Keys.P) && (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl))) + { + paused = !paused; + return; // Don't update on pause start or stop + } + + // Debug - automatically pause every 1000 frames. + + if (mGame.CurrentFrameNumber % 1000 == 0 && mGame.CurrentFrameNumber != lastAutoPause) + { + paused = true; + lastAutoPause = mGame.CurrentFrameNumber; + } + + + //if (pressedKeys.Contains(Keys.Escape)) + // this.Exit(); + + // Game update + + // Direct inputs to the game engine - only report changes. + + foreach (Keys k in pressedKeys) + mGame.ApplyKeyInput(activePlayer, k, true); + + foreach (Keys k in releasedKeys) + mGame.ApplyKeyInput(activePlayer, k, false); + + mGame.ApplyMouseLocationInput(activePlayer, mouseState.X, mouseState.Y); + + if (lastButtonPressed != buttonPressed) + mGame.ApplyMouseButtonInput(activePlayer, buttonPressed); + + lastButtonPressed = buttonPressed; + + if (!paused) + { + // Advance the game engine. + + mGame.Update(mTargetTimeSpan); + } + } + + void DrawTestHarness(GameTime gameTime, SpriteBatch spriteBatch) + { + + // BEGIN: Test harness stuff. + if (paused && gameTime.TotalRealTime.Milliseconds < 500) + spriteBatch.DrawString(font, "-=> Paused <=-", new Vector2(10, 130), Color.White); + + spriteBatch.DrawString(font, "Press [F1]...[F4] to simulate input for each player. Click X's to end game or terminate player.", new Vector2(10, 540), Color.White); + spriteBatch.DrawString(font, "Press [ESC] to exit and [F12] to pause/unpause. Game auto-pauses every 1000 frames.", new Vector2(10, 570), Color.White); + //END: Test harness stuff. + + } + } +}