Exemple #1
0
        private void main_loop()
        {
            //MessageBox.Show(running.ToString());
            Boolean sent = false;

            while (true)
            {
                while (pmc.getFrozen())
                {
                    while (gameStates.Count > 0)
                    {
                        gameStates.Dequeue();
                    }
                }
                if (sent)
                {
                    Thread.Sleep(Int32.Parse(round_timer));
                }
                #region Ask for input and send it to the server

                if (!sent && running && (goleft || goright || goup || godown) && traceMoves.Count == 0)
                {
                    List <String> dirs = new List <String>();
                    if (goleft)
                    {
                        dirs.Add("LEFT");
                    }
                    if (goright)
                    {
                        dirs.Add("RIGHT");
                    }
                    if (goup)
                    {
                        dirs.Add("UP");
                    }
                    if (godown)
                    {
                        dirs.Add("DOWN");
                    }

                    reqObj.RequestMove(PlayersID.FirstOrDefault(x => x.Value == 1).Key, dirs, round);
                    sent = true;
                }
                else if (!sent && traceMoves.Count != 0 && running == true)
                {
                    List <String> dirs = new List <String>();
                    dirs.Add(traceMoves[round]);
                    //traceMoves.Remove(round);
                    reqObj.RequestMove(PlayersID.FirstOrDefault(x => x.Value == 1).Key, dirs, round);
                    sent = true;
                }

                #endregion


                #region update game State
                //while (gameStates.Count <= 0) Thread.Sleep(1); //WAIT FOR UPDATES FROM THE SEVER
                if (gameStates.Count > 0)
                { //WAIT FOR UPDATES FROM THE SEVER
                    sent = false;
                    GameState gm = gameStates.Dequeue();

                    foreach (DTOPlayer player in gm.players) //Update Players Positions
                    {
                        PictureBox p = (PictureBox)this.Controls.Find("pacman" + PlayersID[player.name], true)[0];

                        changeControlPosition(this, new PacEventArgs(player.posX, player.posY, p));
                        changePacDir(this, new PacEventArgs(p, player.faceDirection));
                    }

                    foreach (DTOGhost ghost in gm.ghosts) // Update Ghosts Positions
                    {
                        PictureBox g = (PictureBox)this.Controls.Find("Ghost" + ghost.color, true)[0];
                        changeControlPosition(this, new PacEventArgs(ghost.posX, ghost.posY, g));
                    }

                    foreach (DTOCoin c in gm.coins)     //  Update Coins Visibility
                    {
                        if (c.taken)
                        {
                            Point      pos  = new Point(c.posX, c.posY);
                            PictureBox coin = (PictureBox)GetControlByPosAndType(pos, new PictureBox().GetType());
                            changeCoinVisibility(this, new PacEventArgs(coin, "Doesn't matter"));
                        }
                    }
                    String    myName = PlayersID.FirstOrDefault(x => x.Value == 1).Key;
                    DTOPlayer play   = gm.players.FirstOrDefault(x => x.name == myName);
                    int       score  = 0;
                    foreach (DTOPlayer pl in gm.players)
                    {
                        if (pl.Equals(play))
                        {
                            score = pl.score; break;
                        }
                    }
                    changeTxtText(this, new PacEventArgs(this.Controls.Find("label1", true)[0], "Score: " + score.ToString()));

                    // Do something with the walls, maybe next delivery

                    //update round
                    round = gm.round;

                    if (endgame)
                    {
                        changeTxtText(this,
                                      new PacEventArgs((this.Controls.Find("label2", true)[0]), (play.won) ? "WON" : "GAME OVER!")
                                      );
                        break;
                    }
                }
                #endregion
            }
        }