]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs
New delegate method for getting and handling errors during the async calls. Need...
[chaz/carfire] / Project06 / CS 3505 Project 06 / CS 3505 Project 06 / LobbyGUI.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
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;
12
13 namespace CS_3505_Project_06
14 {
15 public class lobbyGUI : ILobby
16 {
17 float scale;
18
19 Texture2D background;
20 Texture2D spotLight;
21 Texture2D cs;
22 Texture2D selectGameScreen;
23
24 Vector2 backgroundPos;
25 Vector2 spotLightPos;
26 Vector2 spotLightCenter;
27 Vector2 csPos;
28
29 Vector2 zero;
30 Vector2 spotLightVelocity;
31
32 int MaxX;
33 int MinX;
34 int MaxY;
35 int MinY;
36
37 Texture2D checkedBox;
38 Texture2D deselectBox;
39 Texture2D emptySelectBox;
40 Texture2D menuItem;
41
42 Boolean ready;
43
44 SpriteFont menuFont;
45 string selected;
46 Vector2 createGamePos;
47 string createGameText;
48 Vector2 findGamePos;
49 string findGameText;
50
51 Vector2 returnToMainPos;
52 string returnToMainText;
53
54 KeyboardState previousKeyboardState;
55 KeyboardState currentKeyboardState;
56
57 GamerCollection<NetworkGamer> players;
58 LocalNetworkGamer localPlayer;
59 AvailableNetworkSessionCollection availableSessions;
60 bool allReady;
61
62 int selectedSessionIndex;
63
64 private enum lobbyState
65 {
66 Welcome,
67 CreateGame,
68 FindGame,
69 FindingGames, // TODO: New state.
70 JoiningGame, // TODO: New state.
71 Connected
72 }
73
74 lobbyState currentState;
75
76 public lobbyGUI()
77 {
78 currentState = lobbyState.Welcome;
79 selectedSessionIndex = 0;
80 ready = false;
81 }
82
83 public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics)
84 {
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);
94
95 zero = new Vector2(0, 0);
96
97 MaxX = graphics.GraphicsDevice.Viewport.Width;
98 MinX = 0;
99 MaxY = graphics.GraphicsDevice.Viewport.Height;
100 MinY = 100;
101
102 scale = MaxX / 1600f;
103
104 //playerlist stuff
105 checkedBox = contentManager.Load<Texture2D>("checkedBox");
106 deselectBox = contentManager.Load<Texture2D>("deselectBox");
107 emptySelectBox = contentManager.Load<Texture2D>("emptySelectBox");
108 menuItem = contentManager.Load<Texture2D>("menuItem");
109
110
111
112 //menu fonts
113 menuFont = contentManager.Load<SpriteFont>("menuFont");
114 createGamePos = new Vector2(100f, MaxY / 3);
115 createGameText = "Create Game";
116 selected = createGameText;
117
118 findGamePos = new Vector2(100f, (MaxY / 3) + 60);
119 findGameText = "Find Game";
120
121 returnToMainPos = new Vector2(MaxX / 2, MaxY - 120);
122 returnToMainText = "press [ X ] to return to main menu";
123
124
125 }
126
127 public void UnloadContent()
128 {
129
130 }
131
132 // TODO: New method.
133 void JoinedSession(NetworkSession session, NetworkGame networkGame)
134 {
135 if (session != null)
136 {
137 currentState = lobbyState.Connected;
138 }
139 else
140 {
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.");
144 }
145 }
146
147 // TODO: New method.
148 void FoundSessions(AvailableNetworkSessionCollection sessions, NetworkGame networkGame)
149 {
150 availableSessions = sessions;
151
152 if (availableSessions != null && availableSessions.Count > 0)
153 {
154 networkGame.JoinSession(availableSessions[0], JoinedSession);
155 currentState = lobbyState.JoiningGame;
156
157 availableSessions.Dispose();
158 availableSessions = null;
159 }
160 else
161 {
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!");
165 }
166 }
167
168 // TODO: New method.
169 void AsyncCallbackFailed(Exception exception, NetworkGame networkGame)
170 {
171 currentState = lobbyState.Welcome;
172 Console.WriteLine("Exception as thrown during async call: " + exception.Message);
173 }
174
175
176 public long Update(GameTime gameTime, NetworkGame networkGame)
177 {
178
179 UpdateSpotLight(gameTime);
180 currentKeyboardState = Keyboard.GetState();
181
182 if (networkGame.HasActiveSession)
183 {
184 players = networkGame.NetworkGamers;
185 }
186
187 //check inputs
188 switch (currentState)
189 {
190 case lobbyState.Welcome:
191 if (selected == createGameText)
192 {
193 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
194 currentState = lobbyState.CreateGame;
195 if (currentKeyboardState.IsKeyDown(Keys.Down))
196 selected = findGameText;
197 }
198 else
199 {
200 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
201 currentState = lobbyState.FindGame;
202 if (currentKeyboardState.IsKeyDown(Keys.Up))
203 selected = createGameText;
204 }
205 break;
206
207 case lobbyState.CreateGame:
208 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
209 {
210 currentState = lobbyState.Welcome;
211 ready = false;
212 if (networkGame.HasActiveSession)
213 {
214 players = null;
215 networkGame.LeaveSession();
216 }
217 }
218 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
219 {
220 currentState = lobbyState.JoiningGame;
221 networkGame.ErrorDelegate = AsyncCallbackFailed;
222 networkGame.CreateSession(JoinedSession);
223 }
224 break;
225
226 case lobbyState.FindGame:
227 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
228 {
229 currentState = lobbyState.Welcome;
230 ready = false;
231 }
232 networkGame.ErrorDelegate = AsyncCallbackFailed;
233 networkGame.FindSessions(FoundSessions);
234 currentState = lobbyState.FindingGames;
235 break;
236
237 case lobbyState.Connected:
238 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
239 {
240 ready = false;
241 if (networkGame.HasActiveSession)
242 {
243 players = null;
244 networkGame.LeaveSession();
245 }
246 currentState = lobbyState.Welcome;
247 }
248 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
249 networkGame.LocalGamer.IsReady = true;
250
251 if (networkGame.HasActiveSession)
252 {
253 localPlayer = networkGame.LocalGamer;
254 players = networkGame.NetworkGamers;
255 if (players != null)
256 {
257 allReady = true;
258 foreach (NetworkGamer p in players)
259 if (p.IsReady == false)
260 {
261 allReady = false;
262 break;
263 }
264 }
265
266 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
267 if(allReady && players.Count == 2 && localPlayer == players[0])
268 {
269 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
270 {
271 networkGame.ForceStartGame();
272 }
273 }
274 }
275 else
276 currentState = lobbyState.Welcome;
277
278 break;
279
280 }
281 previousKeyboardState = Keyboard.GetState();
282
283 return 1;
284 }
285
286 /// <summary>
287 /// Draws the lobby GUI. Has different states for difference menu configurations
288 /// </summary>
289 public long Draw(SpriteBatch spriteBatch)
290 {
291 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
292 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
293 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
294
295 switch (currentState)
296 {
297 case lobbyState.Welcome:
298 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
299 if (selected == createGameText)
300 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
301 else
302 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
303 if (selected == findGameText)
304 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
305 else
306 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
307 break;
308
309 case lobbyState.CreateGame:
310 DrawPlayerList(spriteBatch);
311 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
312 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
313 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);
314
315 break;
316
317 case lobbyState.FindingGames:
318 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
319 if(availableSessions == null)
320 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
321 else if (availableSessions.Count == 0)
322 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
323 else
324 {
325 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
326 {
327 Color color = Color.Gray;
328
329 if (sessionIndex == selectedSessionIndex)
330 color = Color.Red;
331
332 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
333 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots,
334 new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
335 }
336 }
337 break;
338
339 case lobbyState.Connected:
340 DrawPlayerList(spriteBatch);
341
342 if(allReady && players.Count == 2 && localPlayer == players[0])
343 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
344 else if(allReady)
345 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
346 else
347 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
348 break;
349 }
350
351 return 1;
352 }
353
354 private void UpdateSpotLight(GameTime gameTime)
355 {
356 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
357 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
358
359 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
360 {
361 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
362 }
363 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
364 {
365 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
366 }
367 }
368
369
370 private void DrawPlayerList(SpriteBatch spriteBatch)
371 {
372
373 //reference point
374 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
375
376 //command info
377 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
378 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
379 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
380 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
381 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
382 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
383 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
384
385 //Background squares
386 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
387 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
388 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
389 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
390
391 //Ready Labels
392 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
393 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
394 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
395 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
396
397 //Ready CheckBoxs
398 if (players == null)
399 {
400 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
401 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
402 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
403 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
404 }
405 else
406 {
407 if (!(players.Count >= 1 && players[0].IsReady))
408 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
409 else
410 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
411 if (!(players.Count >= 2 && players[1].IsReady))
412 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
413 else
414 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
415 if (!(players.Count >= 3 && players[2].IsReady))
416 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
417 else
418 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
419 if (!(players.Count >= 4 && players[3].IsReady))
420 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
421 else
422 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
423 }
424
425 //Chat Labels
426 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
427 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
428 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
429 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
430
431 //Chat CheckBoxs
432 Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player
433 if (!chatwith)
434 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
435 else
436 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
437
438 if (!chatwith)
439 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
440 else
441 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
442
443 if (!chatwith)
444 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
445 else
446 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
447
448 if (!chatwith)
449 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
450 else
451 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
452
453
454 //draw player names
455 if (players != null)
456 {
457 for (int g = 0; g < players.Count; g++)
458 {
459 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);
460 }
461 }
462 }
463 }
464 }
This page took 0.057828 seconds and 4 git commands to generate.