/// <summary> /// Called when a connection error occurs during the game. Disconnects all clients from the server. /// </summary> public static void Error() { State_Menu.Singleton.targetState = State_Menu.Singleton; Console.WriteLine("ERROR, Targetstate is Menu"); Pong.targetState = State_Menu.Singleton; State_Menu.Singleton.info = "A client has logged out or a connection error \nocurred, you have been disconnected."; PongConnection.CloseConnection(); }
public Pong() { server = new PongConnection(11000); server.Start(); Singleton = this; graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; state_menu = new State_Menu(); state_playing = new State_Playing(); targetState = state_menu; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { prevKeyState = currKeyState; currKeyState = Keyboard.GetState(); if (PongConnection.PlayerID != -1 && PongConnection.running) { if (currKeyState != prevKeyState) { Keys[] pressedKeys = currKeyState.GetPressedKeys(); if (pressedKeys.Length > 0) { PongConnection.SyncPlayerPositions(pressedKeys[0].ToString()); } } } if (currKeyState.IsKeyDown(Keys.Escape)) { if (currKeyState != prevKeyState) { if (!PongConnection.running) { Exit(); } else { PongConnection.CloseConnection(); } } } currentState = targetState; currentState.Update(gameTime); base.Update(gameTime); }
/// <summary> /// Updates the Menu State. /// </summary> /// <param name="gameTime"></param> public override void Update(GameTime gameTime) { if (Pong.currKeyState != Pong.prevKeyState) { Keys[] keys = Pong.currKeyState.GetPressedKeys(); if (keys.Length > 0) { if (keys[0].Equals(Keys.Back) && input.Length > 0) { input = input.Substring(0, input.Length - 1); } else if (keys[0].ToString().Length <= 2) { input += keys[0].ToString(); } } if (Pong.currKeyState.IsKeyDown(Keys.Down)) { selectedText++; } if (Pong.currKeyState.IsKeyDown(Keys.Up)) { selectedText--; } switch (selectedText) { case 0: serverText = Color.Red; clientText = Color.White; break; case 1: serverText = Color.White; clientText = Color.Red; break; } if (Pong.currKeyState.IsKeyDown(Keys.Enter)) { bool success; switch (selectedText) { case 0: success = PongConnection.StartListening(true, input); break; case 1: success = PongConnection.StartListening(false, input); break; default: success = false; break; } if (success) { info = ""; if (selectedText == 0) { targetState = State_Playing.Singleton; } } } } base.Update(gameTime); }
/// <summary> /// Analyzes the incoming data. /// </summary> /// <param name="data"></param> void GetInformation(string data) { if (data.Length < 1) { return; } Console.WriteLine("DATA INCOME: " + data); int.TryParse(data.First().ToString(), out PlayerID); if (PlayerID == 0) { Type = RequestType.RegisterPlayer; ResponseExpected = true; if (!PongConnection.CheckPassword(data.Replace("?", ""))) { Console.WriteLine("Inputted password is wrong! " + data); dataHandler.connection.correctPassword = false; } else { dataHandler.connection.AddToArray(); dataHandler.connection.correctPassword = true; PlayerID = PongConnection.RegisteredClientIDs.Length + 1; double[] temp = PongConnection.RegisteredClientIDs; double[] newArray = new double[temp.Length + 1]; for (int x = 0; x < newArray.Length; x++) { if (x < newArray.Length - 1) { newArray[x] = temp[x]; } else { newArray[x] = newArray.Length; } } PongConnection.RegisteredClientIDs = newArray; } dataHandler.connection.initalized = true; return; } char Command = data.ToUpper().Last(); switch (Command) { case 'W': Type = RequestType.MoveUp; ResponseExpected = true; break; case 'S': Type = RequestType.MoveDown; ResponseExpected = true; break; case 'P': Type = RequestType.Pause; break; case 'E': Type = RequestType.Exit; break; default: Type = RequestType.Undefined; ResponseExpected = true; Console.WriteLine("UNDEFINED"); break; } }