Esempio n. 1
0
 public Game()
 {
     View = new GameOverview();
 }
Esempio n. 2
0
        private void GameLoop()
        {
            // initialize starting components
            View.Settings.GameHeight = View.Players.SmallestHeight;
            View.Settings.GameWidth = View.Players.SmallestWidth;
            View.Players.PaddlePlayers.ForEach(t => t.Paddle = new Paddle(View.Settings));
            View.Players.BallPlayers.ForEach(t => t.Ball = new Ball(View.Settings));
            View.Status = GameStatus.InProgress;

            // broadcast game start to all
            View.Players.ForEach(t => HubContext.Clients.Client(t.ConnectionId).StartGame(View, t.IsPaddleController));

            // game loop until status changed
            while (View.Status == GameStatus.InProgress)
            {
                // ensure players are still present
                if (View.Players.Count == 0)
                {
                    View.Status = GameStatus.Finished;
                    break;
                }

                // make movements of moveable pieces
                View.Players.BallPlayers.ForEach(t => t.Ball.Move());
                View.Players.BallPlayers.ForEach(t => t.Ball.Move());
                View.Players.PaddlePlayers.ForEach(t => t.Paddle.Move());
                View.Players.CheckCollision(View.Settings);

                // goal has been made
                foreach (var player in View.Players.BallPlayers)
                {
                    if (player.Ball.CheckBeyondGoal())
                    {
                        View.Players.ForEach(t => HubContext.Clients.Client(t.ConnectionId).Goal(player));
                        player.Ball.ResetPosition();
                        player.Goals += 1;
                    }
                }

                // sleep based on game speed and rebroadcast pon gmovement
                Thread.Sleep(View.Settings.GameSpeed);
                View.Players.ForEach(t => HubContext.Clients.Client(t.ConnectionId)
                    .Move(new {
                        Balls = View.Players.BallPlayers.Select(b => new { Name = b.Name, BallX = b.Ball.X, BallY = b.Ball.Y }),
                        Paddles = View.Players.PaddlePlayers.Select(b => new { Name = b.Name, PaddleX = b.Paddle.X, PaddleWidth = b.Paddle.Width })
                    }));
            }

            // portray ending of game
            View.Players.ForEach(t => HubContext.Clients.Client(t.ConnectionId).UpdateGame(View));
            View = new GameOverview();
        }