Example #1
0
        void Attack(AttackCommand command, GameTime gameTime)
        {
            avoidingUnits = false;
            isWithinRangeOfTarget = false;

            if (command.Target.IsDead)
            {
                nextCommand();
                return;
            }

            clearPushStatus();
            clearHitWallStatus();

            //float angle = (float)Math.Atan2(command.Target.CenterPoint.Y - centerPoint.Y, command.Target.CenterPoint.X - centerPoint.X);

            float distanceToTarget = Vector2.Distance(centerPoint, command.Target.CenterPoint) - (Radius + command.Target.Radius);

            if (distanceToTarget <= attackRange)
            {
                isWithinRangeOfTarget = true;

                // begin attack animation
                if (timeSinceLastAttack >= attackDelay)
                {
                    timeSinceLastAttack = 0;
                    attackStarted = true;
                }

                turnTowards(command.Target.CenterPoint, 120 / Radius, gameTime);
            }
            else if (!attackStarted)
            {
                float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
                float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);

                Vector2 wayPoint = command.WayPoints[0];

                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;

                    lastWayPoint = wayPoint;
                    if (command.WayPoints.Count > 1)
                        command.NextWayPoint(this, PathFinder);
                    return;
                }

                float angle = (float)Math.Atan2(wayPoint.Y - centerPoint.Y, wayPoint.X - centerPoint.X);
                moveX *= (float)Math.Cos(angle);
                moveY *= (float)Math.Sin(angle);

                lastMove.X = moveX;
                lastMove.Y = moveY;
                PrecisePosition += lastMove;

                checkForWallHit();
                // checkForPush sets avoidingUnits
                checkForPush(command);

                command.Destination = command.Target.CenterPoint;

                turnTowards(wayPoint, 120 / Radius, gameTime);
            }

            if (avoidingUnits)
            {
                if (timeSinceLastRecalculatePath >= recalculatePathDelay * 2)
                {
                    timeSinceLastRecalculatePath = 0;
                    //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, true);
                    PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), true);
                }
            }
            else if (timeSinceLastRecalculatePath >= recalculatePathDelay)
            {
                timeSinceLastRecalculatePath = 0;
                //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, false);
                PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false);
            }
        }
Example #2
0
        //int initialAttackDelay = 0;// = 150;
        void Attack(AttackCommand command, GameTime gameTime)
        {
            avoidingUnits = false;
            isWithinRangeOfTarget = false;

            // if original target is gone, stop attack
            if (command.Target == null || command.Target.IsDead)
            {
                attackStarted = false;

                if (command.HoldPosition)
                    GiveCommand(new HoldPositionCommand(this));
                else
                    NextCommand();
                return;
            }

            clearPushStatus();
            clearHitWallStatus();

            //float angle = (float)Math.Atan2(command.Target.CenterPoint.Y - centerPoint.Y, command.Target.CenterPoint.X - centerPoint.X);

            float distanceToTarget = Vector2.Distance(centerPoint, command.Target.CenterPoint) - (Radius + command.Target.Radius);

            if (distanceToTarget <= attackRange)
            {
                isWithinRangeOfTarget = true;

                // begin attack animation
                if (timeSinceLastAttack >= attackDelay)
                {
                    timeSinceLastAttack = 0;
                    attackStarted = true;
                }

                turnTowards(command.Target.CenterPoint, 120 / Radius, gameTime);
            }
            else if (attackStarted)
            {
                isWithinRangeOfTarget = false;
                attackStarted = false;
            }
            else if (!attackStarted)
            {
                Vector2 wayPoint = command.WayPoints[0];

                if (!command.HoldPosition)
                {
                    //float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
                    //float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);
                    Speed = MathHelper.Min(Speed + acceleration, MaxSpeed);
                    float moveX = Util.ScaleWithGameTime(Speed, gameTime);
                    float moveY = moveX;

                    Vector2 difference = wayPoint - centerPoint;
                    if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                    {
                        this.CenterPoint = wayPoint;
                        HasMoved = true;

                        lastWayPoint = wayPoint;
                        if (command.WayPoints.Count > 1)
                            command.NextWayPoint(this, Rts.pathFinder);
                        return;
                    }

                    float angle = (float)Math.Atan2(wayPoint.Y - centerPoint.Y, wayPoint.X - centerPoint.X);
                    moveX *= (float)Math.Cos(angle);
                    moveY *= (float)Math.Sin(angle);

                    lastMove.X = moveX;
                    lastMove.Y = moveY;
                    PrecisePosition += lastMove;
                    HasMoved = true;
                }

                checkForWallHit(command);
                // checkForPush sets avoidingUnits
                checkForPush(command);

                command.Destination = command.Target.CenterPoint;

                if (!turnTowards(wayPoint, 120 / Radius, gameTime))
                {
                    Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0);
                }

                if (!command.HoldPosition)
                {
                    if (timeSinceLastRecalculatePath >= recalculatePathDelay)// && command.Calculated)
                    {
                        timeSinceLastRecalculatePath = 0;

                        Rts.pathFinder.AddPathFindRequest(command, false, true, avoidingUnits);
                    }
                    // repath to avoid units
                    /*if (avoidingUnits)
                    {
                        if (timeSinceLastRecalculatePath >= recalculatePathDelay)// && command.Calculated)
                        {
                            timeSinceLastRecalculatePath = 0;

                            PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), true);
                        }
                    }
                    // normal repathing
                    else if (timeSinceLastRecalculatePath >= recalculatePathDelay)// && command.Calculated)
                    {
                        timeSinceLastRecalculatePath = 0;

                        PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false);
                    }*/
                }
            }

            // periodically switch or lose target
            if (command.Target.Team != Team && timeSinceLastLookForTarget >= lookForTargetDelay)
            {
                timeSinceLastLookForTarget = 0;

                // switch to closer target if possible
                if (command.WillingToChangeTarget)
                {
                    RtsObject newTarget = FindNearestTarget();

                    if (newTarget == null)
                    {
                        if (command.HoldPosition)
                            GiveCommand(new HoldPositionCommand(this));
                        else
                            NextCommand();
                    }
                    else
                    {
                        command.Target = newTarget;
                        return;
                    }
                }
                //else if (command.WillingToChangeTarget)
                //    command.Target = newTarget;

                // lose target if out of vision
                //if (!command.Target.CurrentPathNode.Tile.Visible)
                if (!command.Target.Visible)
                {
                    //if (command.HoldPosition)
                    //    GiveCommand(new HoldPositionCommand());
                    //else
                    //    nextCommand();
                    command.Target = null;
                }
            }
        }
Example #3
0
        void Attack(AttackCommand command, GameTime gameTime)
        {
            avoidingUnits         = false;
            isWithinRangeOfTarget = false;

            if (command.Target.IsDead)
            {
                nextCommand();
                return;
            }

            clearPushStatus();
            clearHitWallStatus();

            //float angle = (float)Math.Atan2(command.Target.CenterPoint.Y - centerPoint.Y, command.Target.CenterPoint.X - centerPoint.X);

            float distanceToTarget = Vector2.Distance(centerPoint, command.Target.CenterPoint) - (Radius + command.Target.Radius);

            if (distanceToTarget <= attackRange)
            {
                isWithinRangeOfTarget = true;

                // begin attack animation
                if (timeSinceLastAttack >= attackDelay)
                {
                    timeSinceLastAttack = 0;
                    attackStarted       = true;
                }

                turnTowards(command.Target.CenterPoint, 120 / Radius, gameTime);
            }
            else if (!attackStarted)
            {
                float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
                float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);

                Vector2 wayPoint = command.WayPoints[0];

                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;

                    lastWayPoint = wayPoint;
                    if (command.WayPoints.Count > 1)
                    {
                        command.NextWayPoint(this, PathFinder);
                    }
                    return;
                }

                float angle = (float)Math.Atan2(wayPoint.Y - centerPoint.Y, wayPoint.X - centerPoint.X);
                moveX *= (float)Math.Cos(angle);
                moveY *= (float)Math.Sin(angle);

                lastMove.X       = moveX;
                lastMove.Y       = moveY;
                PrecisePosition += lastMove;

                checkForWallHit();
                // checkForPush sets avoidingUnits
                checkForPush(command);

                command.Destination = command.Target.CenterPoint;

                turnTowards(wayPoint, 120 / Radius, gameTime);
            }

            if (avoidingUnits)
            {
                if (timeSinceLastRecalculatePath >= recalculatePathDelay * 2)
                {
                    timeSinceLastRecalculatePath = 0;
                    //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, true);
                    PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), true);
                }
            }
            else if (timeSinceLastRecalculatePath >= recalculatePathDelay)
            {
                timeSinceLastRecalculatePath = 0;
                //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, false);
                PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false);
            }
        }