Exemplo n.º 1
0
        /**
         * Find a path through the tilemap.  Any tile with any collision flags set is treated as impassable.
         * If no path is discovered then a null reference is returned.
         *
         * @param	Start		The start point in world coordinates.
         * @param	End			The end point in world coordinates.
         * @param	Simplify	Whether to run a basic simplification algorithm over the path data, removing extra points that are on the same line.  Default value is true.
         * @param	RaySimplify	Whether to run an extra raycasting simplification algorithm over the remaining path data.  This can result in some close corners being cut, and should be used with care if at all (yet).  Default value is false.
         *
         * @return	A <code>FlxPath</code> from the start to the end.  If no path could be found, then a null reference is returned.
         */
        public FlxPath findPath(FlxPoint Start, FlxPoint End, bool Simplify = true, bool RaySimplify = false)
        {
            //figure out what tile we are starting and ending on.
            int startIndex = (int)((Start.Y - Y) / _tileHeight) * widthInTiles + (int)((Start.X - X) / _tileWidth);
            int endIndex   = (int)((End.Y - Y) / _tileHeight) * widthInTiles + (int)((End.X - X) / _tileWidth);

            //check that the start and end are clear.
            if (((_tileObjects[_data[startIndex]] as FlxTile).AllowCollisions > 0) ||
                ((_tileObjects[_data[endIndex]] as FlxTile).AllowCollisions > 0))
            {
                return(null);
            }

            //figure out how far each of the tiles is from the starting tile
            List <int> distances = computePathDistance(startIndex, endIndex);

            if (distances == null)
            {
                return(null);
            }

            //then count backward to find the shortest path.
            List <FlxPoint> points = new List <FlxPoint>();

            walkPath(distances, endIndex, points);

            //reset the start and end points to be exact
            FlxPoint node;

            node   = points[points.Count - 1] as FlxPoint;
            node.X = Start.X;
            node.Y = Start.Y;
            node   = points[0] as FlxPoint;
            node.X = End.X;
            node.Y = End.Y;

            //some simple path cleanup options
            if (Simplify)
            {
                simplifyPath(points);
            }
            if (RaySimplify)
            {
                raySimplifyPath(points);
            }

            //finally load the remaining points into a new path object and return it
            FlxPath path = new FlxPath();
            int     i    = points.Count - 1;

            while (i >= 0)
            {
                node = points[i--] as FlxPoint;
                if (node != null)
                {
                    path.AddPoint(node, true);
                }
            }
            return(path);
        }
Exemplo n.º 2
0
        /**
         * Find a path through the tilemap.  Any tile with any collision flags set is treated as impassable.
         * If no path is discovered then a null reference is returned.
         *
         * @param	Start		The start point in world coordinates.
         * @param	End			The end point in world coordinates.
         * @param	Simplify	Whether to run a basic simplification algorithm over the path data, removing extra points that are on the same line.  Default value is true.
         * @param	RaySimplify	Whether to run an extra raycasting simplification algorithm over the remaining path data.  This can result in some close corners being cut, and should be used with care if at all (yet).  Default value is false.
         *
         * @return	A <code>FlxPath</code> from the start to the end.  If no path could be found, then a null reference is returned.
         */
        public FlxPath findPath(FlxPoint Start, FlxPoint End, bool Simplify=true, bool RaySimplify=false)
        {
            //figure out what tile we are starting and ending on.
            int startIndex = (int)((Start.Y-Y)/_tileHeight) * widthInTiles + (int)((Start.X-X)/_tileWidth);
            int endIndex = (int)((End.Y-Y)/_tileHeight) * widthInTiles + (int)((End.X-X)/_tileWidth);

            //check that the start and end are clear.
            if( ((_tileObjects[_data[startIndex]] as FlxTile).AllowCollisions > 0) ||
                ((_tileObjects[_data[endIndex]] as FlxTile).AllowCollisions > 0) )
                return null;

            //figure out how far each of the tiles is from the starting tile
            List<int> distances = computePathDistance(startIndex,endIndex);
            if(distances == null)
                return null;

            //then count backward to find the shortest path.
            List<FlxPoint> points = new List<FlxPoint>();
            walkPath(distances,endIndex,points);

            //reset the start and end points to be exact
            FlxPoint node;
            node = points[points.Count-1] as FlxPoint;
            node.X = Start.X;
            node.Y = Start.Y;
            node = points[0] as FlxPoint;
            node.X = End.X;
            node.Y = End.Y;

            //some simple path cleanup options
            if(Simplify)
                simplifyPath(points);
            if(RaySimplify)
                raySimplifyPath(points);

            //finally load the remaining points into a new path object and return it
            FlxPath path = new FlxPath();
            int i = points.Count - 1;
            while(i >= 0)
            {
                node = points[i--] as FlxPoint;
                if(node != null)
                    path.AddPoint(node,true);
            }
            return path;
        }