private static void ParseReadyCommand(netPongUDPParsePacketThreadParam threadParam) { byte[] packet = threadParam.packetData; byte nPlayerID = packet[3]; Int32 nSessionID = BitConverter.ToInt32(packet, 4); lock (m_runningSessionsLock) { for (int i = 0; i < m_runningSessions.Count; i++) { if (m_runningSessions[i].nSessionID == nSessionID) { netPongUdpGameSession session = m_runningSessions[i]; if (nPlayerID == 1) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - netPong UDP Game Server"); Console.WriteLine("| * Player 1 confirmed UDP Connection "); Console.WriteLine("|========================"); session.epPlayer1 = threadParam.senderRemoteEP; m_runningSessions[i] = session; } else if (nPlayerID == 2) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - netPong UDP Game Server"); Console.WriteLine("| * Player 2 confirmed UDP Connection "); Console.WriteLine("|========================"); session.epPlayer2 = threadParam.senderRemoteEP; m_runningSessions[i] = session; } m_runningSessions[i] = session; if ((session.epPlayer1 != null) && (session.epPlayer2 != null)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - netPong UDP Game Server"); Console.WriteLine("| * Starting BallCalculationThread for Session ID " + session.nSessionID); Console.WriteLine("|========================"); session.bIsInGame = true; session.hBallCalcThread = new Thread(new ParameterizedThreadStart(BallCalculationThread)); session.hBallCalcThread.IsBackground = true; m_runningSessions[i] = session; session.hBallCalcThread.Start(i); } break; } } } }
public static void AddGameSession(netPongInvitation invitation, out int nSessionID) { netPongUdpGameSession gameSession = new netPongUdpGameSession(m_nNextSessionID, invitation.infoPlayer1.strClientName, invitation.infoPlayer2.strClientName, null, null); nSessionID = m_nNextSessionID; m_nNextSessionID++; lock (m_runningSessionsLock) { m_runningSessions.Add(gameSession); } }
private static void CheckWallCollisions(ref netPongUdpGameSession session) { Vector3 newBallPos = session.ballPos; Vector3 newBallDirSpeed = session.ballDirSpeed; if (newBallPos.X > 30.0f) { newBallPos.X = 30.0f; newBallDirSpeed.X *= -1; } if (newBallPos.X < -30.0f) { newBallPos.X = -30.0f; newBallDirSpeed.X *= -1; } session.ballPos = newBallPos; session.ballDirSpeed = newBallDirSpeed; }
private static void CheckPaddleCollisions(ref netPongUdpGameSession session) { Vector3 newBallPos = session.ballPos; Vector3 newBallDirSpeed = session.ballDirSpeed; //player1 must react if (newBallPos.Z > 24.0f) { float xStartPanel = session.fPlayer1PaddleXPos + 6.5f; float xEndPanel = session.fPlayer1PaddleXPos - 6.5f; //player1 caught ball if ((xEndPanel <= newBallPos.X) && (newBallPos.X <= xStartPanel)) { //adjust x-direction float xLengthBallHitPos = (newBallPos.X - xStartPanel) + 6.5f; // = percentage where ball hit paddle * ~70 degree newBallDirSpeed.X = (xLengthBallHitPos / 6.5f) * (float)(Math.PI / 5.5); //adjust z-direction and set ball out ouf collision zone newBallDirSpeed.Z *= -1.0f; newBallPos.Z = 24.0f; } //player1 missed ball else { //session.fPlayer1PaddleXPos = 0.0f; newBallPos.X = 0.0f; newBallDirSpeed.X = 0.0f; newBallPos.Z = -24.0f; } } //player2 must react else if (newBallPos.Z < -24.0f) { float xStartPanel = session.fPlayer2PaddleXPos + 6.5f; float xEndPanel = session.fPlayer2PaddleXPos - 6.5f; //player2 caught ball if ((xEndPanel <= newBallPos.X) && (newBallPos.X <= xStartPanel)) { //adjust x-direction float xLengthBallHitPos = (newBallPos.X - xStartPanel) + 6.5f; // = percentage where ball hit paddle * ~70 degree newBallDirSpeed.X = (xLengthBallHitPos / 6.5f) * (float)(Math.PI / 5.5); //adjust z-direction and set ball out ouf collision zone newBallDirSpeed.Z *= -1.0f; newBallPos.Z = -24.0f; } //player2 missed ball else { //session.fPlayer2PaddleXPos = 0.0f; newBallPos.X = 0.0f; newBallDirSpeed.X = 0.0f; newBallPos.Z = 24.0f; } } session.ballPos = newBallPos; session.ballDirSpeed = newBallDirSpeed; }