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;
64 private enum lobbyState
69 FindingGames, // TODO: New state.
70 JoiningGame, // TODO: New state.
74 lobbyState currentState;
78 currentState = lobbyState.Welcome;
79 selectedSessionIndex = 0;
83 public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics)
85 background = contentManager.Load<Texture2D>("background");
86 spotLight = contentManager.Load<Texture2D>("spotlight");
87 cs = contentManager.Load<Texture2D>("cs");
88 selectGameScreen = contentManager.Load<Texture2D>("selectGameScreen");
89 backgroundPos = new Vector2(0f, 0f);
90 spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98);
91 spotLightCenter = new Vector2(800f, 800f);
92 spotLightVelocity = new Vector2(-100, 33);
93 csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98);
95 zero = new Vector2(0, 0);
97 MaxX = graphics.GraphicsDevice.Viewport.Width;
99 MaxY = graphics.GraphicsDevice.Viewport.Height;
102 scale = MaxX / 1600f;
105 checkedBox = contentManager.Load<Texture2D>("checkedBox");
106 deselectBox = contentManager.Load<Texture2D>("deselectBox");
107 emptySelectBox = contentManager.Load<Texture2D>("emptySelectBox");
108 menuItem = contentManager.Load<Texture2D>("menuItem");
113 menuFont = contentManager.Load<SpriteFont>("menuFont");
114 createGamePos = new Vector2(100f, MaxY / 3);
115 createGameText = "Create Game";
116 selected = createGameText;
118 findGamePos = new Vector2(100f, (MaxY / 3) + 60);
119 findGameText = "Find Game";
121 returnToMainPos = new Vector2(MaxX / 2, MaxY - 120);
122 returnToMainText = "press [ X ] to return to main menu";
127 public void UnloadContent()
133 void JoinedSession(NetworkSession session, NetworkGame networkGame)
137 currentState = lobbyState.Connected;
141 // TODO: This should do something more than just throw the player back to the welcome screen.
142 currentState = lobbyState.Welcome;
143 Console.WriteLine("Couldn't create/join the session.");
148 void FoundSessions(AvailableNetworkSessionCollection sessions, NetworkGame networkGame)
150 availableSessions = sessions;
152 if (availableSessions != null && availableSessions.Count > 0)
154 networkGame.JoinSession(availableSessions[0], JoinedSession);
155 currentState = lobbyState.JoiningGame;
157 availableSessions.Dispose();
158 availableSessions = null;
162 // TODO: This should do something more than just throw the player back to the welcome screen.
163 currentState = lobbyState.Welcome;
164 Console.WriteLine("No sessions to join!");
168 public long Update(GameTime gameTime, NetworkGame networkGame)
171 UpdateSpotLight(gameTime);
172 currentKeyboardState = Keyboard.GetState();
174 if (networkGame.HasActiveSession)
176 players = networkGame.NetworkGamers;
180 switch (currentState)
182 case lobbyState.Welcome:
183 if (selected == createGameText)
185 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
186 currentState = lobbyState.CreateGame;
187 if (currentKeyboardState.IsKeyDown(Keys.Down))
188 selected = findGameText;
192 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
193 currentState = lobbyState.FindGame;
194 if (currentKeyboardState.IsKeyDown(Keys.Up))
195 selected = createGameText;
199 case lobbyState.CreateGame:
200 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
202 currentState = lobbyState.Welcome;
204 if (networkGame.HasActiveSession)
207 networkGame.LeaveSession();
210 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
212 currentState = lobbyState.JoiningGame;
213 networkGame.CreateSession(JoinedSession);
217 case lobbyState.FindGame:
218 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
220 currentState = lobbyState.Welcome;
223 networkGame.FindSessions(FoundSessions);
224 currentState = lobbyState.FindingGames;
227 case lobbyState.Connected:
228 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
231 if (networkGame.HasActiveSession)
234 networkGame.LeaveSession();
236 currentState = lobbyState.Welcome;
238 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
239 networkGame.LocalGamer.IsReady = true;
241 if (networkGame.HasActiveSession)
243 localPlayer = networkGame.LocalGamer;
244 players = networkGame.NetworkGamers;
248 foreach (NetworkGamer p in players)
249 if (p.IsReady == false)
256 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
257 if(allReady && players.Count == 2 && localPlayer == players[0])
259 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
261 networkGame.StartGame();
266 currentState = lobbyState.Welcome;
271 previousKeyboardState = Keyboard.GetState();
277 /// Draws the lobby GUI. Has different states for difference menu configurations
279 public long Draw(SpriteBatch spriteBatch)
281 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
282 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
283 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
285 switch (currentState)
287 case lobbyState.Welcome:
288 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
289 if (selected == createGameText)
290 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
292 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
293 if (selected == findGameText)
294 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
296 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
299 case lobbyState.CreateGame:
300 DrawPlayerList(spriteBatch);
301 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
302 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
303 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);
307 case lobbyState.FindingGames:
308 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
309 if(availableSessions == null)
310 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
311 else if (availableSessions.Count == 0)
312 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
315 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
317 Color color = Color.Gray;
319 if (sessionIndex == selectedSessionIndex)
322 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
323 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots,
324 new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
329 case lobbyState.Connected:
330 DrawPlayerList(spriteBatch);
332 if(allReady && players.Count == 2 && localPlayer == players[0])
333 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
335 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
337 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
344 private void UpdateSpotLight(GameTime gameTime)
346 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
347 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
349 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
351 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
353 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
355 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
360 private void DrawPlayerList(SpriteBatch spriteBatch)
364 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
367 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
368 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
369 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
370 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
371 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
372 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
373 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
376 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
377 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
378 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
379 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
382 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
383 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
384 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
385 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
390 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
391 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
392 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
393 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
397 if (!(players.Count >= 1 && players[0].IsReady))
398 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
400 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
401 if (!(players.Count >= 2 && players[1].IsReady))
402 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
404 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
405 if (!(players.Count >= 3 && players[2].IsReady))
406 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
408 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
409 if (!(players.Count >= 4 && players[3].IsReady))
410 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
412 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
416 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
417 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
418 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
419 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
422 Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player
424 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
426 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
429 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
431 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
434 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
436 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
439 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
441 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
447 for (int g = 0; g < players.Count; g++)
449 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);