]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs
Lobby is almost completely functional. Currently it tests working allowing
[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 Connected
70 }
71
72 lobbyState currentState;
73
74 public lobbyGUI()
75 {
76 currentState = lobbyState.Welcome;
77 selectedSessionIndex = 0;
78 ready = false;
79 }
80
81 public void LoadContent(ContentManager contentManager, GraphicsDeviceManager graphics)
82 {
83 background = contentManager.Load<Texture2D>("background");
84 spotLight = contentManager.Load<Texture2D>("spotlight");
85 cs = contentManager.Load<Texture2D>("cs");
86 selectGameScreen = contentManager.Load<Texture2D>("selectGameScreen");
87 backgroundPos = new Vector2(0f, 0f);
88 spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98);
89 spotLightCenter = new Vector2(800f, 800f);
90 spotLightVelocity = new Vector2(-100, 33);
91 csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98);
92
93 zero = new Vector2(0, 0);
94
95 MaxX = graphics.GraphicsDevice.Viewport.Width;
96 MinX = 0;
97 MaxY = graphics.GraphicsDevice.Viewport.Height;
98 MinY = 100;
99
100 scale = MaxX / 1600f;
101
102 //playerlist stuff
103 checkedBox = contentManager.Load<Texture2D>("checkedBox");
104 deselectBox = contentManager.Load<Texture2D>("deselectBox");
105 emptySelectBox = contentManager.Load<Texture2D>("emptySelectBox");
106 menuItem = contentManager.Load<Texture2D>("menuItem");
107
108
109
110 //menu fonts
111 menuFont = contentManager.Load<SpriteFont>("menuFont");
112 createGamePos = new Vector2(100f, MaxY / 3);
113 createGameText = "Create Game";
114 selected = createGameText;
115
116 findGamePos = new Vector2(100f, (MaxY / 3) + 60);
117 findGameText = "Find Game";
118
119 returnToMainPos = new Vector2(MaxX / 2, MaxY - 120);
120 returnToMainText = "press [ X ] to return to main menu";
121
122
123 }
124
125 public void UnloadContent()
126 {
127
128 }
129
130 public long Update(GameTime gameTime, NetworkGame networkGame)
131 {
132
133 UpdateSpotLight(gameTime);
134 currentKeyboardState = Keyboard.GetState();
135
136 if (networkGame.sessionExists())
137 {
138 players = networkGame.NetworkGamers;
139 }
140
141 //check inputs
142 switch (currentState)
143 {
144 case lobbyState.Welcome:
145 if (selected == createGameText)
146 {
147 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
148 currentState = lobbyState.CreateGame;
149 if (currentKeyboardState.IsKeyDown(Keys.Down))
150 selected = findGameText;
151 }
152 else
153 {
154 if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
155 currentState = lobbyState.FindGame;
156 if (currentKeyboardState.IsKeyDown(Keys.Up))
157 selected = createGameText;
158 }
159 break;
160
161 case lobbyState.CreateGame:
162 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
163 {
164 currentState = lobbyState.Welcome;
165 ready = false;
166 if (networkGame.sessionExists())
167 {
168 players = null;
169 networkGame.LeaveSession();
170 }
171 }
172 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))
173 {
174 currentState = lobbyState.Connected;
175 networkGame.CreateSession(4);
176 }
177 break;
178
179 case lobbyState.FindGame:
180 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
181 {
182 currentState = lobbyState.Welcome;
183 ready = false;
184 }
185 availableSessions = networkGame.FindSessions();
186 if (availableSessions != null)
187 {
188 networkGame.JoinSession(availableSessions[0]);
189 currentState = lobbyState.Connected;
190
191 availableSessions.Dispose();
192 availableSessions = null;
193 }
194 break;
195
196 case lobbyState.Connected:
197 if (currentKeyboardState.IsKeyDown(Keys.X) && previousKeyboardState.IsKeyUp(Keys.X))
198 {
199 ready = false;
200 if (networkGame.sessionExists())
201 {
202 players = null;
203 networkGame.LeaveSession();
204 }
205 currentState = lobbyState.Welcome;
206 }
207 if (currentKeyboardState.IsKeyDown(Keys.R) && previousKeyboardState.IsKeyUp(Keys.R))
208 networkGame.LocalGamer.IsReady = true;
209
210 if (networkGame.sessionExists())
211 {
212 localPlayer = networkGame.LocalGamer;
213 players = networkGame.NetworkGamers;
214 if (players != null)
215 {
216 allReady = true;
217 foreach (NetworkGamer p in players)
218 if (p.IsReady == false)
219 {
220 allReady = false;
221 break;
222 }
223 }
224
225 //allows host to start the game when all players are ready, change count below to different number for testing with less then 4 players
226 if(allReady && players.Count == 2 && localPlayer == players[0])
227 {
228 if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
229 {
230 networkGame.StartGame();
231 }
232 }
233 }
234 else
235 currentState = lobbyState.Welcome;
236
237 break;
238
239 }
240 previousKeyboardState = Keyboard.GetState();
241
242 return 1;
243 }
244
245 /// <summary>
246 /// Draws the lobby GUI. Has different states for difference menu configurations
247 /// </summary>
248 public long Draw(SpriteBatch spriteBatch)
249 {
250 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
251 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
252 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
253
254 switch (currentState)
255 {
256 case lobbyState.Welcome:
257 spriteBatch.DrawString(menuFont, "press [ Home ] to login", new Vector2(350, 20), Color.LightGray, 0f, zero, .6f, SpriteEffects.None, 0.5f);
258 if (selected == createGameText)
259 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
260 else
261 spriteBatch.DrawString(menuFont, createGameText, createGamePos, Color.Gray, 0, zero, 1f,SpriteEffects.None, 0.5f);
262 if (selected == findGameText)
263 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Red, 0, zero, 1f, SpriteEffects.None, 0.5f);
264 else
265 spriteBatch.DrawString(menuFont, findGameText, findGamePos, Color.Gray, 0, zero, 1f, SpriteEffects.None, 0.5f);
266 break;
267
268 case lobbyState.CreateGame:
269 DrawPlayerList(spriteBatch);
270 spriteBatch.DrawString(menuFont, "You are now the Host!", new Vector2(MaxX / 2, MaxY / 3), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
271 spriteBatch.DrawString(menuFont, "press: Y to continue", new Vector2(MaxX /2, (MaxY / 3) + menuFont.LineSpacing), Color.White, 0f, zero, .6f, SpriteEffects.None, 0.5f);
272 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);
273
274 break;
275
276 case lobbyState.FindGame:
277 spriteBatch.Draw(selectGameScreen, backgroundPos, null, Color.White, 0, zero, scale, SpriteEffects.None, 0);
278 if(availableSessions == null)
279 spriteBatch.DrawString(menuFont, "searching for available games ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
280 else if (availableSessions.Count == 0)
281 spriteBatch.DrawString(menuFont, "no games currently available, searching ....", new Vector2(150, 100), Color.Gray, 0f, zero, .7f, SpriteEffects.None, 0.5f);
282 else
283 {
284 for (int sessionIndex = 0; sessionIndex < availableSessions.Count; sessionIndex++)
285 {
286 Color color = Color.Gray;
287
288 if (sessionIndex == selectedSessionIndex)
289 color = Color.Red;
290
291 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].HostGamertag, new Vector2(150, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
292 spriteBatch.DrawString(menuFont, availableSessions[sessionIndex].CurrentGamerCount + " / " + availableSessions[sessionIndex].OpenPublicGamerSlots + availableSessions[sessionIndex].OpenPrivateGamerSlots,
293 new Vector2(400, 100 + sessionIndex * menuFont.LineSpacing), color, 0f, zero, .7f, SpriteEffects.None, 0.5f);
294 }
295 }
296 break;
297
298 case lobbyState.Connected:
299 DrawPlayerList(spriteBatch);
300
301 if(allReady && players.Count == 2 && localPlayer == players[0])
302 spriteBatch.DrawString(menuFont, "Press B to begin game!", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
303 else if(allReady)
304 spriteBatch.DrawString(menuFont, "The game will begin soon.", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
305 else
306 spriteBatch.DrawString(menuFont, "Waiting for ready players...", new Vector2(MaxX / 2, MaxY / 2), Color.White, 0f, zero, .7f, SpriteEffects.None, 0.5f);
307 break;
308 }
309
310 return 1;
311 }
312
313 private void UpdateSpotLight(GameTime gameTime)
314 {
315 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
316 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
317
318 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
319 {
320 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
321 }
322 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
323 {
324 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);
325 }
326 }
327
328
329 private void DrawPlayerList(SpriteBatch spriteBatch)
330 {
331
332 //reference point
333 Vector2 topOfList = new Vector2(MaxX / 8, MaxY / 4);
334
335 //command info
336 spriteBatch.DrawString(menuFont, "Current Players", new Vector2(topOfList.X + 15, topOfList.Y - 25), Color.White);
337 spriteBatch.DrawString(menuFont, "Command Options: to mark your self as ready", new Vector2(20, 20), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
338 spriteBatch.DrawString(menuFont, "type players # to toggle chat", new Vector2(175, 40), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
339 spriteBatch.DrawString(menuFont, "return to main menu", new Vector2(175, 60), Color.Gray, 0f, zero, .6f, SpriteEffects.None, 1f);
340 spriteBatch.DrawString(menuFont, "R", new Vector2(145, 20), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
341 spriteBatch.DrawString(menuFont, "#", new Vector2(145, 40), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
342 spriteBatch.DrawString(menuFont, "X", new Vector2(145, 60), Color.DarkGreen, 0f, zero, .6f, SpriteEffects.None, 1f);
343
344 //Background squares
345 spriteBatch.Draw(menuItem, topOfList, null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
346 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 65), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
347 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 130), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
348 spriteBatch.Draw(menuItem, new Vector2(topOfList.X, topOfList.Y + 195), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
349
350 //Ready Labels
351 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
352 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
353 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
354 spriteBatch.DrawString(menuFont, "Ready", new Vector2(topOfList.X + 5, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
355
356 //Ready CheckBoxs
357 if (players == null)
358 {
359 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
360 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
361 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
362 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
363 }
364 else
365 {
366 if (!(players.Count >= 1 && players[0].IsReady))
367 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
368 else
369 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
370 if (!(players.Count >= 2 && players[1].IsReady))
371 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
372 else
373 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
374 if (!(players.Count >= 3 && players[2].IsReady))
375 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
376 else
377 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
378 if (!(players.Count >= 4 && players[3].IsReady))
379 spriteBatch.Draw(emptySelectBox, new Vector2(topOfList.X - 32, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
380 else
381 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X - 32, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
382 }
383
384 //Chat Labels
385 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 45), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
386 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 110), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
387 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 175), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
388 spriteBatch.DrawString(menuFont, "Chat with", new Vector2(topOfList.X + 152, topOfList.Y + 240), Color.DarkGray, 0f, zero, .6f, SpriteEffects.None, 1f);
389
390 //Chat CheckBoxs
391 Boolean chatwith = false; // change to reflect info from network, move to update and create one for each player
392 if (!chatwith)
393 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X +218, topOfList.Y + 35), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
394 else
395 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 30), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
396
397 if (!chatwith)
398 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 100), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
399 else
400 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 95), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
401
402 if (!chatwith)
403 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 165), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
404 else
405 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 160), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
406
407 if (!chatwith)
408 spriteBatch.Draw(deselectBox, new Vector2(topOfList.X + 218, topOfList.Y + 230), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
409 else
410 spriteBatch.Draw(checkedBox, new Vector2(topOfList.X + 218, topOfList.Y + 225), null, Color.White, 0, zero, scale, SpriteEffects.None, 1f);
411
412
413 //draw player names
414 if (players != null)
415 {
416 for (int g = 0; g < players.Count; g++)
417 {
418 spriteBatch.DrawString(menuFont, players[g].Gamertag, new Vector2(topOfList.X + 10, topOfList.Y + 10 + 65*g), Color.White, 0f, zero, .75f, SpriteEffects.None, 1f);
419 }
420 }
421 }
422 }
423 }
This page took 0.059409 seconds and 4 git commands to generate.