]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs
Gui Finding
[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 availableSessions = null;
233 networkGame.ErrorDelegate = AsyncCallbackFailed;
234 networkGame.FindSessions(FoundSessions);
235 currentState = lobbyState.FindingGames;
236 break;
237
238 case lobbyState.FindingGames:
239 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
240 {
241 currentState = lobbyState.Welcome;
242 ready = false;
243 }
244 if (availableSessions != null && availableSessions.Count == 0)
245 currentState = lobbyState.FindGame;
246 else if (availableSessions != null && availableSessions.Count > 0)
247 {
248 if (currentKeyboardState.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
249 {
250 networkGame.JoinSession(availableSessions[0], JoinedSession);
251 currentState = lobbyState.JoiningGame;
252
253 availableSessions.Dispose();
254 availableSessions = null;
255 }
256 if (currentKeyboardState.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
257 {
258 networkGame.JoinSession(availableSessions[0], JoinedSession);
259 currentState = lobbyState.JoiningGame;
260
261 availableSessions.Dispose();
262 availableSessions = null;
263 }
264 }
265
266
267 Console.WriteLine("Finding");
268 break;
269 case lobbyState.Connected:
270 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
271 {
272 ready = false;
273 if (networkGame.HasActiveSession)
274 {
275 players = null;
276 networkGame.LeaveSession();
277 }
278 currentState = lobbyState.Welcome;
279
280 }
281 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
282 networkGame.LocalGamer.IsReady = true;
283
284 if (networkGame.HasActiveSession)
285 {
286 localPlayer = networkGame.LocalGamer;
287 players = networkGame.NetworkGamers;
288 if (players != null)
289 {
290 allReady = true;
291 foreach (NetworkGamer p in players)
292 if (p.IsReady == false)
293 {
294 allReady = false;
295 break;
296 }
297 }
298
299 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
300 if(allReady && players.Count == 2 && localPlayer == players[0])
301 {
302 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
303 {
304 networkGame.ForceStartGame();
305 }
306 }
307 }
308 else
309 currentState = lobbyState.Welcome;
310
311 break;
312
313 }
314 previousKeyboardState = Keyboard.GetState();
315
316 return 1;
317 }
318
319 /// <summary>
320 /// Draws the lobby GUI. Has different states for difference menu configurations
321 /// </summary>
322 public long Draw(SpriteBatch spriteBatch)
323 {
324 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
325 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
326 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
327
328 switch (currentState)
329 {
330 case lobbyState.Welcome:
331 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
332 if (selected == createGameText)
333 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
334 else
335 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
336 if (selected == findGameText)
337 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
338 else
339 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
340 break;
341
342 case lobbyState.CreateGame:
343 DrawPlayerList(spriteBatch);
344 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
345 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
346 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);
347
348 break;
349
350 case lobbyState.FindingGames:
351 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
352 if(availableSessions == null)
353 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
354 else if (availableSessions.Count == 0)
355 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
356 else
357 {
358 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
359 {
360 Color color = Color.Gray;
361
362 if (sessionIndex == selectedSessionIndex)
363 color = Color.Red;
364
365 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
366 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots,
367 new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
368 }
369 }
370 break;
371
372 case lobbyState.Connected:
373 DrawPlayerList(spriteBatch);
374
375 if(allReady && players.Count == 2 && localPlayer == players[0])
376 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
377 else if(allReady)
378 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
379 else
380 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
381 break;
382 }
383
384 return 1;
385 }
386
387 private void UpdateSpotLight(GameTime gameTime)
388 {
389 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
390 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
391
392 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
393 {
394 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
395 }
396 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
397 {
398 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
399 }
400 }
401
402
403 private void DrawPlayerList(SpriteBatch spriteBatch)
404 {
405
406 //reference point
407 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
408
409 //command info
410 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
411 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
412 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
413 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
414 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
415 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
416 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
417
418 //Background squares
419 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
420 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
421 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
422 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
423
424 //Ready Labels
425 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
426 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
427 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
428 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
429
430 //Ready CheckBoxs
431 if (players == null)
432 {
433 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
434 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
435 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
436 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
437 }
438 else
439 {
440 if (!(players.Count >= 1 && players[0].IsReady))
441 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
442 else
443 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
444 if (!(players.Count >= 2 && players[1].IsReady))
445 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
446 else
447 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
448 if (!(players.Count >= 3 && players[2].IsReady))
449 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
450 else
451 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
452 if (!(players.Count >= 4 && players[3].IsReady))
453 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
454 else
455 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
456 }
457
458 //Chat Labels
459 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
460 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
461 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
462 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
463
464 //Chat CheckBoxs
465 Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player
466 if (!chatwith)
467 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
468 else
469 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
470
471 if (!chatwith)
472 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
473 else
474 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
475
476 if (!chatwith)
477 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
478 else
479 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
480
481 if (!chatwith)
482 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
483 else
484 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
485
486
487 //draw player names
488 if (players != null)
489 {
490 for (int g = 0; g < players.Count; g++)
491 {
492 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);
493 }
494 }
495 }
496 }
497 }
This page took 0.057736 seconds and 4 git commands to generate.