X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=Project06%2FCS%203505%20Project%2006%2FCS%203505%20Project%2006%2FLobbyGUI.cs;h=8f45156c015f8a770669d0a8ed35b5b0feecc83f;hb=775a5b88d6eca31edc79446cd43c2ae292197931;hp=3c157dc944b9042178446e385719cdcfb72451b7;hpb=fc7104b36d66d536d38ba38a4afae588c437f6c6;p=chaz%2Fcarfire 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 index 3c157dc..8f45156 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs @@ -14,6 +14,7 @@ namespace CS_3505_Project_06 { public class lobbyGUI : ILobby { + #region local variables float scale; Texture2D background; @@ -61,6 +62,12 @@ namespace CS_3505_Project_06 int selectedSessionIndex; + bool chatActive; + String chatMessage; + Queue currentChat; + + + private enum lobbyState { Welcome, @@ -72,12 +79,16 @@ namespace CS_3505_Project_06 } lobbyState currentState; + #endregion public lobbyGUI() { currentState = lobbyState.Welcome; selectedSessionIndex = 0; ready = false; + chatActive = false; + chatMessage = ""; + currentChat = new Queue(); } public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics) @@ -129,7 +140,9 @@ namespace CS_3505_Project_06 } - // TODO: New method. + /// + /// Transition into connected state + /// void JoinedSession(NetworkSession session, NetworkGame networkGame) { if (session != null) @@ -144,27 +157,46 @@ namespace CS_3505_Project_06 } } - // TODO: New method. + /// + /// Called when Async FindSession returns. Available sessions is then updated + /// void FoundSessions(AvailableNetworkSessionCollection sessions, NetworkGame networkGame) { availableSessions = sessions; + } - if (availableSessions != null && availableSessions.Count > 0) - { - networkGame.JoinSession(availableSessions[0], JoinedSession); - currentState = lobbyState.JoiningGame; + /// + /// Catches exceptions for and Async calls + /// + void AsyncCallbackFailed(Exception exception, NetworkGame networkGame) + { + currentState = lobbyState.Welcome; + Console.WriteLine("Exception as thrown during async call: " + exception.Message); + } - availableSessions.Dispose(); - availableSessions = null; - } - else + /// + /// Adds and new chats to the chat list. If chat list is full, older messages are removed. + /// + private void UpdateChat(GameTime gameTime, NetworkGame networkGame) + { + List chts = networkGame.ReceiveChats(); + for (int x = 0; x < chts.Count(); x++) + currentChat.Enqueue(chts[x]); + + //if number of chat messages has reached max remove older messages as new ones are added + if (currentChat.Count() > 8) { - // TODO: This should do something more than just throw the player back to the welcome screen. - currentState = lobbyState.Welcome; - Console.WriteLine("No sessions to join!"); + for (int x = 0; x < chts.Count(); x++) + { + currentChat.Dequeue(); + } } + } + /// + /// Main update call for Lobby, what is actually updated is determined by what the current state is. + /// public long Update(GameTime gameTime, NetworkGame networkGame) { @@ -210,6 +242,7 @@ namespace CS_3505_Project_06 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y)) { currentState = lobbyState.JoiningGame; + networkGame.ErrorDelegate = AsyncCallbackFailed; networkGame.CreateSession(JoinedSession); } break; @@ -220,51 +253,134 @@ namespace CS_3505_Project_06 currentState = lobbyState.Welcome; ready = false; } + availableSessions = null; + networkGame.ErrorDelegate = AsyncCallbackFailed; networkGame.FindSessions(FoundSessions); currentState = lobbyState.FindingGames; break; - case lobbyState.Connected: + case lobbyState.FindingGames: if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X)) { + currentState = lobbyState.Welcome; ready = false; - if (networkGame.HasActiveSession) + } + if (availableSessions != null && availableSessions.Count == 0) + currentState = lobbyState.FindGame; + else if (availableSessions != null && availableSessions.Count > 0) + { + if (currentKeyboardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1) && availableSessions.Count > 0) { - players = null; - networkGame.LeaveSession(); + networkGame.JoinSession(availableSessions[0], JoinedSession); } - currentState = lobbyState.Welcome; + else if (currentKeyboardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2) && availableSessions.Count > 1) + { + networkGame.JoinSession(availableSessions[1], JoinedSession); + } + else if (currentKeyboardState.IsKeyDown(Keys.D3) && previousKeyboardState.IsKeyUp(Keys.D3) && availableSessions.Count > 2) + { + networkGame.JoinSession(availableSessions[2], JoinedSession); + } + else if (currentKeyboardState.IsKeyDown(Keys.D4) && previousKeyboardState.IsKeyUp(Keys.D4) && availableSessions.Count > 3) + { + networkGame.JoinSession(availableSessions[3], JoinedSession); + } + + currentState = lobbyState.JoiningGame; + + availableSessions.Dispose(); + availableSessions = null; } - if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R)) - networkGame.LocalGamer.IsReady = true; - if (networkGame.HasActiveSession) + break; + + case lobbyState.Connected: + if (chatActive) //If chat is activated by pressing T all inputs go to chat. Enter finishes chat esc returns { - localPlayer = networkGame.LocalGamer; - players = networkGame.NetworkGamers; - if (players != null) + if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape)) { - allReady = true; - foreach (NetworkGamer p in players) - if (p.IsReady == false) - { - allReady = false; - break; - } + chatActive = false; + break; + } + if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter)) + { + networkGame.SendChat(chatMessage); + chatActive = false; + break; } + Keys[] k = currentKeyboardState.GetPressedKeys(); + Keys[] kp = previousKeyboardState.GetPressedKeys(); + List newKeys = new List(); - //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players - if(allReady && players.Count == 2 && localPlayer == players[0]) + for (int x = 0; x < k.Count(); x++) //copy new keys into array { - if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B)) + if (!kp.Contains(k[x])) { - networkGame.StartGame(); + newKeys.Add(k[x]); } } + + foreach(Keys ky in newKeys) + { + if (ky.Equals(Keys.Back)) + chatMessage = chatMessage.Substring(0, chatMessage.Length-1); + else if(ky.Equals(Keys.Space)) + chatMessage = chatMessage + " "; + else + chatMessage = chatMessage + ky.ToString(); + } + } - else - currentState = lobbyState.Welcome; + else //normal op mode + { + UpdateChat(gameTime, networkGame); + chatMessage = ""; + if (currentKeyboardState.IsKeyDown(Keys.T) && previousKeyboardState.IsKeyUp(Keys.T)) + { + chatActive = true; + } + if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X)) + { + ready = false; + currentChat.Clear(); + if (networkGame.HasActiveSession) + { + players = null; + networkGame.LeaveSession(); + } + currentState = lobbyState.Welcome; + } + if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R)) + networkGame.LocalGamer.IsReady = true; + + if (networkGame.HasActiveSession) + { + localPlayer = networkGame.LocalGamer; + players = networkGame.NetworkGamers; + if (players != null) + { + allReady = true; + foreach (NetworkGamer p in players) + if (p.IsReady == false) + { + allReady = false; + break; + } + } + + //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players + if (allReady && players.Count == 4 && localPlayer == players[0]) + { + if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B)) + { + networkGame.ForceStartGame(); + } + } + } + else + currentState = lobbyState.Welcome; + } break; } @@ -306,6 +422,7 @@ namespace CS_3505_Project_06 case lobbyState.FindingGames: spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0); + spriteBatch.DrawString(menuFont, "select game by pressing listed games index", new Vector2(250, 400), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f); if(availableSessions == null) spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f); else if (availableSessions.Count == 0) @@ -319,17 +436,17 @@ namespace CS_3505_Project_06 if (sessionIndex == selectedSessionIndex) color = Color.Red; - spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f); - spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots, - new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f); + spriteBatch.DrawString(menuFont, sessionIndex+1 + " " + availableSessions[sessionIndex].HostGamertag, new Vector2(150, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f); + spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / 4", + new Vector2(450, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f); } } break; case lobbyState.Connected: DrawPlayerList(spriteBatch); - - if(allReady && players.Count == 2 && localPlayer == players[0]) + DrawChatInfo(spriteBatch); + if(allReady && players.Count == 4 && localPlayer == players[0]) spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f); else if(allReady) spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f); @@ -341,6 +458,9 @@ namespace CS_3505_Project_06 return 1; } + /// + /// Updates backgound animation with moving spotlight. Spotlight bounces off walls + /// private void UpdateSpotLight(GameTime gameTime) { spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, @@ -356,7 +476,27 @@ namespace CS_3505_Project_06 } } + /// + /// Draws the list of current chat messages received by the local client. + /// + private void DrawChatInfo(SpriteBatch spriteBatch) + { + if (currentChat.Count > 0) + { + for (int y = 0; y < currentChat.Count; y++) + { + spriteBatch.DrawString(menuFont, currentChat.ElementAt(y).Sender + ": " + currentChat.ElementAt(y).Message + , new Vector2(400, 10 + y*15), Color.Blue, 0f, zero, .6f, SpriteEffects.None, 1f); + } + + } + if(chatActive) + spriteBatch.DrawString(menuFont, chatMessage, new Vector2(400, 10 + 15*(currentChat.Count + 1)), Color.Green, 0f, zero, .6f, SpriteEffects.None, 1f); + } + /// + /// Draws player list objects for the connected state. + /// private void DrawPlayerList(SpriteBatch spriteBatch) { @@ -367,11 +507,13 @@ namespace CS_3505_Project_06 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White); spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f); spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f); - spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 80), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "enter chat mode", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f); spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f); spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f); - spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f); - + spriteBatch.DrawString(menuFont, "X", new Vector2(145, 80), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f); + spriteBatch.DrawString(menuFont, "T", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f); + //Background squares spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f); @@ -419,7 +561,7 @@ namespace CS_3505_Project_06 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f); //Chat CheckBoxs - Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player + Boolean chatwith = true; // 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 @@ -451,4 +593,4 @@ namespace CS_3505_Project_06 } } } -} +} \ No newline at end of file