十年磨一剑

导航

silverlight 坦克四方向简单寻路代码

 /// <summary>
        /// 简单直线寻路函数,适合四方向的精灵寻路
        /// </summary>
        /// <param name="x">坦克原X坐标</param>
        /// <param name="y">坦克原Y坐标</param>
        /// <param name="targetX">目标X坐标</param>
        /// <param name="targetY">目标Y坐标</param>
        /// <returns></returns>
        public CellRef FindPathLine(int x, int y, int targetX, int targetY)
        {
            List<CellRef> cells = new List<CellRef>();
            List<CellRef> list2 = new List<CellRef>();
            CellRef item = new CellRef(null, (y * this._width) + x, x, y, 0, 0);
            CellRef ref4 = null;
            cells.Add(item);
            CellRef ref2 = null;
            while (item != null)
            {
                //定义最大开销为999999
                int fCost = 99999;
                item = null;
                foreach (CellRef ref5 in cells)
                {
                    if (ref5.FCost <= fCost)
                    {
                        fCost = ref5.FCost;
                        item = ref5;
                    }
                }
                if (item == null)
                {
                    return ref4;
                }
                int index = this.IndexOfCell(cells, item.Index);
                if (index >= 0)
                {
                    cells.RemoveAt(index);
                }
                list2.Add(item);
                //目的地找到则返回
                if ((item.X == targetX) && (item.Y == targetY))
                {
                    return item;
                }
                //坦克往前寻路              
                ref2 = this.Get(item, item.X, item.Y - 1, 14, targetX, targetY, item.X + 1, item.Y - 1);
                if ((ref2 != null) && !this.ConstainsCell(list2, ref2.Index))
                {
                    if (!this.ConstainsCell(cells, ref2.Index))
                    {
                        cells.Add(ref2);
                    }
                    else
                    {
                        this.DetermineBest(this.GetCellAtIndex(cells, ref2.Index), ref2);
                    }
                }
                //坦克往左寻路
                ref2 = this.Get(item, item.X - 1, item.Y, 14, targetX, targetY, item.X - 1, item.Y + 1);
                if ((ref2 != null) && !this.ConstainsCell(list2, ref2.Index))
                {
                    if (!this.ConstainsCell(cells, ref2.Index))
                    {
                        cells.Add(ref2);
                    }
                    else
                    {
                        this.DetermineBest(this.GetCellAtIndex(cells, ref2.Index), ref2);
                    }
                }
                //坦克往右寻路

                ref2 = this.Get(item, item.X + 2, item.Y, 10, targetX, targetY, item.X + 2, item.Y + 1);
                if ((ref2 != null) && !this.ConstainsCell(list2, ref2.Index))
                {
                    if (!this.ConstainsCell(cells, ref2.Index))
                    {
                        cells.Add(ref2);
                    }
                    else
                    {
                        this.DetermineBest(this.GetCellAtIndex(cells, ref2.Index), ref2);
                    }
                }
                //坦克往下寻路
                ref2 = this.Get(item, item.X, item.Y + 2, 10, targetX, targetY, item.X + 1, item.Y + 2);
                if ((ref2 != null) && !this.ConstainsCell(list2, ref2.Index))
                {
                    if (!this.ConstainsCell(cells, ref2.Index))
                    {
                        cells.Add(ref2);
                    }
                    else
                    {
                        this.DetermineBest(this.GetCellAtIndex(cells, ref2.Index), ref2);
                    }
                }

            }
            return ref4;
        }

posted on 2010-10-26 10:17  五年磨一剑  阅读(330)  评论(0)    收藏  举报