Beispiel #1
0
        public sound(ship boat, Canvas sea, string message)
        {
            msg  = message;
            seas = sea;
            double          c    = 3.43;
            DoubleAnimation grow = new DoubleAnimation();
            DoubleAnimation move = new DoubleAnimation();

            mark.StrokeThickness = 20;
            mark.Stroke          = Brushes.Black;
            mark.Width           = 0;
            mark.Height          = 0;
            sea.Children.Add(mark);
            Canvas.SetLeft(mark, boat.pos[0]);
            Canvas.SetTop(mark, boat.pos[1]);
            grow.From     = 0;
            grow.To       = 6000 * c * 2;
            grow.Duration = TimeSpan.FromMilliseconds(6000);

            move.By         = -1 * grow.To / 2;
            move.Duration   = grow.Duration;
            grow.Completed += new EventHandler(grow_comp);
            mark.BeginAnimation(Ellipse.WidthProperty, grow, 0);
            mark.BeginAnimation(Ellipse.HeightProperty, grow, 0);
            mark.BeginAnimation(Canvas.LeftProperty, move, 0);
            mark.BeginAnimation(Canvas.TopProperty, move, 0);
        }
Beispiel #2
0
        private int collision(List <sound> pops, ship boat)
        {
            double[] boatLoc = { Canvas.GetLeft(boat.mark) + boat.mark.Width / 2, Canvas.GetTop(boat.mark) + boat.mark.Height / 2 };

            for (int i = 0; i < pops.Count; i++)
            {
                sound pop = pops[i];
                if (pop.mark.Width > 0)
                {
                    double[] soundLoc = { Canvas.GetLeft(pop.mark) + pop.mark.Width / 2, Canvas.GetTop(pop.mark) + pop.mark.Height / 2 };
                    double   circle   = Math.Pow(boatLoc[0] - soundLoc[0], 2) + Math.Pow(boatLoc[1] - soundLoc[0], 2);
                    if (circle < Math.Pow(pop.mark.Width / 2, 2))
                    {
                        return(i);
                    }
                }
            }
            return(-1);
        }
Beispiel #3
0
        private async void btnStart_Click(object sender, RoutedEventArgs e)
        {
            sea.Children.Clear();
            int t = -600;

            double[] coord = new double[4];
            boats[0] = new ship(t, sea, "COM5");
            await Task.Delay(1000);

            boats[1] = new ship(t, sea, "COM8");
            double[] shift = { Math.Min(0, Math.Min(boats[0].pos[0], boats[1].pos[0])), Math.Min(0, Math.Min(boats[0].pos[1], boats[1].pos[1])) };
            boats[0].pos[0] -= shift[0];
            boats[1].pos[0] -= shift[0];
            boats[0].pos[1] -= shift[1];
            boats[1].pos[1] -= shift[1];


            sea.Width  = Math.Max(shift[0], Math.Max(boats[0].pos[0], boats[1].pos[0]));
            sea.Height = Math.Max(shift[1], Math.Max(boats[0].pos[1], boats[1].pos[1]));

            for (int i = 0; i < 2; i++)
            {
                Canvas.SetLeft(boats[i].mark, boats[i].pos[0]);
                Canvas.SetTop(boats[i].mark, boats[i].pos[1]);
            }


            int count = 0;


            List <sound> pop0 = new List <sound>();
            List <sound> pop1 = new List <sound>();

            while (1 == 1)
            {
                string[] rec = { boats[0].ard.recieve(), boats[1].ard.recieve() };
                count++;
                boats[0].move();
                boats[1].move();
                if (rec[0] != "")
                {
                    pop0.Add(new sound(boats[0], sea, rec[0]));
                }
                if (rec[1] != "")
                {
                    pop1.Add(new sound(boats[1], sea, rec[1]));
                }
                if (pop0.Count > 0 && pop0[0] != null && pop0[0].delete == true)
                {
                    pop0.RemoveAt(0);
                }
                if (pop1.Count > 0 && pop1[0] != null && pop1[0].delete == true)
                {
                    pop1.RemoveAt(0);
                }
                if (pop0.Count > 0)
                {
                    int a = collision(pop0, boats[1]);
                    if (a != -1)
                    {
                        boats[1].ard.send(pop0[a].msg);
                        transmission.AppendText(pop0[a].msg);
                        pop0.RemoveAt(a);
                    }
                }
                if (pop1.Count > 0)
                {
                    int b = collision(pop1, boats[0]);
                    if (b != -1)
                    {
                        boats[0].ard.send(pop1[b].msg);
                        transmission.AppendText(pop1[b].msg);
                        pop1.RemoveAt(b);
                    }
                }


                await Task.Delay(1);
            }
        }