This is a simple path data container. Basically a list of points that a FlxObject can follow. Also has code for drawing debug visuals. FlxTilemap.findPath() returns a path object, but you can also just make your own, using the add() functions below or by creating your own array of points.
예제 #1
0
        /// <summary>
        /// Call this function to give this object a path to follow.
        /// If the path does not have at least one node in it, this function
        /// will log a warning message and return.
        /// </summary>
        /// <param name="path">The <code>FlxPath</code> you want this object to follow.</param>
        /// <param name="speed">How fast to travel along the path in pixels per second.</param>
        /// <param name="mode">Optional, controls the behavior of the object following the path using the path behavior constants. Can use multiple flags at once, for example PATH_YOYO|PATH_HORIZONTAL_ONLY will make an object move back and forth along the X axis of the path only.</param>
        /// <param name="autoRotate">Automatically point the object toward the next node.  Assumes the graphic is pointing upward.  Default behavior is false, or no automatic rotation.</param>
        public void followPath(FlxPath path, float speed = 100, uint mode = PathForward, bool autoRotate = false)
        {
            if (path.Nodes.Count <= 0)
            {
                FlxG.log("WARNING: Paths need at least one node in them to be followed.");
                return;
            }

            Path = path;
            PathSpeed = FlxU.abs(speed);
            _pathMode = mode;
            _pathRotate = autoRotate;

            // get starting node
            if ((_pathMode == PathBackward) || (_pathMode == PathLoopBackward)) // flx# - no bitmask?
            {
                _pathNodeIndex = Path.Nodes.Count - 1;
                _pathInc = -1;
            }
            else
            {
                _pathNodeIndex = 0;
                _pathInc = 1;
            }
        }
예제 #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;
        }