private IEnumerator VitaliiAIShoot(Ship shipToAttack)
    {
        if (powerup == PowerUpType.Kraken)
        {
            StartCoroutine(UseKrakenPowerUp(shipToAttack));
        }
        if (powerup == PowerUpType.FireBoat)
        {
            StartCoroutine(UseFireBoatPowerUp());
        }

        bool shoot = true;

        bool      rotate            = false;
        float     rotationAngle     = 90;
        Direction rotationDirection = Direction.Right;

        VisionSphere.VisionPosition shootDirection = visionSphere.position;
        if (visionSphere.position == VisionSphere.VisionPosition.Back)
        {
            rotate = true;

            if (!CannonIsReloading(VisionSphere.VisionPosition.Right))
            {
                shootDirection = VisionSphere.VisionPosition.Right;
            }
            else if (!CannonIsReloading(VisionSphere.VisionPosition.Left))
            {
                rotationDirection = Direction.Left;
                shootDirection    = VisionSphere.VisionPosition.Left;
            }
            else if (!CannonIsReloading(VisionSphere.VisionPosition.Front))
            {
                rotationAngle  = 180;
                shootDirection = VisionSphere.VisionPosition.Front;
            }
            else
            {
                shoot = false;
            }
        }
        else if (CannonIsReloading(shootDirection))
        {
            shoot = false;
        }

        if (shoot)
        {
            if (rotate)
            {
                yield return(Rotate(rotationAngle, rotationDirection));
            }
            Shoot(shootDirection);
        }
        else
        {
            yield return(Flee());
        }
    }
 /// <summary>
 /// Shoot from one of the cannons depending on passed position
 /// </summary>
 /// <param name="position">Position of a cannon</param>
 protected void Shoot(VisionSphere.VisionPosition position)
 {
     if (position == VisionSphere.VisionPosition.Front)
     {
         _cannonFront.Shoot(accuracy, reloadSpeed);
     }
     else if (position == VisionSphere.VisionPosition.Left)
     {
         _cannonLeft.Shoot(accuracy, reloadSpeed);
     }
     else if (position == VisionSphere.VisionPosition.Right)
     {
         _cannonRight.Shoot(accuracy, reloadSpeed);
     }
 }
    /// <summary>
    /// Check if cannon is reloading
    /// </summary>
    /// <param name="position">Posiiton of a cannon</param>
    /// <returns></returns>
    protected bool CannonIsReloading(VisionSphere.VisionPosition position)
    {
        if (position == VisionSphere.VisionPosition.Front)
        {
            return(_cannonFront.reloading);
        }
        if (position == VisionSphere.VisionPosition.Left)
        {
            return(_cannonLeft.reloading);
        }
        if (position == VisionSphere.VisionPosition.Right)
        {
            return(_cannonRight.reloading);
        }

        return(false);
    }
Example #4
0
    public override IEnumerator RunAI(object caller)
    {
        if (!(caller is CompetitionManager))
        {
            yield break;
        }
        while (CompetitionManager.current.gameStarted && (!dying || !CompetitionManager.current.gameOver))
        {
            if (!visibleShips.Any() && !visiblePowerUps.Any())
            {
                VisionSphere.VisionPosition oldPos = visionSphere.position;
                VisionSphere.VisionPosition newPos = oldPos == VisionSphere.VisionPosition.Back
                    ? VisionSphere.VisionPosition.Left
                    : (oldPos == VisionSphere.VisionPosition.Left
                        ? VisionSphere.VisionPosition.Front
                        : (oldPos == VisionSphere.VisionPosition.Front
                            ? VisionSphere.VisionPosition.Right
                            : VisionSphere.VisionPosition.Back));

                yield return(visionSphere.MoveToDirection(newPos));

                Shoot(VisionSphere.VisionPosition.Front);
                Shoot(VisionSphere.VisionPosition.Left);
                Shoot(VisionSphere.VisionPosition.Right);
                yield return(GradusMoveForward(200));
            }
            if (lowHealth)
            {
                if (hasHealing)
                {
                    StartCoroutine(UseHealingPowerUp());
                }
                else
                {
                    IEnumerable <PowerUp> healingPowerUps =
                        visiblePowerUps.Where(powerUp => powerUp.powerUpType == PowerUpType.Healing);
                    if (healingPowerUps.Any())
                    {
                        PowerUp nearestHealingPowerUp = healingPowerUps.OrderBy(powerUp =>
                                                                                Vector3.Distance(powerUp.transform.position, position)).First();
                        yield return(MoveTowards(nearestHealingPowerUp.transform.position));
                    }
                }
            }

            if (visibleShips.Any() && !visiblePowerUps.Any())
            {
                yield return(ShootGradus());
            }

            if (powerup == PowerUpType.Kraken)
            {
                StartCoroutine(UseKrakenPowerUp());
            }

            if (visibleShips.Any() && health <= 50 && powerup == PowerUpType.Speeder)
            {
                StartCoroutine(UseSpeederPowerUp());
                yield return(Hide());
            }

            if (visiblePowerUps.Any() && !visibleShips.Any())
            {
                yield return(CloseToPowerUps());
            }
            if (visiblePowerUps.Any() && visibleShips.Any())
            {
                yield return(CloseToPowerUps());

                yield return(ShootGradus());
            }
        }
    }
    public override IEnumerator RunAI(object caller)
    {
        if (!(caller is CompetitionManager))
        {
            yield break;
        }
        while (CompetitionManager.current.gameStarted && (!dying || !CompetitionManager.current.gameOver))
        {
            if (!visibleShips.Any() && !visiblePowerUps.Any())
            {
                VisionSphere.VisionPosition oldPos = visionSphere.position;
                VisionSphere.VisionPosition newPos = oldPos == VisionSphere.VisionPosition.Back
                    ? VisionSphere.VisionPosition.Left
                    : (oldPos == VisionSphere.VisionPosition.Left
                        ? VisionSphere.VisionPosition.Front
                        : (oldPos == VisionSphere.VisionPosition.Front
                            ? VisionSphere.VisionPosition.Right
                            : VisionSphere.VisionPosition.Back));

                yield return(visionSphere.MoveToDirection(newPos));

                yield return(Explore());
            }
            else if (visibleShips.Any() && !visiblePowerUps.Any())
            {
                Ship shipToAttack = ShipToAttack;

                if (shipToAttack)
                {
                    if (NeedHealing)
                    {
                        if (CanHeal)
                        {
                            StartCoroutine(UseHealingPowerUp());
                            yield return(VitaliiAIShoot(shipToAttack));
                        }
                        else if (shipToAttack.HasPowerup)
                        {
                            if (powerup == PowerUpType.Speeder)
                            {
                                StartCoroutine(UseSpeederPowerUp());
                            }
                            yield return(Flee());
                        }
                    }
                    else
                    {
                        yield return(VitaliiAIShoot(shipToAttack));
                    }
                }
            }
            else if (!visibleShips.Any() && visiblePowerUps.Any())
            {
                if (CanHeal && health <= 100 - Healing.amountToAdd)
                {
                    StartCoroutine(UseHealingPowerUp());
                }

                if (HasPowerup)
                {
                    yield return(Explore());
                }
                else
                {
                    yield return(FindAndGoToNearestPowerUp());
                }
            }
            else if (visibleShips.Any() && visiblePowerUps.Any())
            {
                if (NeedAndCanHeal)
                {
                    StartCoroutine(UseHealingPowerUp());
                }

                Ship shipToAttack = ShipToAttack;

                if (NeedHealing)
                {
                    IEnumerable <PowerUp> healingPowerUps =
                        visiblePowerUps.Where(powerUp => powerUp.powerUpType == PowerUpType.Healing);

                    if (healingPowerUps.Any())
                    {
                        PowerUp nearestHealingPowerUp = healingPowerUps.OrderBy(powerUp =>
                                                                                Vector3.Distance(powerUp.transform.position, position)).First();

                        yield return(VitaliiAIMoveToward(nearestHealingPowerUp.transform.position));
                    }
                    else
                    {
                        yield return(CheckShipAndShoot(shipToAttack));
                    }
                }
                else
                {
                    if (!HasPowerup)
                    {
                        yield return(FindAndGoToNearestPowerUp());
                    }
                    else
                    {
                        yield return(CheckShipAndShoot(shipToAttack));
                    }
                }
            }
        }
    }