Beispiel #1
0
        /// <summary>
        /// Joins new player to this game
        /// </summary>
        /// <param name="player"></param>
        public void JoinPlayer(PongPlayer player)
        {
            lock (_syncRoot)
            {
                if (_players[LeftPlayer] != null && _players[RightPlayer] != null)
                {
                    throw new InvalidOperationException();
                }

                player.SetGame(this);
                player.PlayerDisconnected += OnPlayerDisconnected;
                player.PlayerMoved        += OnPlayerMoved;

                if (_players[LeftPlayer] == null)
                {
                    _players[LeftPlayer] = player;
                    State = GameState.WaitingForPlayer;
                }
                else
                {
                    _players[RightPlayer] = player;
                    // we have two players, so start the game
                    State = GameState.InProgress;
                    StartBall();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Joins new player to this game
        /// </summary>
        /// <param name="player"></param>
        public void JoinPlayer(PongPlayer player)
        {
            lock (_syncRoot)
            {
                if (_players[LeftPlayer] != null && _players[RightPlayer] != null)
                    throw new InvalidOperationException();

                player.SetGame(this);
                player.PlayerDisconnected += OnPlayerDisconnected;
                player.PlayerMoved += OnPlayerMoved;

                if (_players[LeftPlayer] == null)
                {
                    _players[LeftPlayer] = player;
                    State = GameState.WaitingForPlayer;
                }
                else
                {
                    _players[RightPlayer] = player;
                    // we have two players, so start the game
                    State = GameState.InProgress; 
                    StartBall();
                }
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.IsWebSocketRequest)
            {
                // create a player
                var player = new PongPlayer();
                PongApp.JoinPlayer(player);

                // start receiving from socket
                context.AcceptWebSocketRequest(player.Receiver);
            }
        }
Beispiel #4
0
        private void OnPlayerMoved(PongPlayer player, PlayerPositionMessage position)
        {
            var otherPlayer = OtherPlayer(player);

            if (otherPlayer != null)
            {
                // send new player position to the other player
                otherPlayer.SendMessage(new PlayerPositionMessage {
                    YPos = player.YPos
                });
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.IsWebSocketRequest)
            {
                // create a player
                var player = new PongPlayer();
                PongApp.JoinPlayer(player);

                // start receiving from socket
                context.AcceptWebSocketRequest(player.Receiver);
            }
        }
Beispiel #6
0
 /// <summary>
 /// Добавить игрока в существующую игру или создать новую игру 
 /// </summary>
 /// <param name="player">
 /// Игро
 /// </param>
 public static void JoinPlayer(PongPlayer player)
 {
     lock (_syncRoot)
     {
         var game = _games.Where(g => g.State == GameState.WaitingForPlayer).FirstOrDefault();
         if (game == null)
         {
             game = new PongGame();
             _games.Add(game);
             game.GameOver += OnGameOver;
         }
         game.JoinPlayer(player);
     }
 }
Beispiel #7
0
 /// <summary>
 /// Joins new player to existing game or creates new one
 /// </summary>
 /// <param name="player"></param>
 public static void JoinPlayer(PongPlayer player)
 {
     lock (_syncRoot)
     {
         var game = _games.Where(g => g.State == GameState.WaitingForPlayer).FirstOrDefault();
         if (game == null)
         {
             game = new PongGame();
             _games.Add(game);
             game.GameOver += OnGameOver;
         }
         game.JoinPlayer(player);
     }
 }
Beispiel #8
0
        private void OnPlayerDisconnected(PongPlayer player)
        {
            lock (_syncRoot)
            {
                // stop the ball moving task
                _ballCancellationTokenSource.Cancel();
                var otherPlayer = OtherPlayer(player);

                if (otherPlayer != null)
                {
                    // close connection to other player, which means game is over
                    otherPlayer.Close();
                }
                if (GameOver != null)
                {
                    GameOver(this);
                }
            }
        }
Beispiel #9
0
 public PongPlayer OtherPlayer(PongPlayer thisPlayer)
 {
     var index = GetPlayerIndex(thisPlayer);
     return _players[index == LeftPlayer ? RightPlayer : LeftPlayer];
 }
Beispiel #10
0
 public int GetPlayerIndex(PongPlayer player)
 {
     return Array.IndexOf(_players, player);
 }
Beispiel #11
0
        private void OnPlayerDisconnected(PongPlayer player)
        {
            lock (_syncRoot)
            {
                // stop the ball moving task
                _ballCancellationTokenSource.Cancel();
                var otherPlayer = OtherPlayer(player);

                if (otherPlayer != null)
                {
                    // close connection to other player, which means game is over
                    otherPlayer.Close();
                }
                if (GameOver != null)
                    GameOver(this);
            }
        }
Beispiel #12
0
        private void OnPlayerMoved(PongPlayer player, PlayerPositionMessage position)
        {
            var otherPlayer = OtherPlayer(player);

            if (otherPlayer != null)
            {
                // send new player position to the other player
                otherPlayer.SendMessage(new PlayerPositionMessage { YPos = player.YPos });
            }
        }
Beispiel #13
0
        public PongPlayer OtherPlayer(PongPlayer thisPlayer)
        {
            var index = GetPlayerIndex(thisPlayer);

            return(_players[index == LeftPlayer ? RightPlayer : LeftPlayer]);
        }
Beispiel #14
0
 public int GetPlayerIndex(PongPlayer player)
 {
     return(Array.IndexOf(_players, player));
 }
Beispiel #15
0
        /// <summary>
        /// Реагирует на движение игрока
        /// </summary>
        /// <param name="player">
        /// Игрок
        /// </param>
        /// <param name="position">
        /// Сообщение с новой позицией игрока
        /// </param>
        private void OnPlayerMoved(PongPlayer player, PlayerPositionMessage position)
        {
            var otherPlayer = OtherPlayer(player);

            if (otherPlayer != null)
            {
                // отправить игроку поицию другого игрока
                otherPlayer.SendMessage(new PlayerPositionMessage { YPos = player.YPos });
            }
        }
Beispiel #16
0
        /// <summary>
        /// С игроком прервано соединение
        /// </summary>
        /// <param name="player">
        /// Игрок
        /// </param>
        private void OnPlayerDisconnected(PongPlayer player)
        {
            lock (_syncRoot)
            {
                // остановить мяч
                _ballCancellationTokenSource.Cancel();
                var otherPlayer = OtherPlayer(player);

                if (otherPlayer != null)
                {
                    // закрыть соединение с другим игроком, что означает окончание игры
                    otherPlayer.Close();
                }
                if (GameOver != null)
                    GameOver(this);
            }
        }