/** * 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); }
/** * 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. * * @param Path The <code>FlxPath</code> you want this object to follow. * @param Speed How fast to travel along the path in pixels per second. * @param 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 AutoRotate Automatically point the object toward the next node. Assumes the graphic is pointing upward. Default behavior is false, or no automatic rotation. */ public void followPath(FlxPath Path, float Speed = 100, uint Mode = PATH_FORWARD, 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 == PATH_BACKWARD) || (_pathMode == PATH_LOOP_BACKWARD)) { _pathNodeIndex = path.nodes.Count - 1; _pathInc = -1; } else { _pathNodeIndex = 0; _pathInc = 1; } }
/** * Tells this object to stop following the path its on. * * @param DestroyPath Tells this function whether to call destroy on the path object. Default value is false. */ public void stopFollowingPath(bool DestroyPath = false) { pathSpeed = 0; if (DestroyPath && (path != null)) { path.destroy(); path = null; } }
/// <summary> /// Override to null out variables manually /// </summary> public override void destroy() { velocity = null; acceleration = null; drag = null; maxVelocity = null; scrollFactor = null; _point = null; _rect = null; last = null; //cameras = null; if (path != null) { path.destroy(); } path = null; }
/// <summary> /// Instantiates a <code>FlxObject</code> /// </summary> /// <param name="X">X-coordinate of the object in space</param> /// <param name="Y">y-coordinate of the object in space</param> /// <param name="Width">Desired width of the rectangle</param> /// <param name="Height">Desired height of the rectangle</param> public FlxObject(float X = 0, float Y = 0, float Width = 0, float Height = 0) : base() { x = X; y = Y; last = new FlxPoint(x, y); width = Width; height = Height; mass = 1.0f; elasticity = 0.0f; health = 1; immovable = false; moves = true; touching = NONE; wasTouching = NONE; allowCollisions = ANY; velocity = new FlxPoint(); acceleration = new FlxPoint(); drag = new FlxPoint(); maxVelocity = new FlxPoint(10000, 10000); angle = 0; angularVelocity = 0; angularAcceleration = 0; angularDrag = 0; maxAngular = 10000; scrollFactor = new FlxPoint(1, 1); _flicker = false; _flickerTimer = 0; _point = new FlxPoint(); _rect = new FlxRect(); path = null; pathSpeed = 0; pathAngle = 0; }
public Player(int X = 0, int Y = 0) : base(X, Y) { maxVelocity = new FlxPoint(100, 100); drag = new FlxPoint(500, 500); makeGraphic(40, 40, FlxColor.CADETBLUE); _currPos = new FlxPoint(x, y); _lastPos = new FlxPoint(x, y); _nodes = new List <FlxPoint>(); _nodes.Add(_lastPos); _nodes.Add(_currPos); _path = new FlxPath(_nodes); footsteps = new FlxEmitter(x + width / 2, y + height / 2, 0, this); footsteps.makeParticles(FlxG.content.Load <Texture2D>("footsteps"), true, 25, 0f, 0f); footsteps.setXSpeed(); footsteps.setYSpeed(); _indoors = false; }