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
22 Texture2D selectGameScreen;
24 Vector2 backgroundPos;
26 Vector2 spotLightCenter;
30 Vector2 spotLightVelocity;
38 Texture2D deselectBox;
39 Texture2D emptySelectBox;
46 Vector2 createGamePos;
47 string createGameText;
51 Vector2 returnToMainPos;
52 string returnToMainText;
54 KeyboardState previousKeyboardState;
55 KeyboardState currentKeyboardState;
57 GamerCollection<NetworkGamer> players;
58 LocalNetworkGamer localPlayer;
59 AvailableNetworkSessionCollection availableSessions;
62 int selectedSessionIndex;
66 Queue<ChatInfo> currentChat;
68 private enum lobbyState
73 FindingGames, // TODO: New state.
74 JoiningGame, // TODO: New state.
78 lobbyState currentState;
82 currentState = lobbyState.Welcome;
83 selectedSessionIndex = 0;
87 currentChat = new Queue<ChatInfo>();
90 public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics)
92 background = contentManager.Load<Texture2D>("background");
93 spotLight = contentManager.Load<Texture2D>("spotlight");
94 cs = contentManager.Load<Texture2D>("cs");
95 selectGameScreen = contentManager.Load<Texture2D>("selectGameScreen");
96 backgroundPos = new Vector2(0f, 0f);
97 spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98);
98 spotLightCenter = new Vector2(800f, 800f);
99 spotLightVelocity = new Vector2(-100, 33);
100 csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98);
102 zero = new Vector2(0, 0);
104 MaxX = graphics.GraphicsDevice.Viewport.Width;
106 MaxY = graphics.GraphicsDevice.Viewport.Height;
109 scale = MaxX / 1600f;
112 checkedBox = contentManager.Load<Texture2D>("checkedBox");
113 deselectBox = contentManager.Load<Texture2D>("deselectBox");
114 emptySelectBox = contentManager.Load<Texture2D>("emptySelectBox");
115 menuItem = contentManager.Load<Texture2D>("menuItem");
120 menuFont = contentManager.Load<SpriteFont>("menuFont");
121 createGamePos = new Vector2(100f, MaxY / 3);
122 createGameText = "Create Game";
123 selected = createGameText;
125 findGamePos = new Vector2(100f, (MaxY / 3) + 60);
126 findGameText = "Find Game";
128 returnToMainPos = new Vector2(MaxX / 2, MaxY - 120);
129 returnToMainText = "press [ X ] to return to main menu";
134 public void UnloadContent()
140 void JoinedSession(NetworkSession session, NetworkGame networkGame)
144 currentState = lobbyState.Connected;
148 // TODO: This should do something more than just throw the player back to the welcome screen.
149 currentState = lobbyState.Welcome;
150 Console.WriteLine("Couldn't create/join the session.");
155 void FoundSessions(AvailableNetworkSessionCollection sessions, NetworkGame networkGame)
157 availableSessions = sessions;
161 void AsyncCallbackFailed(Exception exception, NetworkGame networkGame)
163 currentState = lobbyState.Welcome;
164 Console.WriteLine("Exception as thrown during async call: " + exception.Message);
167 private void UpdateChat(GameTime gameTime, NetworkGame networkGame)
169 List<ChatInfo> chts = networkGame.ReceiveChats();
170 for (int x = 0; x < chts.Count(); x++)
171 currentChat.Enqueue(chts[x]);
173 //if number of chat messages has reached max remove older messages as new ones are added
174 if (currentChat.Count() > 8)
176 for (int x = 0; x < chts.Count(); x++)
178 currentChat.Dequeue();
184 public long Update(GameTime gameTime, NetworkGame networkGame)
187 UpdateSpotLight(gameTime);
188 currentKeyboardState = Keyboard.GetState();
190 if (networkGame.HasActiveSession)
192 players = networkGame.NetworkGamers;
196 switch (currentState)
198 case lobbyState.Welcome:
199 if (selected == createGameText)
201 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
202 currentState = lobbyState.CreateGame;
203 if (currentKeyboardState.IsKeyDown(Keys.Down))
204 selected = findGameText;
208 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
209 currentState = lobbyState.FindGame;
210 if (currentKeyboardState.IsKeyDown(Keys.Up))
211 selected = createGameText;
215 case lobbyState.CreateGame:
216 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
218 currentState = lobbyState.Welcome;
220 if (networkGame.HasActiveSession)
223 networkGame.LeaveSession();
226 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
228 currentState = lobbyState.JoiningGame;
229 networkGame.ErrorDelegate = AsyncCallbackFailed;
230 networkGame.CreateSession(JoinedSession);
234 case lobbyState.FindGame:
235 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
237 currentState = lobbyState.Welcome;
240 availableSessions = null;
241 networkGame.ErrorDelegate = AsyncCallbackFailed;
242 networkGame.FindSessions(FoundSessions);
243 currentState = lobbyState.FindingGames;
246 case lobbyState.FindingGames:
247 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
249 currentState = lobbyState.Welcome;
252 if (availableSessions != null && availableSessions.Count == 0)
253 currentState = lobbyState.FindGame;
254 else if (availableSessions != null && availableSessions.Count > 0)
256 if (currentKeyboardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
258 networkGame.JoinSession(availableSessions[0], JoinedSession);
259 currentState = lobbyState.JoiningGame;
261 availableSessions.Dispose();
262 availableSessions = null;
264 if (currentKeyboardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
266 networkGame.JoinSession(availableSessions[0], JoinedSession);
267 currentState = lobbyState.JoiningGame;
269 availableSessions.Dispose();
270 availableSessions = null;
276 case lobbyState.Connected:
277 if (chatActive) //If chat is activated by pressing T all inputs go to chat. Enter finishes chat esc returns
279 if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
284 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
286 networkGame.SendChat(chatMessage);
290 Keys[] k = currentKeyboardState.GetPressedKeys();
291 Keys[] kp = previousKeyboardState.GetPressedKeys();
292 List<Keys> newKeys = new List<Keys>();
294 for (int x = 0; x < k.Count(); x++) //copy new keys into array
296 if (!kp.Contains(k[x]))
302 foreach(Keys ky in newKeys)
304 if (ky.Equals(Keys.Back))
305 chatMessage = chatMessage.Substring(0, chatMessage.Length-1);
306 else if(ky.Equals(Keys.Space))
307 chatMessage = chatMessage + " ";
309 chatMessage = chatMessage + ky.ToString();
315 UpdateChat(gameTime, networkGame);
317 if (currentKeyboardState.IsKeyDown(Keys.T) && previousKeyboardState.IsKeyUp(Keys.T))
321 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
325 if (networkGame.HasActiveSession)
328 networkGame.LeaveSession();
330 currentState = lobbyState.Welcome;
333 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
334 networkGame.LocalGamer.IsReady = true;
336 if (networkGame.HasActiveSession)
338 localPlayer = networkGame.LocalGamer;
339 players = networkGame.NetworkGamers;
343 foreach (NetworkGamer p in players)
344 if (p.IsReady == false)
351 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
352 if (allReady && players.Count == 2 && localPlayer == players[0])
354 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
356 networkGame.ForceStartGame();
361 currentState = lobbyState.Welcome;
366 previousKeyboardState = Keyboard.GetState();
372 /// Draws the lobby GUI. Has different states for difference menu configurations
374 public long Draw(SpriteBatch spriteBatch)
376 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
377 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
378 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
380 switch (currentState)
382 case lobbyState.Welcome:
383 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
384 if (selected == createGameText)
385 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
387 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
388 if (selected == findGameText)
389 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
391 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
394 case lobbyState.CreateGame:
395 DrawPlayerList(spriteBatch);
396 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
397 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
398 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);
402 case lobbyState.FindingGames:
403 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
404 spriteBatch.DrawString(menuFont, "select game by pressing listed games index", new Vector2(250, 400), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
405 if(availableSessions == null)
406 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
407 else if (availableSessions.Count == 0)
408 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
411 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
413 Color color = Color.Gray;
415 if (sessionIndex == selectedSessionIndex)
418 spriteBatch.DrawString(menuFont, sessionIndex+1 + " " + availableSessions[sessionIndex].HostGamertag, new Vector2(150, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
419 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / 4",
420 new Vector2(450, 125 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
425 case lobbyState.Connected:
426 DrawPlayerList(spriteBatch);
427 DrawChatInfo(spriteBatch);
428 if(allReady && players.Count == 2 && localPlayer == players[0])
429 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
431 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
433 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
440 private void UpdateSpotLight(GameTime gameTime)
442 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
443 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
445 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
447 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
449 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
451 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
455 private void DrawChatInfo(SpriteBatch spriteBatch)
457 if (currentChat.Count > 0)
459 for (int y = 0; y < currentChat.Count; y++)
461 spriteBatch.DrawString(menuFont, currentChat.ElementAt(y).Sender + ": " + currentChat.ElementAt(y).Message
462 , new Vector2(400, 10 + y*15), Color.Blue, 0f, zero, .6f, SpriteEffects.None, 1f);
467 spriteBatch.DrawString(menuFont, chatMessage, new Vector2(400, 10 + 15*(currentChat.Count + 1)), Color.Green, 0f, zero, .6f, SpriteEffects.None, 1f);
470 private void DrawPlayerList(SpriteBatch spriteBatch)
474 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
477 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
478 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
479 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
480 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 80), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
481 spriteBatch.DrawString(menuFont, "enter chat mode", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
482 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
483 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
484 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 80), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
485 spriteBatch.DrawString(menuFont, "T", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
488 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
489 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
490 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
491 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
494 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
495 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
496 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
497 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
502 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
503 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
504 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
505 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
509 if (!(players.Count >= 1 && players[0].IsReady))
510 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
512 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
513 if (!(players.Count >= 2 && players[1].IsReady))
514 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
516 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
517 if (!(players.Count >= 3 && players[2].IsReady))
518 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
520 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
521 if (!(players.Count >= 4 && players[3].IsReady))
522 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
524 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
528 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
529 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
530 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
531 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
534 Boolean chatwith = true; // change to reflect info from network, move to update and create one for each player
536 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
538 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
541 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
543 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
546 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
548 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
551 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
553 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
559 for (int g = 0; g < players.Count; g++)
561 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);