示例#1
0
        /// <summary>
        /// Сюда будут приходить все сообщения от 2 игрока
        /// </summary>
        void WorkerRecieveMessagesEvent(string message)
        {
            //есть 5 видов сообщений: 1) при завершении игры посылается close
            //2) первое сообщение - это ник присоединившегося игрока
            //3) второе подтверждение OK_ник игрока хостера
            //4) сообщение вида 1_5 где 1 - строка, 5 столбец. причем нумерация столбцов и строк идет с 1
            //5) значение 0, 1 или 2. Это возможные результаты хода: 0 - мимо, 1 - ранил, 2 - убил

            if (message == "close")
            {
                MessageBox.Show(string.Format("Game over. {0} win!", Model.CurrentNick));
                ResetGame();
                return;
            }
            var messageItems = message.Split('_');

            if (messageItems.Length == 1 && !Utils.IsNum(messageItems[0]))
            //нет ни одного разделения по "_", close и статус мы исключили, остается ник (2 пункт)
            {
                SetEnemyNick(message);
                Worker.SendMessage(string.Format("OK_{0}", Model.CurrentNick));
            }
            if (messageItems.Length == 2 && messageItems[0] == "OK")//пункт 3
            {
                SetEnemyNick(messageItems[1]);
                SetGameStatus(GameStatus.InGameTurn);                                                     //джойнившийся ходит первым после получения ника от сервера
            }
            if (messageItems.Length == 2 && Utils.IsNum(messageItems[0]) && Utils.IsNum(messageItems[1])) //пункт 4
            {
                int x = Convert.ToInt32(messageItems[0]) - 1;
                int y = Convert.ToInt32(messageItems[1]) - 1;
                ProcessEnemyTurn(x, y);
                CurrentBackground.Invalidate();
            }
            if (messageItems.Length == 1 && Utils.IsNum(messageItems[0]))           //пункт 5
            {
                TurnsResult status = (TurnsResult)Convert.ToInt32(messageItems[0]); //int будет приведен к enum
                //это сообщение приходит в ответ на ход текущего игрока, поэтому мы уже знаем координаты ячейки которую атаковал игрок
                //теперь когда у нас есть результат хода можно установить на ее месте точку или хит
                var lastCurrentCoords = Model.LastCurrentCoords;
                Model.Enemy[lastCurrentCoords.X][lastCurrentCoords.Y] = status == TurnsResult.Miss ? Cell.Miss : Cell.Dead;

                if (status == TurnsResult.Kill || status == TurnsResult.Hit)
                {//если игрок попал он ходит еще раз, иначе ждет сообщения с координатами хода противника
                    SetGameStatus(GameStatus.InGameTurn);
                }
                if (status == TurnsResult.Kill)
                {
                    MarkShipAsDead(Model.Enemy, lastCurrentCoords.X, lastCurrentCoords.Y);
                }

                EnemyBackground.Invalidate();
            }
        }
示例#2
0
        private void CurrentBackgroundMouseClick(object sender, MouseEventArgs e)
        {
            if (Model.Status == GameStatus.Ready)
            {
                Point coord = Get2DByGround(new Point(e.X, e.Y));

                if (e.Button == MouseButtons.Left)
                {
                    TrySetupCell(Model.Current, coord.X, coord.Y);
                }
                if (e.Button == MouseButtons.Right)
                {
                    Model.Current[coord.X][coord.Y] = Cell.Empty;
                }

                RecountReadyShips(Model.Current, out Model.CurrentShips);
                UpdateShipsLabels(Model.CurrentShips);
                CurrentBackground.Invalidate();
            }
        }
示例#3
0
        void ResetGame()
        {
            SetGameStatus(GameStatus.Ready);

            for (int i = 0; i < Model.Current.Length; i++)
            {
                for (int j = 0; j < Model.Current[0].Length; j++)
                {
                    Model.Current[i][j] = Cell.Empty;
                    Model.Enemy[i][j]   = Cell.Empty;
                }
            }
            Worker.Reset();


            RecountReadyShips(Model.Current, out Model.CurrentShips);
            UpdateShipsLabels(Model.CurrentShips);

            CurrentBackground.Invalidate();
            EnemyBackground.Invalidate();
        }
 //#region INotifyPropertyChanged implementation
 //public event PropertyChangedEventHandler PropertyChanged;
 //protected void Notify(string propertyName)
 //{
 //    if (this.PropertyChanged != null)
 //    {
 //        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 //    }
 //}
 //#endregion INotifyPropertyChanged implementation
 //double _glyphScale = 1;
 //BitmapSource _Image;
 //Color _ColorText;
 //Color _ColorName;
 //Point _TextStart;
 //Point _NameStart;
 //int _LineSpacing;
 //public Point TextStart
 //{
 //    get { return _TextStart; }
 //    private set
 //    {
 //        if (value != _TextStart)
 //        {
 //            _TextStart = value;
 //            Notify("TextStart");
 //        }
 //    }
 //}
 //public Point NameStart
 //{
 //    get { return _NameStart; }
 //    private set
 //    {
 //        if (value != _NameStart)
 //        {
 //            _NameStart = value;
 //            Notify("NameStart");
 //        }
 //    }
 //}
 //public double GlyphScale
 //{
 //    get { return _glyphScale; }
 //    private set
 //    {
 //        if (value != _glyphScale)
 //        {
 //            _glyphScale = value;
 //            Notify("GlyphScale");
 //        }
 //    }
 //}
 //public BitmapSource Image
 //{
 //    get { return _Image; }
 //    private set
 //    {
 //        if (value != _Image)
 //        {
 //            _Image = value;
 //            Drawing.ImageSource = _Image;
 //            Drawing.Rect = new Rect(0, 0, _Image.Width, _Image.Height);
 //            Rect.Rect = Drawing.Rect;
 //            Notify("Image");
 //        }
 //    }
 //}
 //public Color ColorText
 //{
 //    get { return _ColorText; }
 //    set
 //    {
 //        if (_ColorText != value)
 //        {
 //            _ColorText = value;
 //            Notify("ColorText");
 //        }
 //    }
 //}
 //public Color ColorName
 //{
 //    get { return _ColorName; }
 //    set
 //    {
 //        if (_ColorName != value)
 //        {
 //            _ColorName = value;
 //            Notify("ColorName");
 //        }
 //    }
 //}
 //public int LineSpacing
 //{
 //    get { return _LineSpacing; }
 //    set
 //    {
 //        if (_LineSpacing != value)
 //        {
 //            _LineSpacing = value;
 //            Notify("LineSpacing");
 //        }
 //    }
 //}
 //public ImageDrawing Drawing { get; private set; } = new ImageDrawing();
 //public RectangleGeometry Rect { get; private set; } = new RectangleGeometry();
 public Backgrounds()
 {
     BackgroundList.Add("Default");
     CurrentBackground.SetDefault();
     GetBackgroundList();
 }
示例#5
0
 public async void SlideBackground()
 {
     await CurrentBackground.ScaleTo(1.1, 40000, Easing.SinIn);
 }