UnityGameProjectsCode / RelianceGame / AI Control / Ally Scripts / AllyAttackState.cs
AllyAttackState.cs
Raw
using AllyAIMachineTools;
using System;
using System.Collections;
using UnityEngine;

public class AllyAttackState : AllyState
{
    private static AllyAttackState _instance; //only declared once

    private AllyAttackState() //set instance to itself
    {
        if (_instance != null)
        {
            return;
        }

        _instance = this;
    }

    public static AllyAttackState instance //creates the instance of the first state if it does not exist
    {
        get
        {
            if (_instance == null)
            {
                new AllyAttackState();
            }

            return _instance;
        }
    }

    public override IEnumerator InState(AIAllyMachine owner)
    {
        float timeWaitingToShoot = 0;
        float timeSinceFiring = 0;
        bool shotFired = false;
        bool movingCloserToEnemy = false;

        while (owner.botMachine.currentState == AllyAttackState.instance)
        {
            if (!owner.disabled)
            {
                if (owner.enemyObject == null)
                {
                    owner.canSeeEnemy = false;
                    owner.enemyHealth = null;
                    owner.laserLine.enabled = false;
                    owner.barrelLight.enabled = false;

                    owner.botMachine.ChangeState(AllyChaseState.instance); //go to a retreat state
                    yield break;
                }
                else
                {
                    if (shotFired == true)
                    {
                        timeSinceFiring += Time.deltaTime;

                        if (!owner.animator.GetCurrentAnimatorStateInfo(0).IsName("BarrelLightFade"))
                        {
                            owner.laserLine.enabled = false;
                            owner.barrelLight.enabled = false;
                        }

                        if (timeSinceFiring >= owner.fireRate && shotFired == true)
                        {
                            timeSinceFiring = 0.0f;
                            shotFired = false;
                        }
                    }

                    //before firing check if there are too many enemies and if there are any nearby allies to decide whether to retreat or not
                    if (owner.additionalEnemies.Count >= 2 && owner.nearbyFriendlies.Count <= 1)
                    {
                        for (int i = 0; i < owner.friendlyBots.Count; i++) //for all moving bots, check if any of them are close enough to retreat to
                        {
                            if (Vector3.Distance(owner.friendlyBots[i].transform.position, owner.transform.position) <= owner.retreatRadius && Vector3.Distance(owner.friendlyBots[i].transform.position, owner.transform.position) >= 50)
                            {
                                owner.laserLine.enabled = false;
                                owner.barrelLight.enabled = false;

                                owner.botMachine.ChangeState(AllyRetreatState.instance);
                                yield break;
                            }
                        }
                    }

                    int numberOfBotsCalled = 0;

                    for (int i = 0; i < owner.friendlyBots.Count; i++) //if there are any friendly bots within the reinforcementRadius, give them this bots target
                    {
                        if (Vector3.Distance(owner.transform.position, owner.friendlyBots[i].transform.position) <= owner.reinforcementRadius)
                        {
                            if (owner.friendlyBots[i].GetComponent<AIAllyMachine>().enemyObject == null) //if the current bot doesnt already have a target, set it
                            {
                                owner.friendlyBots[i].GetComponent<AIAllyMachine>().enemyObject = owner.enemyObject;
                                owner.friendlyBots[i].GetComponent<AIAllyMachine>().canSeeEnemy = true;
                                numberOfBotsCalled++;
                            }
                        }
                    }

                    if (numberOfBotsCalled != 0)
                    {
                        owner.logD.RecieveLog(owner.displayName + " called " + numberOfBotsCalled + " bot(s) for help!");
                        numberOfBotsCalled = 0;
                    }

                    if (movingCloserToEnemy == false)
                        owner.thisAgent.SetDestination(owner.transform.position);

                    if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance && owner.canSeeEnemy)
                    {
                        MoveTurret(owner);

                        timeWaitingToShoot += Time.deltaTime;

                        if (timeWaitingToShoot >= 5f)
                        {
                            owner.thisAgent.SetDestination(owner.enemyObject.transform.position);
                            movingCloserToEnemy = true;

                            if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= 20 || owner.onTarget)
                            {
                                owner.thisAgent.SetDestination(owner.transform.position);
                                movingCloserToEnemy = false;
                            }
                        }

                        if (owner.onTarget == true)
                        {
                            timeWaitingToShoot = 0;

                            if (!shotFired)
                            {
                                timeSinceFiring = 0.0f;
                                shotFired = true;
                                Shoot(owner);
                            }

                            bool removing = false;

                            for (int i = 0; i < owner.friendlyBots.Count; i++) //if any of the bots are already removing an enemy, dont do anything
                            {
                                if (owner.friendlyBots[i].GetComponent<AIAllyMachine>().removingEnemy == true)
                                {
                                    removing = true;
                                    break;
                                }
                            }

                            if (removing == false)
                            {
                                try
                                {
                                    if (owner.enemyHealth.health <= 0)
                                    {
                                        owner.removingEnemy = true;
                                        OnEnemyDeath(owner);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Debug.LogError(e.StackTrace);
                                }
                            }
                        }
                    }
                    else
                    {
                        owner.laserLine.enabled = false;
                        owner.barrelLight.enabled = false;

                        owner.botMachine.ChangeState(AllyChaseState.instance); //go to a retreat state
                        yield break;
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return null;
        }
    }

    private void Shoot(AIAllyMachine _owner)
    {
        _owner.soundMgr.StartMusicCouroutine();

        int random = UnityEngine.Random.Range(0, 100); //use a random int to determine if the shot will land

        float pitchVariance = UnityEngine.Random.Range(0.80f, 1.20f);
        _owner.audioS.pitch = pitchVariance;
        _owner.audioS.PlayOneShot(_owner.GetComponent<MultipleAudioClips>().clips[1]);

        int randomLaserMat = UnityEngine.Random.Range(0, 5);
        _owner.laserLine.material = _owner.lasers[randomLaserMat];
        _owner.laserLine.SetPosition(0, _owner.barrelEnd.position);
        _owner.laserLine.SetPosition(1, _owner.contactPoint);
        _owner.laserLine.enabled = true;
        _owner.barrelLight.enabled = true;

        _owner.animator.Play("BarrelLightFade", 0);
        _owner.animator.Play("BarrelMove", 2);
        _owner.animator.Play("LaserFade", 1);
        _owner.laserParticles.Play();

        if (random <= _owner.hitChance && _owner.enemyObject != null)
        {
            _owner.CreateImpactParticles();

            int randomTwo = UnityEngine.Random.Range(0, 100);
            _owner.enemyObject.GetComponent<AudioSource>().PlayOneShot(_owner.enemyObject.GetComponent<MultipleAudioClips>().clips[0]); //play the hit sound on the enemy bot

            if (randomTwo <= _owner.critChance) //use a second random int to determine if the shot will be a critical hit
            {
                try
                {
                    _owner.enemyHealth.HealthReduce(2);
                }
                catch (Exception e)
                {
                    Debug.LogError(e.StackTrace);
                }
            }
            else
            {
                try
                {
                    _owner.enemyHealth.HealthReduce();
                }
                catch (Exception e)
                {
                    Debug.LogError(e.StackTrace);
                }
            }

            if (!_owner.enemyObject.GetComponent<StructureInfo>()) //only execute if the enemy is not a structure
            {
                if (_owner.enemyObject.GetComponent<EnemyAIMachine>().enemyObject == null) //if the enemy you are firing at does not have a target, set its target to you
                {
                    _owner.enemyObject.GetComponent<EnemyAIMachine>().enemyObject = _owner.gameObject; //set the enemies enemyObject to this bot so that it will fight back
                    _owner.enemyObject.GetComponent<EnemyAIMachine>().canSeeEnemy = true;
                }
            }

            _owner.enemyObject.GetComponent<AudioSource>().PlayOneShot(_owner.enemyObject.GetComponent<MultipleAudioClips>().clips[0]); //play the hit sound on the enemy bot
        }
        else
        {
            _owner.logD.RecieveLog(_owner.displayName + " missed.");
        }
    }

    private void OnEnemyDeath(AIAllyMachine _owner)
    {
        var tempEnemyHolder = _owner.enemyObject; //create a temp var to hold the enemy object so that it can be removed from this bot while still holding the reference

        _owner.enemyObject = null;
        _owner.canSeeEnemy = false;
        _owner.enemyHealth = null;

        if (tempEnemyHolder.GetComponent<StructureInfo>()) //if the enemy destroyed was a structure
        {
            _owner.logD.RecieveLog(tempEnemyHolder.GetComponent<StructureInfo>().displayName + " was destroyed", Color.red);
            tempEnemyHolder.SetActive(false);
        }
        else //if the enemy destroyed was a bot
        {
            _owner.logD.RecieveLog(tempEnemyHolder.GetComponent<EnemyAIMachine>().displayName + " was destroyed", Color.red);
            tempEnemyHolder.SetActive(false);
            _owner.gameManager.OnKill(tempEnemyHolder);
        }

        for (int i = 0; i < _owner.friendlyBots.Count; i++) //after the enemy is dead, check other friendly bots for the same target, remove it, then send them to an appropriate state
        {
            AIMachine targetBot = null;
            AIAllyMachine targetAlly = null;

            if (_owner.friendlyBots[i].GetComponent<BotStats>()) //if the bot is in the list but doesnt have botstats, it is an ally bot
                targetBot = _owner.friendlyBots[i].GetComponent<AIMachine>();
            else
                targetAlly = _owner.friendlyBots[i].GetComponent<AIAllyMachine>();

            if (targetAlly == null)
            {
                if (targetBot.enemyObject == tempEnemyHolder && targetBot.gameObject.activeSelf == true)
                {
                    targetBot.enemyObject = null;
                    targetBot.enemyHealth = null;
                    targetBot.canSeeEnemy = false;
                    targetBot.laserLine.enabled = false;
                    targetBot.barrelLight.enabled = false;

                    var currentState = targetBot.botMachine.currentState;

                    if (currentState == SearchState.instance)
                        targetBot.botMachine.ChangeState(ChaseState.instance);
                    else if (currentState == CaptureAreaState.instance)
                        targetBot.botMachine.ChangeState(ChaseState.instance);
                    else if (currentState == FollowState.instance)
                        targetBot.botMachine.ChangeState(FollowState.instance);
                    else if (currentState == PartnerState.instance)
                        targetBot.botMachine.ChangeState(ChaseState.instance);
                    else if (currentState == MoveToState.instance)
                        targetBot.botMachine.ChangeState(ChaseState.instance);
                    else if (currentState == SentryState.instance)
                        targetBot.botMachine.ChangeState(SentryState.instance);
                }
            }
            else
            {
                if (targetAlly.enemyObject == tempEnemyHolder && targetAlly.gameObject.activeSelf == true)
                {
                    targetAlly.enemyObject = null;
                    targetAlly.enemyHealth = null;
                    targetAlly.canSeeEnemy = false;
                    targetAlly.laserLine.enabled = false;
                    targetAlly.barrelLight.enabled = false;

                    var currentState = targetAlly.botMachine.currentState;

                    if (currentState != AllyRetreatState.instance)
                        targetAlly.botMachine.ChangeState(AllyChaseState.instance);
                }
            }
        }

        GameObject playerEnemy = _owner.playerObject.GetComponent<PlayerCallForHelp>().GetEnemy();

        if (playerEnemy == tempEnemyHolder)
            _owner.playerObject.GetComponent<PlayerCallForHelp>().SetEnemyNull();

        _owner.removingEnemy = false;
    }

    private void MoveTurret(AIAllyMachine _owner) //called to move the bots turret so that it faces the target
    {
        //declare variables that will be used to determine how to move the turret
        Vector3 hitPos = _owner.enemyObject.GetComponentInChildren<HighlightObject>().gameObject.transform.position;
        Quaternion turretRot = Quaternion.LookRotation(hitPos - _owner.turretPos.transform.position);

        if (Vector3.Angle(_owner.turretPos.transform.forward, _owner.enemyObject.transform.position - _owner.turretPos.transform.position) > 5) //rotate the turret until it is facing within 5 degrees of the enemy
        {
            //rotate the turret over time to the enemy
            _owner.turretPos.transform.rotation = Quaternion.RotateTowards(_owner.turretPos.transform.rotation, turretRot, Time.deltaTime * _owner.turretRotateSpeed);

            //set the turrets y euler angle so that it only moves on that transform
            _owner.turretPos.transform.localEulerAngles = new Vector3(0, _owner.turretPos.transform.localEulerAngles.y, 0);
        }

        //rotate the barrel over time to the enemy
        _owner.barrelPos.transform.rotation = Quaternion.RotateTowards(_owner.barrelPos.transform.rotation, turretRot, Time.deltaTime * _owner.barrelMoveSpeed);

        //set the barrels y euler angle so that it only moves on that transform
        _owner.barrelPos.transform.localEulerAngles = new Vector3(0, _owner.barrelPos.transform.localEulerAngles.y, 0);
    }
}