Beispiel #1
0
        /**
         * Internal function for moving the object along the path.
         * Generally this function is called automatically by <code>preUpdate()</code>.
         * The first half of the function decides if the object can advance to the next node in the path,
         * while the second half handles actually picking a velocity toward the next node.
         */
        protected void updatePathMotion()
        {
            //first check if we need to be pointing at the next node yet
            _point.x = x + width * 0.5f;
            _point.y = y + height * 0.5f;
            FlxPoint node   = path.nodes[_pathNodeIndex];
            float    deltaX = node.x - _point.x;
            float    deltaY = node.y - _point.y;

            bool horizontalOnly = (_pathMode & PATH_HORIZONTAL_ONLY) > 0;
            bool verticalOnly   = (_pathMode & PATH_VERTICAL_ONLY) > 0;

            if (horizontalOnly)
            {
                if (((deltaX > 0)?deltaX:-deltaX) < pathSpeed * FlxG.elapsed)
                {
                    node = advancePath();
                }
            }
            else if (verticalOnly)
            {
                if (((deltaY > 0)?deltaY:-deltaY) < pathSpeed * FlxG.elapsed)
                {
                    node = advancePath();
                }
            }
            else
            {
                if (Math.Sqrt(deltaX * deltaX + deltaY * deltaY) < pathSpeed * FlxG.elapsed)
                {
                    node = advancePath();
                }
            }

            //then just move toward the current node at the requested speed
            if (pathSpeed != 0)
            {
                //set velocity based on path mode
                _point.x = x + width * 0.5f;
                _point.y = y + height * 0.5f;
                if (horizontalOnly || (_point.y == node.y))
                {
                    velocity.x = (_point.x < node.x)?pathSpeed:-pathSpeed;
                    if (velocity.x < 0)
                    {
                        pathAngle = -90;
                    }
                    else
                    {
                        pathAngle = 90;
                    }
                    if (!horizontalOnly)
                    {
                        velocity.y = 0;
                    }
                }
                else if (verticalOnly || (_point.x == node.x))
                {
                    velocity.y = (_point.y < node.y)?pathSpeed:-pathSpeed;
                    if (velocity.y < 0)
                    {
                        pathAngle = 0;
                    }
                    else
                    {
                        pathAngle = 180;
                    }
                    if (!verticalOnly)
                    {
                        velocity.x = 0;
                    }
                }
                else
                {
                    pathAngle = FlxU.getAngle(_point, node);
                    FlxU.rotatePoint(0, pathSpeed, 0, 0, pathAngle, velocity);
                }

                //then set object rotation if necessary
                if (_pathRotate)
                {
                    angularVelocity     = 0;
                    angularAcceleration = 0;
                    angle = pathAngle;
                }
            }
        }