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]);
172 //if number of chat messages has reached max remove older messages as new ones are added
173 if (currentChat.Count() > 8)
175 for (int x = 0; x < chts.Count(); x++)
177 currentChat.Dequeue();
183 public long Update(GameTime gameTime, NetworkGame networkGame)
186 UpdateSpotLight(gameTime);
187 currentKeyboardState = Keyboard.GetState();
189 if (networkGame.HasActiveSession)
191 players = networkGame.NetworkGamers;
195 switch (currentState)
197 case lobbyState.Welcome:
198 if (selected == createGameText)
200 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
201 currentState = lobbyState.CreateGame;
202 if (currentKeyboardState.IsKeyDown(Keys.Down))
203 selected = findGameText;
207 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
208 currentState = lobbyState.FindGame;
209 if (currentKeyboardState.IsKeyDown(Keys.Up))
210 selected = createGameText;
214 case lobbyState.CreateGame:
215 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
217 currentState = lobbyState.Welcome;
219 if (networkGame.HasActiveSession)
222 networkGame.LeaveSession();
225 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
227 currentState = lobbyState.JoiningGame;
228 networkGame.ErrorDelegate = AsyncCallbackFailed;
229 networkGame.CreateSession(JoinedSession);
233 case lobbyState.FindGame:
234 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
236 currentState = lobbyState.Welcome;
239 availableSessions = null;
240 networkGame.ErrorDelegate = AsyncCallbackFailed;
241 networkGame.FindSessions(FoundSessions);
242 currentState = lobbyState.FindingGames;
245 case lobbyState.FindingGames:
246 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
248 currentState = lobbyState.Welcome;
251 if (availableSessions != null && availableSessions.Count == 0)
252 currentState = lobbyState.FindGame;
253 else if (availableSessions != null && availableSessions.Count > 0)
255 if (currentKeyboardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
257 networkGame.JoinSession(availableSessions[0], JoinedSession);
258 currentState = lobbyState.JoiningGame;
260 availableSessions.Dispose();
261 availableSessions = null;
263 if (currentKeyboardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
265 networkGame.JoinSession(availableSessions[0], JoinedSession);
266 currentState = lobbyState.JoiningGame;
268 availableSessions.Dispose();
269 availableSessions = null;
275 case lobbyState.Connected:
276 if (chatActive) //If chat is activated by pressing T all inputs go to chat. Enter finishes chat esc returns
278 if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
283 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
285 networkGame.SendChat(chatMessage);
289 Keys[] k = currentKeyboardState.GetPressedKeys();
290 Keys[] kp = previousKeyboardState.GetPressedKeys();
291 List<Keys> newKeys = new List<Keys>();
293 for (int x = 0; x < k.Count(); x++) //copy new keys into array
295 if (!kp.Contains(k[x]))
301 foreach(Keys ky in newKeys)
303 if(ky.Equals(Keys.Space))
304 chatMessage = chatMessage + " ";
306 chatMessage = chatMessage + ky.ToString();
312 UpdateChat(gameTime, networkGame);
314 if (currentKeyboardState.IsKeyDown(Keys.T) && previousKeyboardState.IsKeyUp(Keys.T))
318 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
321 if (networkGame.HasActiveSession)
324 networkGame.LeaveSession();
326 currentState = lobbyState.Welcome;
329 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
330 networkGame.LocalGamer.IsReady = true;
332 if (networkGame.HasActiveSession)
334 localPlayer = networkGame.LocalGamer;
335 players = networkGame.NetworkGamers;
339 foreach (NetworkGamer p in players)
340 if (p.IsReady == false)
347 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
348 if (allReady && players.Count == 2 && localPlayer == players[0])
350 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
352 networkGame.ForceStartGame();
357 currentState = lobbyState.Welcome;
362 previousKeyboardState = Keyboard.GetState();
368 /// Draws the lobby GUI. Has different states for difference menu configurations
370 public long Draw(SpriteBatch spriteBatch)
372 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
373 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
374 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
376 switch (currentState)
378 case lobbyState.Welcome:
379 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
380 if (selected == createGameText)
381 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
383 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
384 if (selected == findGameText)
385 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
387 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
390 case lobbyState.CreateGame:
391 DrawPlayerList(spriteBatch);
392 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
393 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
394 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);
398 case lobbyState.FindingGames:
399 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
400 if(availableSessions == null)
401 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
402 else if (availableSessions.Count == 0)
403 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
406 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
408 Color color = Color.Gray;
410 if (sessionIndex == selectedSessionIndex)
413 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
414 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots,
415 new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
420 case lobbyState.Connected:
421 DrawPlayerList(spriteBatch);
422 DrawChatInfo(spriteBatch);
423 if(allReady && players.Count == 2 && localPlayer == players[0])
424 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
426 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
428 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
435 private void UpdateSpotLight(GameTime gameTime)
437 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
438 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
440 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
442 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
444 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
446 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
450 private void DrawChatInfo(SpriteBatch spriteBatch)
452 if (currentChat.Count > 0)
454 for (int y = 0; y < currentChat.Count; y++)
456 spriteBatch.DrawString(menuFont, currentChat.ElementAt(y).Sender + ": " + currentChat.ElementAt(y).Message
457 , new Vector2(400, 10 + y*15), Color.Blue, 0f, zero, .6f, SpriteEffects.None, 1f);
462 spriteBatch.DrawString(menuFont, chatMessage, new Vector2(400, 10 + 15*(currentChat.Count + 1)), Color.Green, 0f, zero, .6f, SpriteEffects.None, 1f);
465 private void DrawPlayerList(SpriteBatch spriteBatch)
469 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
472 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
473 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
474 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
475 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
476 spriteBatch.DrawString(menuFont, "enter chat mode", new Vector2(175, 80), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
477 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
478 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
479 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
480 spriteBatch.DrawString(menuFont, "T", new Vector2(145, 80), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
483 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
484 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
485 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
486 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
489 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
490 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
491 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
492 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
497 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
498 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
499 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
500 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
504 if (!(players.Count >= 1 && players[0].IsReady))
505 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
507 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
508 if (!(players.Count >= 2 && players[1].IsReady))
509 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
511 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
512 if (!(players.Count >= 3 && players[2].IsReady))
513 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
515 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
516 if (!(players.Count >= 4 && players[3].IsReady))
517 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
519 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
523 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
524 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
525 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
526 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
529 Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player
531 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
533 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
536 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
538 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
541 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
543 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
546 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
548 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
554 for (int g = 0; g < players.Count; g++)
556 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);