2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Net;
6 using System.Diagnostics;
7 using Microsoft.Xna.Framework.GamerServices;
8 using Microsoft.Xna.Framework.Graphics;
9 using Microsoft.Xna.Framework;
10 using Microsoft.Xna.Framework.Input;
11 using Microsoft.Xna.Framework.Content;
13 namespace CS_3505_Project_06
15 public class lobbyGUI : ILobby
17 #region local variables
23 Texture2D selectGameScreen;
25 Vector2 backgroundPos;
27 Vector2 spotLightCenter;
31 Vector2 spotLightVelocity;
39 Texture2D deselectBox;
40 Texture2D emptySelectBox;
47 Vector2 createGamePos;
48 string createGameText;
52 Vector2 returnToMainPos;
53 string returnToMainText;
55 KeyboardState previousKeyboardState;
56 KeyboardState currentKeyboardState;
58 GamerCollection<NetworkGamer> players;
59 LocalNetworkGamer localPlayer;
60 AvailableNetworkSessionCollection availableSessions;
63 int selectedSessionIndex;
67 Queue<ChatInfo> currentChat;
71 private enum lobbyState
76 FindingGames, // TODO: New state.
77 JoiningGame, // TODO: New state.
81 lobbyState currentState;
86 currentState = lobbyState.Welcome;
87 selectedSessionIndex = 0;
91 currentChat = new Queue<ChatInfo>();
94 public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics)
96 background = contentManager.Load<Texture2D>("background");
97 spotLight = contentManager.Load<Texture2D>("spotlight");
98 cs = contentManager.Load<Texture2D>("cs");
99 selectGameScreen = contentManager.Load<Texture2D>("selectGameScreen");
100 backgroundPos = new Vector2(0f, 0f);
101 spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98);
102 spotLightCenter = new Vector2(800f, 800f);
103 spotLightVelocity = new Vector2(-100, 33);
104 csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98);
106 zero = new Vector2(0, 0);
108 MaxX = graphics.GraphicsDevice.Viewport.Width;
110 MaxY = graphics.GraphicsDevice.Viewport.Height;
113 scale = MaxX / 1600f;
116 checkedBox = contentManager.Load<Texture2D>("checkedBox");
117 deselectBox = contentManager.Load<Texture2D>("deselectBox");
118 emptySelectBox = contentManager.Load<Texture2D>("emptySelectBox");
119 menuItem = contentManager.Load<Texture2D>("menuItem");
124 menuFont = contentManager.Load<SpriteFont>("menuFont");
125 createGamePos = new Vector2(100f, MaxY / 3);
126 createGameText = "Create Game";
127 selected = createGameText;
129 findGamePos = new Vector2(100f, (MaxY / 3) + 60);
130 findGameText = "Find Game";
132 returnToMainPos = new Vector2(MaxX / 2, MaxY - 120);
133 returnToMainText = "press [ X ] to return to main menu";
138 public void UnloadContent()
144 /// Transition into connected state
146 void JoinedSession(NetworkSession session, NetworkGame networkGame)
150 currentState = lobbyState.Connected;
154 // TODO: This should do something more than just throw the player back to the welcome screen.
155 currentState = lobbyState.Welcome;
156 Console.WriteLine("Couldn't create/join the session.");
161 /// Called when Async FindSession returns. Available sessions is then updated
163 void FoundSessions(AvailableNetworkSessionCollection sessions, NetworkGame networkGame)
165 availableSessions = sessions;
169 /// Catches exceptions for and Async calls
171 void AsyncCallbackFailed(Exception exception, NetworkGame networkGame)
173 currentState = lobbyState.Welcome;
174 Console.WriteLine("Exception as thrown during async call: " + exception.Message);
178 /// Adds and new chats to the chat list. If chat list is full, older messages are removed.
180 private void UpdateChat(GameTime gameTime, NetworkGame networkGame)
182 List<ChatInfo> chts = networkGame.ReceiveChats();
183 for (int x = 0; x < chts.Count(); x++)
184 currentChat.Enqueue(chts[x]);
186 //if number of chat messages has reached max remove older messages as new ones are added
187 if (currentChat.Count() > 8)
189 for (int x = 0; x < chts.Count(); x++)
191 currentChat.Dequeue();
198 /// Main update call for Lobby, what is actually updated is determined by what the current state is.
200 public long Update(GameTime gameTime, NetworkGame networkGame)
203 UpdateSpotLight(gameTime);
204 currentKeyboardState = Keyboard.GetState();
206 if (networkGame.HasActiveSession)
208 players = networkGame.NetworkGamers;
212 switch (currentState)
214 case lobbyState.Welcome:
215 if (selected == createGameText)
217 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
218 currentState = lobbyState.CreateGame;
219 if (currentKeyboardState.IsKeyDown(Keys.Down))
220 selected = findGameText;
224 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
225 currentState = lobbyState.FindGame;
226 if (currentKeyboardState.IsKeyDown(Keys.Up))
227 selected = createGameText;
231 case lobbyState.CreateGame:
232 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
234 currentState = lobbyState.Welcome;
236 if (networkGame.HasActiveSession)
239 networkGame.LeaveSession();
242 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
244 currentState = lobbyState.JoiningGame;
245 networkGame.ErrorDelegate = AsyncCallbackFailed;
246 networkGame.CreateSession(JoinedSession);
250 case lobbyState.FindGame:
251 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
253 currentState = lobbyState.Welcome;
256 availableSessions = null;
257 networkGame.ErrorDelegate = AsyncCallbackFailed;
258 networkGame.FindSessions(FoundSessions);
259 currentState = lobbyState.FindingGames;
262 case lobbyState.FindingGames:
263 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
265 currentState = lobbyState.Welcome;
268 if (availableSessions != null && availableSessions.Count == 0)
269 currentState = lobbyState.FindGame;
270 else if (availableSessions != null && availableSessions.Count > 0)
272 if (currentKeyboardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
274 networkGame.JoinSession(availableSessions[0], JoinedSession);
275 currentState = lobbyState.JoiningGame;
277 availableSessions.Dispose();
278 availableSessions = null;
280 if (currentKeyboardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
282 networkGame.JoinSession(availableSessions[0], JoinedSession);
283 currentState = lobbyState.JoiningGame;
285 availableSessions.Dispose();
286 availableSessions = null;
292 case lobbyState.Connected:
293 if (chatActive) //If chat is activated by pressing T all inputs go to chat. Enter finishes chat esc returns
295 if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
300 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
302 networkGame.SendChat(chatMessage);
306 Keys[] k = currentKeyboardState.GetPressedKeys();
307 Keys[] kp = previousKeyboardState.GetPressedKeys();
308 List<Keys> newKeys = new List<Keys>();
310 for (int x = 0; x < k.Count(); x++) //copy new keys into array
312 if (!kp.Contains(k[x]))
318 foreach(Keys ky in newKeys)
320 if (ky.Equals(Keys.Back))
321 chatMessage = chatMessage.Substring(0, chatMessage.Length-1);
322 else if(ky.Equals(Keys.Space))
323 chatMessage = chatMessage + " ";
325 chatMessage = chatMessage + ky.ToString();
329 else //normal op mode
331 UpdateChat(gameTime, networkGame);
333 if (currentKeyboardState.IsKeyDown(Keys.T) && previousKeyboardState.IsKeyUp(Keys.T))
337 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
341 if (networkGame.HasActiveSession)
344 networkGame.LeaveSession();
346 currentState = lobbyState.Welcome;
349 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
350 networkGame.LocalGamer.IsReady = true;
352 if (networkGame.HasActiveSession)
354 localPlayer = networkGame.LocalGamer;
355 players = networkGame.NetworkGamers;
359 foreach (NetworkGamer p in players)
360 if (p.IsReady == false)
367 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
368 if (allReady && players.Count == 4 && localPlayer == players[0])
370 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
372 networkGame.ForceStartGame();
377 currentState = lobbyState.Welcome;
382 previousKeyboardState = Keyboard.GetState();
388 /// Draws the lobby GUI. Has different states for difference menu configurations
390 public long Draw(SpriteBatch spriteBatch)
392 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
393 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
394 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
396 switch (currentState)
398 case lobbyState.Welcome:
399 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
400 if (selected == createGameText)
401 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
403 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
404 if (selected == findGameText)
405 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
407 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
410 case lobbyState.CreateGame:
411 DrawPlayerList(spriteBatch);
412 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
413 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
414 spriteBatch.DrawString(menuFont, "X to return to main menu", new Vector2(MaxX /2 + 40, (MaxY / 3) + 2 * menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
418 case lobbyState.FindingGames:
419 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
420 spriteBatch.DrawString(menuFont, "select game by pressing listed games index", new Vector2(250, 400), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
421 if(availableSessions == null)
422 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
423 else if (availableSessions.Count == 0)
424 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
427 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
429 Color color = Color.Gray;
431 if (sessionIndex == selectedSessionIndex)
434 spriteBatch.DrawString(menuFont, sessionIndex+1 + " " + availableSessions[sessionIndex].HostGamertag, new Vector2(150, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
435 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / 4",
436 new Vector2(450, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
441 case lobbyState.Connected:
442 DrawPlayerList(spriteBatch);
443 DrawChatInfo(spriteBatch);
444 if(allReady && players.Count == 2 && localPlayer == players[0])
445 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
447 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
449 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
457 /// Updates backgound animation with moving spotlight. Spotlight bounces off walls
459 private void UpdateSpotLight(GameTime gameTime)
461 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
462 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
464 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
466 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
468 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
470 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
475 /// Draws the list of current chat messages received by the local client.
477 private void DrawChatInfo(SpriteBatch spriteBatch)
479 if (currentChat.Count > 0)
481 for (int y = 0; y < currentChat.Count; y++)
483 spriteBatch.DrawString(menuFont, currentChat.ElementAt(y).Sender + ": " + currentChat.ElementAt(y).Message
484 , new Vector2(400, 10 + y*15), Color.Blue, 0f, zero, .6f, SpriteEffects.None, 1f);
489 spriteBatch.DrawString(menuFont, chatMessage, new Vector2(400, 10 + 15*(currentChat.Count + 1)), Color.Green, 0f, zero, .6f, SpriteEffects.None, 1f);
493 /// Draws player list objects for the connected state.
495 private void DrawPlayerList(SpriteBatch spriteBatch)
499 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
502 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
503 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
504 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
505 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 80), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
506 spriteBatch.DrawString(menuFont, "enter chat mode", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
507 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
508 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
509 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 80), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
510 spriteBatch.DrawString(menuFont, "T", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
513 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
514 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
515 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
516 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
519 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
520 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
521 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
522 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
527 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
528 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
529 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
530 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
534 if (!(players.Count >= 1 && players[0].IsReady))
535 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
537 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
538 if (!(players.Count >= 2 && players[1].IsReady))
539 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
541 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
542 if (!(players.Count >= 3 && players[2].IsReady))
543 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
545 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
546 if (!(players.Count >= 4 && players[3].IsReady))
547 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
549 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
553 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
554 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
555 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
556 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
559 Boolean chatwith = true; // change to reflect info from network, move to update and create one for each player
561 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
563 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
566 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
568 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
571 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
573 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
576 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
578 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
584 for (int g = 0; g < players.Count; g++)
586 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);