我如何使用带有MapsLevel类的路径查找器类以及使我的地图中的路径上的敌人行走的路标?

时间:2014-08-15 21:12:02

标签: c# .net xna xna-4.0

这是我创建的MapsLevels类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TowerDefense
{
    public class Level
    {

        private List<Texture2D> tileTextures = new List<Texture2D>();
        private List<int[,]> MapsArrays = new List<int[,]>();
        private Queue<Vector2> waypoints = new Queue<Vector2>();


        int[,] map = new int[,]
        {
        {1,1,1,0,0,0,0,0,},
        {0,0,1,0,0,0,0,0,},
        {0,0,1,1,0,0,1,1,},
        {0,0,0,1,0,0,1,0,},
        {0,0,1,1,0,0,1,0,},
        {0,0,1,0,0,0,1,0,},
        {0,0,1,1,1,0,1,0,},
        {0,0,0,0,1,1,1,0,},
        };

        int[,] map1 = new int[,]
        {
        {0,0,0,1,1,1,1,0,},
        {0,0,0,1,0,0,1,0,},
        {0,0,0,1,1,0,1,0,},
        {1,1,0,0,1,0,1,0,},
        {0,1,0,0,1,0,1,0,},
        {0,1,1,1,1,0,1,0,},
        {0,0,0,1,1,0,1,1,},
        {0,0,0,0,0,0,0,0,},
        };

        int[,] map2 = new int[,]
        {
        {1,1,0,0,1,0,0,0,},
        {0,1,0,0,1,0,1,1,},
        {0,1,0,0,1,0,1,0,},
        {0,1,1,1,1,1,1,0,},
        {0,0,0,0,0,1,0,0,},
        {0,1,1,1,1,1,1,1,},
        {0,1,0,1,0,0,0,0,},
        {0,1,0,1,0,0,0,0,},
        };

        public Level()
        {

            waypoints.Enqueue(new Vector2(0, 0) * 64);
            waypoints.Enqueue(new Vector2(2, 0) * 64);
            waypoints.Enqueue(new Vector2(2, 3) * 64);
            waypoints.Enqueue(new Vector2(3, 2) * 64);
            waypoints.Enqueue(new Vector2(4, 2) * 64);
            waypoints.Enqueue(new Vector2(4, 4) * 64);
            waypoints.Enqueue(new Vector2(3, 4) * 64);
            waypoints.Enqueue(new Vector2(3, 5) * 64);
            waypoints.Enqueue(new Vector2(2, 5) * 64);
            waypoints.Enqueue(new Vector2(2, 7) * 64);
            waypoints.Enqueue(new Vector2(7, 7) * 64);

            MapsArrays.Add(map);
            MapsArrays.Add(map1);
            MapsArrays.Add(map2);
        }

        public Queue<Vector2> Waypoints
        {
            get { return waypoints; }
        }

        public void AddTexture(Texture2D texture)
        {
            tileTextures.Add(texture);
        }

        public int Width
        {
            get { return map1.GetLength(1); }
        }
        public int Height
        {
            get { return map1.GetLength(0); }
        }

        public void Draw(SpriteBatch batch)
        {
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int textureIndex = MapsArrays[0][y, x];
                    if (textureIndex == -1)
                        continue;

                    Texture2D texture = tileTextures[textureIndex];
                    batch.Draw(texture, new Rectangle(
                        x * 64, y * 64, 64, 64), Color.White);
                }
            }
        }

        public int GetIndex(int cellX, int cellY)
        {
            if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
                return 0;

            return map[cellY, cellX];
        }
    }
}    

我有3张地图,但现在我只使用第一张变量图。 而且我有航路点,但它们不适合地图,因此敌人不会走在我创造的路径上。 我的地图中的路径标有数字1,其余的都是0。

这是我将它添加到此站点的Pathfinder类:

Pathfinder class

这是我的完整项目,也许在整个项目中更容易看到它:

这是我的项目的rar文件,名为TowerDefense:

TowerDefense

问题在于第274行的Pathfinder类:

SearchNode endNode = searchNodes[endPoint.X, endPoint.Y];

我得到异常索引超出了数组的范围。

我最终要做的是以正确的方式使用Pathfinder类,并使敌人在我的路径上移动,路点应自动设置为我的地图中的路径。 并且硬编码就像现在一样。

编辑:

我解决了Game1.cs中的第一个异常,我改变了这一行:

List<Vector2> path = pathfinder.FindPath(new Point(0, 0), new Point(9, 9));

要:

List<Vector2> path = pathfinder.FindPath(new Point(0, 0), new Point(7, 7));

我现在仍然在第282行的Pathfinder类中遇到两个问题:

startNode.InOpenList = true;

由于startNode为null,因此抛出异常null。 在List searchNodes中的许多项目中,在Pathfinder类中,有些项目为空。

第二个问题是在MapsLevel.cs类中如何使航点自动适应地图(变量地图设计),以便敌人在我的地图中的路径上移动?

0 个答案:

没有答案