Exemple #1
0
 public void EndGame()
 {
     if (_connection != null)
     {
         _connection.Close();
         _connection = null;
     }
     ended = true;
     this.Exit();
 }
Exemple #2
0
        public void ConnectionExited(ConnectionError e)
        {
            Console.WriteLine(e.ToString());
            if (_connection != null)
            {
                _connection.Close();
                _connection = null;
            }

            switch (e)
            {
                case ConnectionError.LISTENING_SOCKET_IN_USE:
                    this._gameState = GameState.MENU;
                    break;
                case ConnectionError.REJECTED_BY_HOST:
                    this._gameState = GameState.MENU;
                    break;
                case ConnectionError.CONNECTION_EXITED:
                    this._gameState = GameState.MENU;
                    break;
                default:
                    this._gameState = GameState.MENU;
                    break;
            }

            GenerateBalls();
        }
Exemple #3
0
 public TCPServer(Networking parent, IPAddress listenAddress, int port)
 {
     this.parent = parent;
     Console.WriteLine("TCP Server started");
     this.tcpListener = new TcpListener(listenAddress, port);
     this.listenThread = new Thread(new ThreadStart(ListenForClient));
     this.listenThread.Start();
 }
Exemple #4
0
        protected void UpdateMenu(GameTime gameTime)
        {
            if (Mouse.GetState().LeftButton == ButtonState.Released && Mouse.GetState().LeftButton != _mouseStatePrevious.LeftButton)
            {
                switch (_menuState)
                {
                    case MenuState.HOME:
                        if (Mouse.GetState().Y >= 350 && Mouse.GetState().Y < 400)
                        {
                            _menuState = MenuState.MODE;
                        }
                        if (Mouse.GetState().Y >= 400 && Mouse.GetState().Y < 450)
                        {
                            _gameMode = GameMode.PRACTICE;
                            pottedBalls.Clear();
                            GenerateBalls();
                            _startTime = (long)gameTime.TotalGameTime.TotalSeconds;
                            _menuState = MenuState.HOME;
                            _gameState = GameState.GAMEPLAY;
                        }
                        break;
                    case MenuState.MODE:
                        if (Mouse.GetState().Y >= 350 && Mouse.GetState().Y < 400)
                        {
                            _gameMode = GameMode.OFFLINE;
                            GenerateBalls();
                            pottedBalls.Clear();
                            _menuState = MenuState.HOME;
                            _gameState = GameState.GAMEPLAY;
                        }
                        if (Mouse.GetState().Y >= 400 && Mouse.GetState().Y < 450)
                        {
                            _menuState = MenuState.AWAITCONNECTION;
                        }
                        if (Mouse.GetState().Y >= 450 && Mouse.GetState().Y < 500)
                        {
                            _menuState = MenuState.ENTERIP;
                        }
                        if (Mouse.GetState().Y >= 500 && Mouse.GetState().Y < 550)
                        {
                            _menuState = MenuState.HOME;
                        }
                        break;
                    case MenuState.AWAITCONNECTION:
                        if (Mouse.GetState().Y >= 450 && Mouse.GetState().Y < 500)
                        {
                            _menuState = MenuState.MODE;
                        }
                        break;
                    case MenuState.ENTERIP:
                        if (Mouse.GetState().Y >= 500 && Mouse.GetState().Y < 550)
                        {
                            if (IPAddress.TryParse(_ipInput, out _connectionAddress))
                                _menuState = MenuState.CONNECTING;
                        }
                        if (Mouse.GetState().Y >= 550 && Mouse.GetState().Y < 600)
                        {
                            _menuState = MenuState.MODE;
                        }
                        break;
                    default:
                        break;
                }
            }

            if (_menuState == MenuState.ENTERIP)
            {
                if (gameTime.TotalGameTime.TotalMilliseconds >= _lastInput + 150)
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.D0) || Keyboard.GetState().IsKeyDown(Keys.NumPad0))
                    {
                        _ipInput += "0";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D1) || Keyboard.GetState().IsKeyDown(Keys.NumPad1))
                    {
                        _ipInput += "1";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D2) || Keyboard.GetState().IsKeyDown(Keys.NumPad2))
                    {
                        _ipInput += "2";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D3) || Keyboard.GetState().IsKeyDown(Keys.NumPad3))
                    {
                        _ipInput += "3";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D4) || Keyboard.GetState().IsKeyDown(Keys.NumPad4))
                    {
                        _ipInput += "4";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D5) || Keyboard.GetState().IsKeyDown(Keys.NumPad5))
                    {
                        _ipInput += "5";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D6) || Keyboard.GetState().IsKeyDown(Keys.NumPad6))
                    {
                        _ipInput += "6";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D7) || Keyboard.GetState().IsKeyDown(Keys.NumPad7))
                    {
                        _ipInput += "7";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D8) || Keyboard.GetState().IsKeyDown(Keys.NumPad8))
                    {
                        _ipInput += "8";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D9) || Keyboard.GetState().IsKeyDown(Keys.NumPad9))
                    {
                        _ipInput += "9";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.OemPeriod))
                    {
                        _ipInput += ".";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.OemSemicolon))
                    {
                        _ipInput += ":";
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.Back) && _ipInput.Length > 0)
                    {
                        _ipInput = _ipInput.Substring(0, _ipInput.Length - 1);
                        _lastInput = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (!Keyboard.GetState().IsKeyDown(Keys.Enter) && _keyboardStatePrevious.IsKeyDown(Keys.Enter))
                    {
                        if (IPAddress.TryParse(_ipInput, out _connectionAddress))
                        {
                            _menuState = MenuState.CONNECTING;
                        }
                    }
                }
            }
            else if (_menuState == MenuState.CONNECTING)
            {
                if (_connection == null)
                {
                    ConnectionExitedDelegate ced = new ConnectionExitedDelegate(ConnectionExited);
                    _connection = new Networking(this, ced, _connectionAddress, 7167, false);
                }
                else if (_connection.Connected)
                {
                    _gameState = GameState.GAMEPLAY;
                    pottedBalls.Clear();
                    _gameMode = GameMode.ONLINE;
                    _myPlayer = PlayerIndex.Two;
                    GenerateBalls();
                    _menuState = MenuState.HOME;
                }
            }
            else if (_menuState == MenuState.AWAITCONNECTION)
            {
                if (_connection == null)
                {
                    ConnectionExitedDelegate ced = new ConnectionExitedDelegate(ConnectionExited);
                    _connection = new Networking(this, ced, IPAddress.Any, 7167, true);
                }
                else if (_connection.Connected)
                {
                    _gameState = GameState.GAMEPLAY;
                    _gameMode = GameMode.ONLINE;
                    _myPlayer = PlayerIndex.One;
                    GenerateBalls();
                    _menuState = MenuState.HOME;
                }
            }
        }
Exemple #5
0
        public TCPClient(Networking parent, ConnectionExitedDelegate ced, IPAddress serverAddress, int port)
        {
            this.ced = ced;
            Console.WriteLine("Creating TCP client");
            this.parent = parent;
            this.serverEndpoint = new IPEndPoint(serverAddress, 7167);
            tcpClient = new TcpClient();
            try
            {
                tcpClient.Connect(serverEndpoint);
            }
            catch (SocketException e)
            {
                e.ToString();
                ced(ConnectionError.REJECTED_BY_HOST);
                this.Close();
            }

            if (tcpClient != null)
            {
                if (tcpClient.Connected)
                {
                    // Send handshake
                    clientThread = new Thread(new ParameterizedThreadStart(parent.CommunicationLoop));
                    clientThread.Start(tcpClient);
                }
            }
            else
            {
                ced(ConnectionError.ERROR_UNKNOWN);
                this.Close();
            }
        }