Exemple #1
0
    private void Update()
    {
        if (!kamikazeMode)
        {
            if (currentRockets <= 0)
            {
                kamikazeMode     = true;
                gameObject.layer = LayerMaskExtensions.layermask_to_layer(whatAreOurProjectiles);
                GetComponent <Collider2D>().isTrigger = true;
            }

            curSpd = Mathf.SmoothDamp(curSpd, speed, ref veloc4, 6f);

            if (returningToBaseAlt)
            {
                transform.up = Vector3.MoveTowards(transform.up, retPosition, Random.Range(0.07f, 0.1f));

                if (transform.position.y >= yBaseAltitude)
                {
                    returningToBaseAlt        = false;
                    enemyIsTooCloseEvadeTimer = 0;
                }

                return;
            }

            if (transform.position.y <= waterLevel + 4f && !returningToBaseAlt)
            {
                returningToBaseAlt = true;
                evading            = false;
                retPosition        = new Vector3(transform.up.x * 30f, yBaseAltitude + Random.Range(4f, 6f), transform.position.z);
            }

            if (currentRockets == 0)
            {
                currentRockets = -1;
                Debug.Log("OUT OF ROCKETS!");
            }

            if (evading == true)
            {
                if (Time.time > evadeTimer)
                {
                    evading = false;
                    return;
                }

                if ((target.position - transform.position).magnitude <= aircraftEscapeRange)
                {
                    enemyIsTooCloseEvadeTimer += Time.deltaTime;

                    if (enemyIsTooCloseEvadeTimer >= 5f)
                    {
                        Debug.Log("Too close!");
                        int rand = Random.Range(0, 30) > 20 ? -1 : 1;
                        retPosition        = new Vector3(transform.up.x * Random.Range(14f, 30f) * rand, yBaseAltitude + Random.Range(4f, 14f), transform.position.z);
                        evading            = false;
                        returningToBaseAlt = true;
                    }
                }
                else
                {
                    enemyIsTooCloseEvadeTimer = 0;
                }

                transform.up = Vector3.MoveTowards(transform.up, (evadePos - transform.position).normalized, 0.06f);
                return;
            }

            if (target != null)
            {
                if ((target.position - transform.position).magnitude >= targetCheckRadius)
                {
                    target = null;
                    return;
                }

                bool targetIsVisual = CheckIfLookingAtTarget();

                if (targProps.whatAmI == UnitType.Aircraft)
                {
                    Vector3 dist = (target.position - transform.position + (Vector3)targRb.velocity / 2f).normalized;
                    transform.up = Vector3.MoveTowards(transform.up, dist, 0.04f);

                    if ((target.position - transform.position).magnitude <= aircraftEscapeRange)
                    {
                        evadeVSI = transform.up.x > 0 ? Mathf.CeilToInt(transform.up.x) : Mathf.FloorToInt(transform.up.x);
                        float yAlt = evadeVSI == 1 ? (evadeYAltitude * evadeVSI) * Random.Range(2f, 3f) : (evadeYAltitude * evadeVSI) / Random.Range(8f, 14f);
                        evadePos   = new Vector3(transform.up.x * 100f, yAlt, transform.position.z);
                        evading    = true;
                        evadeTimer = Time.time + evasionLength;
                        return;
                    }
                }
                else // target's a ship yeet
                {
                    Vector3 dist = (target.position - transform.position).normalized;
                    transform.up = Vector3.MoveTowards(transform.up, dist, 0.04f);

                    if ((target.position - transform.position).magnitude <= shipEscapeRange)
                    {
                        evadeVSI = transform.up.x > 0 ? Mathf.CeilToInt(transform.up.x) : Mathf.FloorToInt(transform.up.x);
                        float yAlt = evadeVSI == 1 ? (evadeYAltitude * evadeVSI) / Random.Range(1.4f, 1.6f) : (evadeYAltitude * evadeVSI) / Random.Range(8f, 14f);
                        evadePos   = new Vector3(transform.up.x * 100f, yAlt, transform.position.z);
                        evading    = true;
                        evadeTimer = Time.time + evasionLength;
                        return;
                    }
                }

                if (targetIsVisual)
                {
                    if (Time.time > rocketTimer && currentRockets > 0)
                    {
                        FireRocket();
                    }
                }
            }
            else
            {
                if (FindTarget())
                {
                    targProps = target.GetComponent <ShipHumanoid>();
                    targRb    = target.GetComponent <Rigidbody2D>();
                    return; // looping back to the start since target was found
                }
                else
                {
                    findTargetTimer = Time.time + targetCheckDelay;
                }

                transform.up = Vector3.MoveTowards(transform.up, new Vector2(transform.up.x, 0f), 0.01f);
            }
        }
        else
        {
            if (target)
            {
                if ((target.position - transform.position).magnitude >= targetCheckRadius)
                {
                    target = null;
                    return;
                }

                Vector3 dist = (target.position - transform.position).normalized;
                transform.up = Vector3.MoveTowards(transform.up, dist, 0.04f);

                curSpd = Mathf.SmoothDamp(curSpd, speed * 1.4f, ref veloc4, 7f);
            }

            if (FindTarget())
            {
                targProps = target.GetComponent <ShipHumanoid>();
                targRb    = target.GetComponent <Rigidbody2D>();
                return; // looping back to the start since target was found
            }
            else
            {
                findTargetTimer = Time.time + targetCheckDelay;
            }
        }
    }
Exemple #2
0
    private void Update()
    {
        if (returningToBaseAlt)
        {
            transform.up = Vector3.MoveTowards(transform.up, retPosition, Random.Range(0.07f, 0.1f));

            if (transform.position.y >= yBaseAltitude)
            {
                if (!takenOff)
                {
                    takenOff = true;
                }

                returningToBaseAlt        = false;
                enemyIsTooCloseEvadeTimer = 0;
                currentShells             = ammoPerMagazine;
            }

            return;
        }

        if (!takenOff)
        {
            curSpd = Mathf.SmoothDamp(curSpd, speed, ref veloc4, timeToTakeOffFully / 3f);
        }
        else
        {
            if (transform.position.y <= waterLevel + 7f && !returningToBaseAlt)
            {
                returningToBaseAlt = true;
                evading            = false;
                retPosition        = new Vector3(transform.up.x * 30f, yBaseAltitude + Random.Range(4f, 6f), transform.position.z);
            }

            if (currentRockets == 0)
            {
                currentRockets = -1;
                StartCoroutine(ReloadRockets());
            }

            if (evading == true)
            {
                if (Time.time > evadeTimer)
                {
                    evading       = false;
                    currentShells = ammoPerMagazine;
                    return;
                }

                if ((target.position - transform.position).magnitude <= aircraftEscapeRange)
                {
                    enemyIsTooCloseEvadeTimer += Time.deltaTime;

                    if (enemyIsTooCloseEvadeTimer >= 5f)
                    {
                        Debug.Log("Too close!");
                        int rand = Random.Range(0, 30) > 20 ? -1 : 1;
                        retPosition        = new Vector3(transform.up.x * Random.Range(14f, 30f) * rand, yBaseAltitude + Random.Range(4f, 14f), transform.position.z);
                        evading            = false;
                        returningToBaseAlt = true;
                    }
                }
                else
                {
                    enemyIsTooCloseEvadeTimer = 0;
                }

                transform.up = Vector3.MoveTowards(transform.up, (evadePos - transform.position).normalized, 0.06f);
                return;
            }

            if (currentShells <= 0)
            {
                evadeVSI   = transform.up.x > 0 ? Mathf.CeilToInt(transform.up.x) : Mathf.FloorToInt(transform.up.x);
                evadeVSI  *= -1;
                evadePos   = new Vector3(transform.up.x * 100f * evadeVSI, evadeYAltitude * Random.Range(2f, 3f), transform.position.z);
                evading    = true;
                evadeTimer = (Time.time + evasionLength) * 1.2f;
                return;
            }

            if (target != null)
            {
                if ((target.position - transform.position).magnitude >= targetCheckRadius)
                {
                    target = null;
                    return;
                }

                bool targetIsVisual = CheckIfLookingAtTarget();

                if (targProps.whatAmI == UnitType.Aircraft)
                {
                    Vector3 dist = (target.position - transform.position + (Vector3)targRb.velocity / 2f).normalized;
                    transform.up = Vector3.MoveTowards(transform.up, dist, 0.04f);

                    if ((target.position - transform.position).magnitude <= aircraftEscapeRange)
                    {
                        evadeVSI = transform.up.x > 0 ? Mathf.CeilToInt(transform.up.x) : Mathf.FloorToInt(transform.up.x);
                        float yAlt = evadeVSI == 1 ? (evadeYAltitude * evadeVSI) * Random.Range(2f, 3f) : (evadeYAltitude * evadeVSI) / Random.Range(8f, 14f);
                        evadePos   = new Vector3(transform.up.x * 100f, yAlt, transform.position.z);
                        evading    = true;
                        evadeTimer = Time.time + evasionLength;
                        return;
                    }
                }
                else // target's a ship yeet
                {
                    Vector3 dist = (target.position - transform.position).normalized;
                    transform.up = Vector3.MoveTowards(transform.up, dist, 0.04f);

                    if ((target.position - transform.position).magnitude <= shipEscapeRange)
                    {
                        evadeVSI = transform.up.x > 0 ? Mathf.CeilToInt(transform.up.x) : Mathf.FloorToInt(transform.up.x);
                        float yAlt = evadeVSI == 1 ? (evadeYAltitude * evadeVSI) / Random.Range(1.4f, 1.6f) : (evadeYAltitude * evadeVSI) / Random.Range(8f, 14f);
                        evadePos   = new Vector3(transform.up.x * 100f, yAlt, transform.position.z);
                        evading    = true;
                        evadeTimer = Time.time + evasionLength;
                        return;
                    }
                }

                if (targetIsVisual)
                {
                    if (Time.time > cannonTimer && currentShells > 0)
                    {
                        FireCannon();
                    }

                    if (Time.time > rocketTimer && currentRockets > 0)
                    {
                        FireRocket();
                    }
                }
            }
            else
            {
                if (FindTarget())
                {
                    targProps = target.GetComponent <ShipHumanoid>();
                    targRb    = target.GetComponent <Rigidbody2D>();
                    return; // looping back to the start since target was found
                }
                else
                {
                    findTargetTimer = Time.time + targetCheckDelay;
                }

                transform.up = Vector3.MoveTowards(transform.up, new Vector2(transform.up.x, 0f), 0.01f);
            }
        }
    }