/// <summary> /// Выстрел по мне. /// </summary> private async void MyGameField_ShotMyField(object sender, ShotEventArgs e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Rectangle rectangle = new Rectangle(); Player1Grid.Children.Add(rectangle); Grid.SetColumn(rectangle, e.Hits[0].X); Grid.SetRow(rectangle, e.Hits[0].Y); if (e.ShotResult == Game.ShotResult.Miss) //промах { rectangle.Fill = new SolidColorBrush(killColor); Model.CanShot = true; } else if (e.ShotResult == Game.ShotResult.Damage) //ранил { SetImage("ms-appx:///Assets/Ships/Hurt.png", 1, 1, e.Hits[0].X, e.Hits[0].Y, Player1Grid); Model.CanShot = false; } else if (e.ShotResult == Game.ShotResult.Kill) //убил { Ship s = (Ship)e.Ship.Clone(); ClientShip ship = new ClientShip(s.Id, s.ShipClass, s.Orientation, s.Location); // сюда передается кораблик KillShip(ship, Player1Grid); SetImage("ms-appx:///Assets/Ships/Hurt.png", ship.ShipWidth, ship.ShipHeight, ship.Location.X, ship.Location.Y, Player1Grid); Model.CanShot = false; } }); if (MyGameField.IsGameOver) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Canvas.SetZIndex(panel, 7); panel.Background = new SolidColorBrush(Colors.White); tbGameResult.Text = "Вы проиграли"; btnStartNewGame.Visibility = Visibility.Visible; }); } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (Model.CanShot == true) { tbWait.Visibility = Visibility.Collapsed; tbGo.Visibility = Visibility.Visible; progressRing.IsActive = false; } else { StateObject state = new StateObject(); state.workSocket = Model.PlayerSocket; state.obj = MyGameField; Model.PlayerSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallbackEnemyShot), state); } }); }
/// <summary> /// выстрел по противнику /// </summary> private async void EnemyGameField_EnemyShot(object sender, ShotEventArgs e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Rectangle rectangle = new Rectangle(); Player2Grid.Children.Add(rectangle); Grid.SetColumn(rectangle, e.Hits[0].X); Grid.SetRow(rectangle, e.Hits[0].Y); if (e.ShotResult == Game.ShotResult.Miss) //промах { rectangle.Fill = new SolidColorBrush(killColor); Model.CanShot = false; } else if (e.ShotResult == Game.ShotResult.Damage) //ранил { SetImage("ms-appx:///Assets/Ships/Hurt.png", 1, 1, e.Hits[0].X, e.Hits[0].Y, Player2Grid); Model.CanShot = true; } else if (e.ShotResult == Game.ShotResult.Kill) //убил { Ship s = (Ship)e.Ship.Clone(); ClientShip ship = new ClientShip(s.Id, s.ShipClass, s.Orientation, s.Location); KillShip(ship, Player2Grid); SetImage(ship.Source, ship.ShipWidth, ship.ShipHeight, ship.Location.X, ship.Location.Y, Player2Grid); Model.CanShot = true; } }); if (EnemyGameField.IsGameOver) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Canvas.SetZIndex(panel, 7); panel.Background = new SolidColorBrush(Colors.White); tbGameResult.Text = "Вы победили"; btnStartNewGame.Visibility = Visibility.Visible; }); } await AwaitEnemyView(); }