Exemplo n.º 1
0
        void EmptyCell_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (Occupied == true || Occupant != null)
            { return; }

            ListBoxItem lbi = (Window1.instance.Towers.SelectedItem as ListBoxItem);

            if (lbi == null)
            { return; }

            Occupied = true;

            Arena.instance.PathInvaliation.Enqueue(this);

            foreach (EmptyCell C in Adjacent)
            {
                if (Arena.instance.PathInvaliation.Contains(C) == false)
                {
                    Arena.instance.PathInvaliation.Enqueue(C);
                }

            }

           // if (Arena.instance.FindPathCells(Arena.instance.Begin, Arena.instance.End) != null)
            {
                Tower b = null;

                //System.Xml.XmlParserContext xpc = new System.Xml.XmlParserContext(

                System.Windows.Markup.ParserContext parserContext;
                parserContext = new System.Windows.Markup.ParserContext();
                parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
                parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");


                TowerData PTD = lbi.Tag as TowerData;

                if ((PTD as HeatTowerData) != null)
                {
                    HeatTower ht = new HeatTower();
                    ht.Data = (PTD as HeatTowerData);

                    object tf = System.Windows.Markup.XamlReader.Parse(ht.Data.XAML, parserContext);
                    FrameworkElement tb = tf as FrameworkElement;

                    Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                    ht.Body = bc;

                    tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                    tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                    ht.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                    ht.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                    ht.VisualObject = tb;
                    Arena.instance.AddToArena(tb);
                    Arena.instance.AddToArena(ht);
                    ht.OwningCell = this;
                    this.Occupant = ht;
                    this.Occupied = true;
                }

                if ((PTD as ProjectileTowerData) != null)
                {
                    ProjectileTower pt = new ProjectileTower();
                    pt.Data = (PTD as ProjectileTowerData);

                    object tf = System.Windows.Markup.XamlReader.Parse(pt.Data.XAML, parserContext);

                    FrameworkElement tb = tf as FrameworkElement;
                    if (tb != null)
                    {
                        Shape bb = LogicalTreeHelper.FindLogicalNode(tb, "_Barrel") as Shape;
                        pt.Barrel = bb;
                        Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                        pt.Body = bc;

                        tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        pt.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.VisualObject = tb;
                        Arena.instance.AddToArena(tb);
                        Arena.instance.AddToArena(pt);
                        pt.OwningCell = this;
                        this.Occupant = pt;
                        this.Occupied = true;
                        //Arena.instance.Children.Add(pt);
                        //Arena.instance.Children.Add(tb);
                    }

                    //b.OwningCell = this;
                    //Arena.instance.BankBalance -= b.Data.Price;

                    //FrameworkElement fe = new FrameworkElement();


                }
                if ((PTD as BeamTowerData) != null)
                {
                    BeamTower pt = new BeamTower();
                    pt.Data = (PTD as BeamTowerData);

                    object tf = System.Windows.Markup.XamlReader.Parse(pt.Data.XAML, parserContext);

                    FrameworkElement tb = tf as FrameworkElement;
                    if (tb != null)
                    {
                        Shape bb = LogicalTreeHelper.FindLogicalNode(tb, "_Barrel") as Shape;
                        pt.Barrel = bb;
                        Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                        pt.Body = bc;

                        tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        pt.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.VisualObject = tb;
                        Arena.instance.AddToArena(tb);
                        Arena.instance.AddToArena(pt);
                        pt.OwningCell = this;
                        this.Occupant = pt;
                        this.Occupied = true;
                        //Arena.instance.Children.Add(pt);
                        //Arena.instance.Children.Add(tb);
                    }

                    //b.OwningCell = this;
                    //Arena.instance.BankBalance -= b.Data.Price;

                    //FrameworkElement fe = new FrameworkElement();


                }

                //TowerData
                //
              //  MessageBox.Show((lbi.Tag as System.Xml.XPath.XPathItem).Value);
                /*
                switch (Window1.instance.Towers.SelectedIndex)
                {
                    case 0:
                        b = new ShortProjectileTower();

                        break;
                    case 1:

                        b = new MediumProjectileTower();
                        break;
                    case 2:
                        b = new LongProjectileTower();
                        break;
                    default:
                        Occupied = false;
                        break;

                }
                */
                if (b != null)
                {
                    if (Arena.instance.BankBalance >= b.Data.Price)
                    {
                        b.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        b.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        b.OwningCell = this;
                        Arena.instance.Children.Add(b);
                        Arena.instance.BankBalance -= b.Data.Price;
                        Occupant = b;
                    }
                    else
                    {
                        Occupied = false;
                    }


                }
                //Occupied = true;


            }
           // else
            {
              //  Occupied = false;

            }
            foreach (EmptyCell C in Adjacent)
            {
                if (Arena.instance.PathInvaliation.Contains(C) == false)
                {
                    Arena.instance.PathInvaliation.Enqueue(C);
                }

            }

            //Arena.instance.DrawTrack();
            //Arena.instance.FindPath();
        }
Exemplo n.º 2
0
        void EmptyCell_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (Occupied == true || Occupant != null)
            {
                return;
            }

            ListBoxItem lbi = (Window1.instance.Towers.SelectedItem as ListBoxItem);

            if (lbi == null)
            {
                return;
            }

            Occupied = true;

            Arena.instance.PathInvaliation.Enqueue(this);

            foreach (EmptyCell C in Adjacent)
            {
                if (Arena.instance.PathInvaliation.Contains(C) == false)
                {
                    Arena.instance.PathInvaliation.Enqueue(C);
                }
            }

            // if (Arena.instance.FindPathCells(Arena.instance.Begin, Arena.instance.End) != null)
            {
                Tower b = null;

                //System.Xml.XmlParserContext xpc = new System.Xml.XmlParserContext(

                System.Windows.Markup.ParserContext parserContext;
                parserContext = new System.Windows.Markup.ParserContext();
                parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
                parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");


                TowerData PTD = lbi.Tag as TowerData;

                if ((PTD as HeatTowerData) != null)
                {
                    HeatTower ht = new HeatTower();
                    ht.Data = (PTD as HeatTowerData);

                    object           tf = System.Windows.Markup.XamlReader.Parse(ht.Data.XAML, parserContext);
                    FrameworkElement tb = tf as FrameworkElement;

                    Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                    ht.Body = bc;

                    tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                    tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                    ht.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                    ht.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                    ht.VisualObject = tb;
                    Arena.instance.AddToArena(tb);
                    Arena.instance.AddToArena(ht);
                    ht.OwningCell = this;
                    this.Occupant = ht;
                    this.Occupied = true;
                }

                if ((PTD as ProjectileTowerData) != null)
                {
                    ProjectileTower pt = new ProjectileTower();
                    pt.Data = (PTD as ProjectileTowerData);

                    object tf = System.Windows.Markup.XamlReader.Parse(pt.Data.XAML, parserContext);

                    FrameworkElement tb = tf as FrameworkElement;
                    if (tb != null)
                    {
                        Shape bb = LogicalTreeHelper.FindLogicalNode(tb, "_Barrel") as Shape;
                        pt.Barrel = bb;
                        Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                        pt.Body = bc;

                        tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        pt.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.VisualObject = tb;
                        Arena.instance.AddToArena(tb);
                        Arena.instance.AddToArena(pt);
                        pt.OwningCell = this;
                        this.Occupant = pt;
                        this.Occupied = true;
                        //Arena.instance.Children.Add(pt);
                        //Arena.instance.Children.Add(tb);
                    }

                    //b.OwningCell = this;
                    //Arena.instance.BankBalance -= b.Data.Price;

                    //FrameworkElement fe = new FrameworkElement();
                }
                if ((PTD as BeamTowerData) != null)
                {
                    BeamTower pt = new BeamTower();
                    pt.Data = (PTD as BeamTowerData);

                    object tf = System.Windows.Markup.XamlReader.Parse(pt.Data.XAML, parserContext);

                    FrameworkElement tb = tf as FrameworkElement;
                    if (tb != null)
                    {
                        Shape bb = LogicalTreeHelper.FindLogicalNode(tb, "_Barrel") as Shape;
                        pt.Barrel = bb;
                        Shape bc = LogicalTreeHelper.FindLogicalNode(tb, "_Body") as Shape;
                        pt.Body = bc;

                        tb.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        tb.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        pt.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        pt.VisualObject = tb;
                        Arena.instance.AddToArena(tb);
                        Arena.instance.AddToArena(pt);
                        pt.OwningCell = this;
                        this.Occupant = pt;
                        this.Occupied = true;
                        //Arena.instance.Children.Add(pt);
                        //Arena.instance.Children.Add(tb);
                    }

                    //b.OwningCell = this;
                    //Arena.instance.BankBalance -= b.Data.Price;

                    //FrameworkElement fe = new FrameworkElement();
                }

                //TowerData
                //
                //  MessageBox.Show((lbi.Tag as System.Xml.XPath.XPathItem).Value);

                /*
                 * switch (Window1.instance.Towers.SelectedIndex)
                 * {
                 *  case 0:
                 *      b = new ShortProjectileTower();
                 *
                 *      break;
                 *  case 1:
                 *
                 *      b = new MediumProjectileTower();
                 *      break;
                 *  case 2:
                 *      b = new LongProjectileTower();
                 *      break;
                 *  default:
                 *      Occupied = false;
                 *      break;
                 *
                 * }
                 */
                if (b != null)
                {
                    if (Arena.instance.BankBalance >= b.Data.Price)
                    {
                        b.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty));
                        b.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty));
                        b.OwningCell = this;
                        Arena.instance.Children.Add(b);
                        Arena.instance.BankBalance -= b.Data.Price;
                        Occupant = b;
                    }
                    else
                    {
                        Occupied = false;
                    }
                }
                //Occupied = true;
            }
            // else
            {
                //  Occupied = false;
            }
            foreach (EmptyCell C in Adjacent)
            {
                if (Arena.instance.PathInvaliation.Contains(C) == false)
                {
                    Arena.instance.PathInvaliation.Enqueue(C);
                }
            }

            //Arena.instance.DrawTrack();
            //Arena.instance.FindPath();
        }