Exemplo n.º 1
0
        private static void notInstructedClient(string PID, Uri clientURL, List <Uri> serverURLs)
        {
            Hub       hub       = new Hub(serverURLs, clientURL, new SimpleGame());
            Form      form      = new AutomaticStartForm(hub, PID);
            FormStage formStage = null;

            hub.OnStart += (stage) =>
            {
                form.Invoke(new System.Action(() => {
                    form.Hide();
                    formStage = new FormStage(hub, stage);
                    formStage.Show();
                }));
            };

            hub.OnDeath += () =>
            {
                form.Invoke(new System.Action(() =>
                {
                    FormDead f = new FormDead();
                    f.Show();
                }));
            };

            hub.OnGameEnd += (winner) =>
            {
                hub.UnregisterChannel(); //unrigester current channel before creating a new one
                form.Invoke(new System.Action(() =>
                {
                    //MessageBox.Show("END");
                    formStage.Hide();
                    FormEndGame endGame = new FormEndGame(winner);
                    endGame.PID         = PID;
                    endGame.clientURL   = clientURL;
                    endGame.serverURLs  = serverURLs;
                    endGame.Show();
                    //quando um botao for clicado este método tem de ser executado.
                    endGame.OnPlayAgain += (_PID, _clientURL, _serverURLs) =>
                    {
                        notInstructedClient(_PID, _clientURL, _serverURLs);
                    };
                }));
            };

            try
            {
                Application.Run(form);
            }
            catch (InvalidUsernameException exc)
            {
                MessageBox.Show(exc.Message);
                Application.Exit();
            }
        }
Exemplo n.º 2
0
        public void ReceiveRound(List <Shared.Action> actions, List <IPlayer> players, int round)
        {
            mutex.WaitOne();
            //This assures that no previous round is received and applied
            //experimenta
            if (this.roundState.Keys.Count != 0 && this.roundState.Keys.Max() >= round)
            {
                mutex.ReleaseMutex();
                return;
            }

            foreach (Shared.Action action in actions)
            {
                PictureBox pictureBox;
                switch (action.action)
                {
                case Shared.Action.ActionTaken.MOVE:
                    pictureBox = stageObjects[action.ID];

                    Point pos1 = new Point(action.position.X - action.width / 2, action.position.Y - action.height / 2);
                    Point pos2 = fit(pos1);
                    int   x    = pos2.X;
                    int   y    = pos2.Y;

                    Invoke(new System.Action(() =>
                    {
                        if (stageObjectsType[action.ID] == "player")
                        {
                            switch (action.direction)
                            {
                            case Shared.Action.Direction.DOWN:
                                pictureBox.Image = global::pacman.Properties.Resources.down;
                                break;

                            case Shared.Action.Direction.LEFT:
                                pictureBox.Image = global::pacman.Properties.Resources.Left;
                                break;

                            case Shared.Action.Direction.UP:
                                pictureBox.Image = global::pacman.Properties.Resources.Up;
                                break;

                            case Shared.Action.Direction.RIGHT:
                                pictureBox.Image = global::pacman.Properties.Resources.Right;
                                break;
                            }
                        }

                        pictureBox.Location = new Point(x, y);
                    }));
                    break;

                case Shared.Action.ActionTaken.REMOVE:
                    pictureBox = stageObjects[action.ID];
                    Invoke(new System.Action(() => panelGame.Controls.Remove(pictureBox))); // removeu a picture box

                    if (hub.CurrentSession.PlayerId == action.ID)                           // This client cant play anymore
                    {
                        timer1.Stop();                                                      // stop receiving inputs
                        this.Invoke(new System.Action(() =>
                        {
                            FormDead f = new FormDead();
                            f.Show();
                        }));
                    }
                    break;
                }
            }
            string scores = "";

            foreach (IPlayer player in players)
            {
                // check if its the player of the current client window
                if (hub.CurrentSession.Username != player.Username)
                {
                    scores += player.Username + " -- Score: " + player.Score + "\r\n";
                }
                else
                {
                    Task.Run(() =>
                    {
                        string score = player.Score.ToString();
                        this.Invoke(new System.Action(() =>
                        {
                            this.labelScore.Text = score;
                        }));
                    });
                }
            }
            this.Invoke(new System.Action(() =>
            {
                this.textboxPlayers.Text = scores;
            }));

            this.roundState[round] = GetState();
            mutex.ReleaseMutex();
        }