Beispiel #1
0
        public void MoveTo(Point destination)
        {
            if (destination.X < 0 || destination.Y < 0 || destination.X >= layer.Width() || destination.Y >= layer.Height() || !layer.IsPassable(destination) || Moving)
            {
                return;
            }

            destinations = new Queue <Point>();

            List <Point> points = layer.Pathfind.FindPath(position, destination, Group);

            for (int i = 0; i < points.Count(); ++i)
            {
                if (CanMoveThrough(points[i]))
                {
                    destinations.Enqueue(points[i]);
                }
                else
                {
                    break;
                }
            }

            if (destinations.Count() > 0)
            {
                moveDest = destinations.Dequeue();
                moving   = true;
                elapsed  = toMove;
            }
        }
Beispiel #2
0
        public void InitializeSearchNodes(TileLayer layer)
        {
            searchNodes = new SearchNode[levelWidth, levelHeight];

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = new SearchNode();

                    node.Position = new Point(x, y);

                    node.Passable = layer.IsPassable(x, y);

                    if (node.Passable)
                    {
                        node.Neighbors = new SearchNode[4];

                        searchNodes[x, y] = node;
                    }
                }
            }

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = searchNodes[x, y];

                    if (node == null || !node.Passable)
                    {
                        continue;
                    }

                    Point[] neighbors = new Point[]
                    {
                        new Point(x, y - 1),
                        new Point(x, y + 1),
                        new Point(x - 1, y),
                        new Point(x + 1, y)
                    };

                    for (int i = 0; i < neighbors.Length; ++i)
                    {
                        Point position = neighbors[i];

                        if (position.X < 0 || position.X >= levelWidth || position.Y < 0 || position.Y >= levelHeight)
                        {
                            continue;
                        }

                        SearchNode neighbor = searchNodes[position.X, position.Y];

                        if (neighbor == null || !neighbor.Passable)
                        {
                            continue;
                        }

                        node.Neighbors[i] = neighbor;
                    }
                }
            }
        }
Beispiel #3
0
        public void InitializeSearchNodes(TileLayer layer)
        {
            searchNodes = new SearchNode[levelWidth, levelHeight];

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = new SearchNode();

                    node.Position = new Point(x, y);

                    node.Passable = layer.IsPassable(x, y);

                    if (node.Passable)
                    {
                        node.Neighbors = new SearchNode[4];

                        searchNodes[x, y] = node;
                    }
                }
            }

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = searchNodes[x, y];

                    if (node == null || !node.Passable)
                        continue;

                    Point[] neighbors = new Point[]
                    {
                        new Point(x, y - 1),
                        new Point(x, y + 1),
                        new Point(x - 1, y),
                        new Point(x + 1, y)
                    };

                    for (int i = 0; i < neighbors.Length; ++i)
                    {
                        Point position = neighbors[i];

                        if (position.X < 0 || position.X >= levelWidth || position.Y < 0 || position.Y >= levelHeight)
                            continue;

                        SearchNode neighbor = searchNodes[position.X, position.Y];

                        if (neighbor == null || !neighbor.Passable)
                            continue;

                        node.Neighbors[i] = neighbor;
                    }
                }
            }
        }