Esempio n. 1
0
 private void OnChildAdded(object sender, ElementEventArgs e)
 {
     Device.BeginInvokeOnMainThread(() =>
     {
         LogScrollView.ScrollToAsync(LogLayout.Children.Last(), ScrollToPosition.MakeVisible, true);
     });
 }
Esempio n. 2
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Factory.StartNew(
                () =>
            {
                try
                {
                    m_service = new GameService(logString => { Invoke(() =>
                        {
                            Log.Items.Add(logString);
                            LogScrollView.ScrollToEnd();
                        }); });

                    m_host = new ServiceHost(m_service);

                    m_host.Open();

                    int port = m_host.Description.Endpoints.Single().ListenUri.Port;

                    string info = string.Format("Started on {0} port", port);

                    Invoke(() => m_statusParameter.Value = info);

                    Task.Factory.StartNew(() => UpdateParamters());
                }
                catch (Exception exc)
                {
                    Invoke(() => m_statusParameter.Value = string.Format("Error ({0})", exc.Message));
                }
            });
        }
Esempio n. 3
0
 private void SetLogAsync(string log)
 {
     Task.Factory.StartNew(() => {
         Log.Text += log;
         LogScrollView.ScrollToBottom();
     }, new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()).Wait();
 }
Esempio n. 4
0
 public void SetLog(string log)
 {
     Task.Factory.StartNew(() => {
         Log.Text += log;
         LogScrollView.ScrollToBottom();
     }, new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
 }
 void LogToScreen(string text)
 {
     Log.Text += $"{DateTime.Now.ToLongTimeString()}: {text}\n";
     InvalidateMeasure();
     LogScrollView.ScrollToAsync(Log, ScrollToPosition.End, false);
 }
Esempio n. 6
0
        /// <summary>
        /// Fills the HitAreaStackPanel with rectangles representing Tiles in the Enemy's Playgrid
        /// This method will clear and rebuild the HitStackPanel if called multiple times
        /// </summary>
        public void FillEnemyGrid()
        {
            HitAreaStackPanel.Children.Clear();
            for (int i = 0; i < usedData.EnemyShipsGrid.Width; i++)
            {
                int        x  = i;
                StackPanel sp = new StackPanel();
                for (int j = 0; j < usedData.EnemyShipsGrid.Height; j++)
                {
                    int       y = j;
                    Rectangle r = new Rectangle()
                    {
                        Height      = HitAreaStackPanel.MinHeight / usedData.EnemyShipsGrid.Height,
                        Width       = HitAreaStackPanel.MinWidth / usedData.EnemyShipsGrid.Width,
                        DataContext = usedData.EnemyShipsGrid.Grid[i, j],
                        RadiusX     = 5,
                        RadiusY     = 5,
                    };
                    Binding b = new Binding("State");
                    b.Converter = t2bObf;
                    r.SetBinding(Rectangle.FillProperty, b);
                    //Fire on click logic here
                    r.MouseLeftButtonDown += async(sender, args) =>
                    {
                        if (isGameRunning)
                        {
                            TileState aimed = ((Tile)r.DataContext).State; //need to lambda because of this line refering to the rectangle
                            if (aimed != TileState.Hit && aimed != TileState.Missed)
                            {
                                TurnIdentifierLabel.Content    = "Enemy Turn";
                                TurnIdentifierLabel.Background = Brushes.IndianRed; //Make the turn label very obvious
                                HitAreaStackPanel.IsEnabled    = false;             //because of Delay(), the player could make another turn, so disable the stackpanel to prevent this from calling from another rectangle
                                bool youHit = usedData.Shoot(x, y, usedData.EnemyShipsGrid);
                                usedData.LogInfo = usedData.LogInfo + $"The Player hit point {ToBattleshipPoint(x, y)}";
                                usedData.LogInfo = usedData.LogInfo + (youHit ? "\nThe Player hits a ship!\n" : "\n");
                                Ship youMaybeHit = usedData.GetJustSank(usedData.EnemyShips);
                                if (youMaybeHit != null)
                                {
                                    usedData.LogInfo = usedData.LogInfo + $" !! The Player sank the enemy's {youMaybeHit.Name}\n";
                                    MessageBox.Show($"You just sank the enemy's {youMaybeHit.Name}!");
                                }
                                bool playerWin = GameEnd();

                                if (!playerWin)                           //AI moves if the game didnt end ie the player did not win
                                {
                                    await Delay(new Random().Next(1500)); //Random time to delay to make the AI seem like its 'thinking'

                                    Logical.Point p        = usedData.ActiveAI.ChoosePoint();
                                    bool          enemyHit = usedData.Shoot(p.X, p.Y, usedData.PlayerShipsGrid);
                                    usedData.LogInfo = usedData.LogInfo + $"The Enemy hit point {ToBattleshipPoint(p.X, p.Y)}";
                                    usedData.LogInfo = usedData.LogInfo + (enemyHit ? "\nThe Enemy hits a ship!\n" : "\n");
                                    Ship enemyMaybeHit = usedData.GetJustSank(usedData.PlayerShips);
                                    if (enemyMaybeHit != null)
                                    {
                                        usedData.LogInfo = usedData.LogInfo + $" !! The Enemy sank the player's {enemyMaybeHit.Name}\n";
                                        MessageBox.Show($"The enemy sank your {enemyMaybeHit.Name}!");
                                    }
                                    LogScrollView.ScrollToBottom();

                                    TurnIdentifierLabel.Content    = "Your turn";
                                    TurnIdentifierLabel.Background = Brushes.LawnGreen;
                                    HitAreaStackPanel.IsEnabled    = true;
                                    GameEnd();
                                }
                            }
                            else
                            {
                                MessageBox.Show("You already fired here");
                            }
                        }
                    };
                    r.MouseEnter += (sender, args) =>
                    {
                        hitAreaMouseX = x;
                        hitAreaMouseY = y;
                    };
                    sp.Children.Add(r);
                }
                HitAreaStackPanel.Children.Add(sp);
            }
        }