Exemplo n.º 1
0
 public void Start()
 {
     owner            = GetComponent <AIMachine>();
     currentState     = IdleState.instance;
     currentStateName = currentState.ToString();
     ChangeState(IdleState.instance);
 }
Exemplo n.º 2
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == FollowState.instance)
        {
            if (!owner.disabled)
            {
                if (RotateBody(owner, owner.playerObject.transform.position))
                {
                    owner.thisAgent.SetDestination(owner.playerObject.transform.position);

                    if (Vector3.Distance(owner.transform.position, owner.playerObject.transform.position) <= owner.followDistance)
                    {
                        owner.thisAgent.SetDestination(owner.transform.position);
                    }
                }

                if (owner.canSeeEnemy && owner.enemyObject != null && Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance)
                {
                    owner.botMachine.ChangeState(AttackState.instance);
                    yield break;
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
Exemplo n.º 3
0
 private void LeaveState(AIMachine _owner)
 {
     if (_owner.botMachine.baseState == SearchState.instance)
     {
         _owner.botMachine.ChangeState(ChaseState.instance);
     }
     else if (_owner.botMachine.baseState == CaptureAreaState.instance)
     {
         _owner.botMachine.ChangeState(ChaseState.instance);
     }
     else if (_owner.botMachine.baseState == FollowState.instance)
     {
         _owner.botMachine.ChangeState(FollowState.instance);
     }
     else if (_owner.botMachine.baseState == PartnerState.instance)
     {
         _owner.botMachine.ChangeState(ChaseState.instance);
     }
     else if (_owner.botMachine.baseState == MoveToState.instance)
     {
         _owner.botMachine.ChangeState(ChaseState.instance);
     }
     else if (_owner.botMachine.baseState == SentryState.instance)
     {
         _owner.botMachine.ChangeState(SentryState.instance);
     }
 }
Exemplo n.º 4
0
    public override IEnumerator InState(AIMachine owner)
    {
        float waitTime    = 2f;
        float elapsedTime = 0f;

        while (owner.botMachine.currentState == SentryState.instance)
        {
            if (!owner.disabled)
            {
                owner.thisAgent.SetDestination(owner.transform.position);

                elapsedTime += Time.deltaTime;

                if (elapsedTime >= waitTime)
                {
                    //once time is greater than wait time the bot is set into sentry

                    if (owner.canSeeEnemy && owner.enemyObject != null)
                    {
                        owner.botMachine.ChangeState(AttackState.instance); //bot will switch into a special attack state that will return to sentry state after the target dies
                        yield break;
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
    private void Awake()
    {
        greenUnit       = GameObject.Find("AISphere Green");
        blueUnit        = GameObject.Find("AISphere Blue");
        orangeUnit      = GameObject.Find("AISphere Orange");
        hubTracker      = GameObject.Find("Persistent Object").GetComponent <HUBTracker>();
        commSubPanel    = GameObject.Find("RunningUI/Commands Group/Command Sub-Panel");
        aiControlGreen  = greenUnit.GetComponent <AIMachine>();
        aiControlBlue   = blueUnit.GetComponent <AIMachine>();
        aiControlOrange = orangeUnit.GetComponent <AIMachine>();

        foreach (Transform button in commSubPanel.GetComponentsInChildren <Transform>())
        {
            cmdButtons.Add(button.gameObject);
        }

        blueSelected   = false;
        greenSelected  = false;
        orangeSelected = false;

        leadTargeted   = false;
        blueTargeted   = false;
        greenTargeted  = false;
        orangeTargeted = false;

        for (int i = 0; i < cmdButtons.Count; i++) //use this for loop to determine what buttons should be deactivated before the level starts
        {
            if (cmdButtons[i].name.Contains("Heal"))
            {
                cmdButtons[i].SetActive(false);
            }
        }
    }
Exemplo n.º 6
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == ChaseState.instance)
        {
            if (!owner.disabled)
            {
                if (owner.enemyObject == null)
                {
                    owner.canSeeEnemy = false;
                    owner.enemyHealth = null;
                    if (owner.botMachine.baseState == SearchState.instance)
                    {
                        owner.botMachine.ChangeState(SearchState.instance);
                    }
                    else if (owner.botMachine.baseState == CaptureAreaState.instance)
                    {
                        owner.botMachine.ChangeState(CaptureAreaState.instance);
                    }
                    else if (owner.botMachine.baseState == PartnerState.instance)
                    {
                        owner.botMachine.ChangeState(PartnerState.instance);
                    }
                    else if (owner.botMachine.baseState == MoveToState.instance)
                    {
                        owner.botMachine.ChangeState(MoveToState.instance);
                    }
                    yield break;
                }

                if (owner.enemyObject != null)
                {
                    owner.thisAgent.SetDestination(owner.transform.position);

                    if (RotateBody(owner, owner.enemyObject.transform.position))
                    {
                        owner.thisAgent.SetDestination(owner.enemyObject.transform.position);

                        if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance && owner.canSeeEnemy)
                        {
                            owner.botMachine.ChangeState(AttackState.instance);
                            yield break;
                        }
                    }

                    if (Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance && owner.canSeeEnemy)
                    {
                        owner.botMachine.ChangeState(AttackState.instance);
                        yield break;
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
    public override IEnumerator InState(AIMachine owner)
    {
        Vector3 destination = owner.transform.position;

        for (int i = 0; i < owner.friendlyBots.Count; i++)
        {
            if (Vector3.Distance(owner.friendlyBots[i].transform.position, owner.transform.position) <= owner.retreatRadius && Vector3.Distance(owner.friendlyBots[i].transform.position, owner.transform.position) >= 50)
            {
                destination = owner.friendlyBots[i].transform.position;
                break;
            }
        }

        owner.logD.RecieveLog(owner.displayName + " is attempting to retreat!"); //generate a log stating that the bot is retreating

        while (owner.botMachine.currentState == RetreatState.instance)           //called from bots in the attack state when they are outnumbered
        {
            if (!owner.disabled)
            {
                if (RotateBody(owner, destination))
                {
                    owner.thisAgent.SetDestination(destination);                        //set the destination for the agent to the destination variable that was generated before entering the while loop

                    Debug.DrawLine(owner.transform.position, destination, Color.green); //draw a debug line that shows the destination the bot should retreat to

                    if (Vector3.Distance(owner.transform.position, destination) <= 5)   //if the distance between the bot and the destination is less than 5, change to the search state
                    {
                        owner.canSeeEnemy = false;
                        owner.enemyObject = null;
                        owner.enemyHealth = null;
                        owner.additionalEnemies.Clear(); //clear the bots list of enemies

                        if (owner.botMachine.baseState == SearchState.instance)
                        {
                            owner.botMachine.ChangeState(SearchState.instance);
                        }
                        else if (owner.botMachine.baseState == CaptureAreaState.instance)
                        {
                            owner.botMachine.ChangeState(CaptureAreaState.instance);
                        }
                        else if (owner.botMachine.baseState == PartnerState.instance)
                        {
                            owner.botMachine.ChangeState(PartnerState.instance);
                        }
                        else if (owner.botMachine.baseState == MoveToState.instance)
                        {
                            owner.botMachine.ChangeState(MoveToState.instance);
                        }
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }
            yield return(null);
        }
    }
Exemplo n.º 8
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == PartnerState.instance)
        {
            if (!owner.disabled)
            {
                if (owner.followTarget != null) //if there is a follow target, follow it
                {
                    if (RotateBody(owner, owner.followTarget.transform.position))
                    {
                        owner.thisAgent.SetDestination(owner.followTarget.transform.position);

                        if (Vector3.Distance(owner.transform.position, owner.followTarget.transform.position) <= owner.followDistance) //if close to the target, stop
                        {
                            owner.thisAgent.SetDestination(owner.transform.position);
                        }
                    }

                    if (!owner.followTarget.activeSelf) //if the follow target is no longer active it is null
                    {
                        owner.followTarget = null;
                        owner.logD.RecieveLog(owner.displayName + "'s leader has died, reverting to idle", owner.gameObject);
                        owner.botMachine.ChangeState(IdleState.instance);
                        yield break;
                    }

                    if (owner.followTarget.GetComponent <AIMachine>().canSeeEnemy&& owner.enemyObject == null) //if follow target has target and this bot does not, set the target to the follow targets target
                    {
                        owner.canSeeEnemy = true;
                        owner.enemyObject = owner.followTarget.GetComponent <AIMachine>().enemyObject;
                        owner.botMachine.ChangeState(ChaseState.instance);
                        yield break;
                    }

                    if (owner.canSeeEnemy && owner.enemyObject != null && Vector3.Distance(owner.transform.position, owner.enemyObject.transform.position) <= owner.attackDistance) //if this bot sees a target, attack it
                    {
                        owner.botMachine.ChangeState(AttackState.instance);
                        yield break;
                    }
                }
                else //if the follow target is null, go to the idle state
                {
                    owner.logD.RecieveLog(owner.displayName + "'s leader has died, reverting to idle", owner.gameObject);
                    owner.botMachine.ChangeState(IdleState.instance);
                    yield break;
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }


            yield return(null);
        }
    }
    public Vector3 GenerateLandingPoint(AIMachine _owner, GameObject targetBot)
    {
        NavMeshHit NH;

        Vector3 point = targetBot.transform.position + Random.insideUnitSphere * 20; //create a point on the navmesh

        NavMesh.SamplePosition(point, out NH, 20, NavMesh.AllAreas);                 //create a position to give to the NH var

        return(NH.position);
    }
Exemplo n.º 10
0
 public void ResetValues()
 {
     Debug.Log("resetting mouse values");
     botSelected = false;
     leadBot     = false;
     blueBot     = false;
     greenBot    = false;
     orangeBot   = false;
     enemyBot    = false;
     selectedBot = null;
     ChangeCursor(5);
 }
Exemplo n.º 11
0
    public override IEnumerator InState(AIMachine owner)
    {
        bool locationGet = false; //bool used to figure out if the bot has a location before navigating to it

        while (owner.botMachine.currentState == MoveToState.instance)
        {
            if (!owner.disabled)
            {
                if (Input.GetKeyDown(KeyCode.Mouse1) && locationGet == false) //if the right mouse button is pressed and the location is not found yet, get a position
                {
                    owner.MarkLocation();
                    locationGet = true;
                }

                if (locationGet == true) //if the location is found, go to it
                {
                    if (RotateBody(owner, owner.marker.transform.position))
                    {
                        owner.thisAgent.SetDestination(owner.marker.transform.position);

                        if (Vector3.Distance(owner.transform.position, owner.marker.transform.position) <= owner.moveToDistance)
                        {
                            owner.RemoveMarker(); //get rid of the marker if it is not needed anymore
                            owner.botMachine.ChangeState(IdleState.instance);
                            yield break;
                        }

                        if (owner.canSeeEnemy && owner.enemyObject != null)
                        {
                            owner.RemoveMarker();
                            owner.botMachine.ChangeState(ChaseState.instance);
                            yield break;
                        }
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }


            yield return(null);
        }
    }
Exemplo n.º 12
0
    void Start()
    {
        stateText = GetComponent <TextMeshProUGUI>();

        //if the text objects name contains the color of the bot, set the text equal to the state of that bot
        if (stateText.gameObject.name.Contains("Blue"))
        {
            botTarget = GameObject.Find("AISphere Blue").GetComponent <AIMachine>();
        }
        if (stateText.gameObject.name.Contains("Green"))
        {
            botTarget = GameObject.Find("AISphere Green").GetComponent <AIMachine>();
        }
        if (stateText.gameObject.name.Contains("Orange"))
        {
            botTarget = GameObject.Find("AISphere Orange").GetComponent <AIMachine>();
        }
    }
Exemplo n.º 13
0
    private bool RotateBody(AIMachine _owner, Vector3 navPoint) //called to have the bot rotate to face the waypoint before navigating to it
    {
        //declare variables that will be used to determine how to move the bot
        Quaternion targetRot = Quaternion.LookRotation(navPoint - _owner.transform.position);

        //rotate the bot over time to face the enemy
        _owner.transform.rotation = Quaternion.RotateTowards(_owner.transform.rotation, targetRot, Time.deltaTime * _owner.bodyRotateSpeed);

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

        if (Vector3.Angle(_owner.transform.forward, navPoint - _owner.transform.position) < 10)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Exemplo n.º 14
0
    private void MoveTurret(AIMachine _owner, Vector3 hitPos) //called to move the bots turret so that it faces whatever Vector3 is sent
    {
        //declare variables that will be used to determine how to move the turret
        Quaternion hitRot    = Quaternion.LookRotation(hitPos - _owner.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 target
        {
            //rotate the turret over time to the target
            _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 target
        _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);
    }
Exemplo n.º 15
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == CaptureAreaState.instance) //needs to navigate to the closest objective rather than any objective
        {
            if (!owner.disabled)
            {
                GameObject targetObj = null;

                foreach (GameObject obj in owner.objectives)
                {
                    targetObj = obj;

                    if (RotateBody(owner, targetObj.transform.position))
                    {
                        owner.thisAgent.SetDestination(targetObj.transform.position);
                        break;
                    }
                }

                if (Vector3.Distance(owner.transform.position, targetObj.transform.position) <= owner.followDistance)
                {
                    owner.thisAgent.SetDestination(owner.transform.position);
                }

                if (owner.canSeeEnemy && owner.enemyObject != null)
                {
                    owner.botMachine.ChangeState(ChaseState.instance); //needs a special chase state so that it will go back to the objective after fighting
                    yield break;
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
Exemplo n.º 16
0
    public override IEnumerator InState(AIMachine owner)
    {
        float waitTime       = 2f;
        float elapsedTime    = 0f;
        float randomRotation = Random.Range(-20, 20);

        while (owner.botMachine.currentState == IdleState.instance)
        {
            if (!owner.disabled)
            {
                owner.thisAgent.SetDestination(owner.transform.position);

                elapsedTime += Time.deltaTime;

                if (elapsedTime >= waitTime)
                {
                    elapsedTime = 0;
                    owner.transform.Rotate(0, randomRotation * Time.deltaTime, 0);
                }

                if (owner.canSeeEnemy && owner.enemyObject != null)
                {
                    owner.botMachine.ChangeState(ChaseState.instance);
                    yield break;
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }



            yield return(null);
        }
    }
Exemplo n.º 17
0
    public override IEnumerator InState(AIMachine owner)
    {
        Waypoint currentWaypoint   = null;
        Waypoint previousWaypoint  = null;
        bool     firstPointVisited = false;
        bool     travelling        = false;

        while (owner.botMachine.currentState == SearchState.instance)
        {
            if (!owner.disabled)
            {
                if (currentWaypoint == null)
                {
                    GameObject[]    allWaypoints   = GameObject.FindGameObjectsWithTag("Waypoint");
                    List <Waypoint> closeWaypoints = new List <Waypoint>();

                    if (allWaypoints.Length > 0)
                    {
                        while (currentWaypoint == null)
                        {
                            foreach (GameObject waypoint in allWaypoints)
                            {
                                if (Vector3.Distance(owner.gameObject.transform.position, waypoint.transform.position) <= 50)
                                {
                                    closeWaypoints.Add(waypoint.GetComponent <Waypoint>());
                                }
                            }

                            int      random           = Random.Range(0, closeWaypoints.Count);
                            Waypoint startingWaypoint = closeWaypoints[random];

                            if (startingWaypoint != null)
                            {
                                currentWaypoint = startingWaypoint;
                            }
                        }
                    }
                }

                if (firstPointVisited == true && travelling == false)
                {
                    Waypoint nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
                    previousWaypoint = currentWaypoint;
                    currentWaypoint  = nextWaypoint;
                }

                owner.targetWaypoint = currentWaypoint.transform.position;

                foreach (GameObject bot in owner.friendlyBots)
                {
                    if (bot != null && !bot.GetComponent <AIAllyMachine>())
                    {
                        var target = bot.GetComponent <AIMachine>().targetWaypoint;

                        if (owner.targetWaypoint == target)
                        {
                            Debug.Log(bot + "has the same target as another bot");
                            Waypoint nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
                            previousWaypoint = currentWaypoint;
                            currentWaypoint  = nextWaypoint;
                        }
                    }
                }

                if (owner.targetWaypoint != null)
                {
                    travelling = true;

                    //check if the bot is already rotated before rotating the body
                    if (Vector3.Angle(owner.transform.forward, owner.targetWaypoint - owner.transform.position) > 10)
                    {
                        if (RotateBody(owner, owner.targetWaypoint)) //as it is, this makes the bot basically do a lookat command. This should stop happening after the bot is rotated
                        {
                            owner.thisAgent.SetDestination(owner.targetWaypoint);

                            Debug.DrawLine(owner.transform.position, owner.targetWaypoint, Color.blue);

                            if (travelling && owner.thisAgent.remainingDistance <= owner.markerDistance)
                            {
                                travelling        = false;
                                firstPointVisited = true;
                            }
                        }
                    }
                    else
                    {
                        owner.thisAgent.SetDestination(owner.targetWaypoint);

                        Debug.DrawLine(owner.transform.position, owner.targetWaypoint, Color.blue);

                        if (travelling && owner.thisAgent.remainingDistance <= owner.markerDistance)
                        {
                            travelling        = false;
                            firstPointVisited = true;
                        }
                    }
                }

                if (owner.canSeeEnemy && owner.enemyObject != null)
                {
                    owner.botMachine.ChangeState(ChaseState.instance);
                    yield break;
                }
                else
                {
                    owner.canSeeEnemy = false;
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
Exemplo n.º 18
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == SupportState.instance)
        {
            if (!owner.disabled)
            {
                GameObject targetBot = null;

                //in this state the bot will locate friendly bots that are not buffed (via a bool) and then launch grenades with an AOE buff to their location
                for (int i = 0; i < owner.friendlyBots.Count; i++)
                {
                    if (!owner.friendlyBots[i].GetComponent <AIMachine>().supportBuff)
                    {
                        targetBot = owner.friendlyBots[i];
                        break;
                    }
                }

                if (targetBot != null)
                {
                    //if not buffed, get in range to buff
                    if (Vector3.Distance(owner.transform.position, targetBot.transform.position) >= owner.healDistance)
                    {
                        if (RotateBody(owner, targetBot.transform.position))
                        {
                            owner.thisAgent.SetDestination(targetBot.transform.position);
                        }
                    }
                    else
                    {
                        MoveTurret(owner, targetBot);

                        if (owner.onTarget)
                        {
                            owner.thisAgent.SetDestination(owner.transform.position);
                            //once in range to buff, launch grenade (grenade will be a prefab object)
                            var grenadeCopy = GameObject.Instantiate(owner.grenadePrefab as GameObject, owner.transform);
                            grenadeCopy.SetActive(true);
                            //generate a sphere around the target bot and chose a random location within it for the grenade to impact
                            Vector3 landArea = GenerateLandingPoint(owner, targetBot);

                            //have the grenade travel over time to the destination. Look at https://lunarlabs.pt/blog/post/the_art_of_lerp for help (get code working then impliment lerp after)
                            yield return(new WaitForSeconds(2.0f));

                            grenadeCopy.transform.position = landArea;
                            //once the grenade impacts, generate another sphere around the impact location that will be the AOE buff zone
                            //instantiate another object that will be the buff area and have a script on it to handle buffing
                            var areaPrefab = GameObject.Instantiate(owner.areaHealPrefab as GameObject, grenadeCopy.transform);
                            areaPrefab.transform.parent = grenadeCopy.transform.parent.parent;
                            areaPrefab.GetComponent <AreaBuff>().grenadeObject = grenadeCopy;
                        }

                        owner.botMachine.ChangeState(IdleState.instance);
                    }
                }
                else
                {
                    owner.thisAgent.SetDestination(owner.transform.position);
                }

                yield return(null);
            }
        }
    }
Exemplo n.º 19
0
    public void SetStatBonuses()
    {
        aiMachine = GetComponent <AIMachine>();
        agent     = GetComponent <NavMeshAgent>();

        if (!firstStatsSet)
        {
            health.HealthUpdate(strength, true);
        }
        else
        {
            health.HealthUpdate(strength, false);
        }

        if (firstStatsSet == false)
        {
            agentSpeed      = agent.speed;
            agentAcc        = agent.acceleration;
            agentAngSpd     = agent.angularSpeed;
            hitChance       = aiMachine.hitChance;
            searchRadius    = aiMachine.searchRadius;
            turretRotateSpd = aiMachine.turretRotateSpeed;
            barrelMoveSpd   = aiMachine.barrelMoveSpeed;
            fireRate        = aiMachine.fireRate;
            heavyHitChance  = aiMachine.heavyHitChance;
            heavyHitMod     = aiMachine.heavyHitModifier;
        }

        agent.speed        = agility + agentSpeed;
        agent.acceleration = agility + agentAcc;
        agent.angularSpeed = agility + agentAngSpd;

        aiMachine.hitChance  = combat + hitChance;
        aiMachine.critChance = combat + critChance;

        aiMachine.searchRadius = scanning + searchRadius;

        aiMachine.turretRotateSpeed = (efficiency * 2) + turretRotateSpd;
        aiMachine.barrelMoveSpeed   = (efficiency * 2) + barrelMoveSpd;
        aiMachine.fireRate          = fireRate - (efficiency / 50); //may not work correctly

        aiMachine.heavyHitChance = heavyHitter + heavyHitChance;

        int hHMod = 0;

        for (int i = 0; i < heavyHitter; i += 3)
        {
            hHMod += 1;
        }
        aiMachine.heavyHitModifier = hHMod + heavyHitMod;

        firstStatsSet = true;

        GetComponent <DefenderStat>().UpdateProcChance(defender);

        if (regenerator >= 1)
        {
            health.autoHeal = true;
        }

        if (teamPlayer >= 1)
        {
            tpStatObject.EnableStat(teamPlayer);
        }

        if (informant >= 1)
        {
            infoStatObject.SetRadius(informant);
        }
    }
Exemplo n.º 20
0
    public override IEnumerator InState(AIMachine owner)
    {
        while (owner.botMachine.currentState == HealState.instance)
        {
            if (!owner.disabled)
            {
                for (int i = 0; i < owner.friendlyBots.Count; i++)                                                                      //for each friendly bot
                {
                    if (owner.friendlyBots[i].GetComponent <Health>().health < owner.friendlyBots[i].GetComponent <Health>().maxHealth) //if the current friendly bots HP is less than max
                    {
                        if (RotateBody(owner, owner.friendlyBots[i].transform.position))
                        {
                            owner.thisAgent.SetDestination(owner.friendlyBots[i].transform.position);                                       //move to the current friendly bot

                            if (Vector3.Distance(owner.transform.position, owner.friendlyBots[i].transform.position) <= owner.healDistance) //if the distance between this bot and the friendly bot is less than the heal distance
                            {
                                owner.thisAgent.SetDestination(owner.transform.position);                                                   //set the destination to itself so that it stops
                                Debug.Log("In position to heal");

                                if (owner.friendlyBots[i].activeSelf) //if the bot is still active when the heal bot is in range, begin healing
                                {
                                    owner.laserLine.SetPosition(0, owner.transform.position);
                                    owner.laserLine.SetPosition(1, owner.friendlyBots[i].transform.position);
                                    owner.laserLine.gameObject.GetComponent <Renderer>().material.color = Color.green;

                                    do
                                    {
                                        Debug.Log("Healing");
                                        owner.laserLine.enabled = true;
                                        owner.audioS.PlayOneShot(owner.GetComponent <MultipleAudioClips>().clips[2]);
                                        yield return(new WaitForSeconds(owner.healRate));

                                        owner.laserLine.enabled = false;

                                        owner.friendlyBots[i].GetComponent <Health>().HealthIncrease();
                                        owner.logD.RecieveLog(owner.displayName + ": " + owner.friendlyBots[i].GetComponent <AIMachine>().displayName + " was healed by 1 point", owner.gameObject);
                                    }while (owner.friendlyBots[i].GetComponent <Health>().health < owner.friendlyBots[i].GetComponent <Health>().maxHealth&& owner.friendlyBots[i].activeSelf);
                                }
                            }
                        }
                    }
                }

                if (owner.playerObject.GetComponent <Health>().health < owner.playerObject.GetComponent <Health>().maxHealth) //do the same as above but for the player bot
                {
                    if (RotateBody(owner, owner.playerObject.transform.position))
                    {
                        owner.thisAgent.SetDestination(owner.playerObject.transform.position);

                        if (Vector3.Distance(owner.transform.position, owner.playerObject.transform.position) <= owner.healDistance)
                        {
                            owner.thisAgent.SetDestination(owner.transform.position);
                            Debug.Log("In position to heal");
                            owner.thisAgent.transform.LookAt(owner.playerObject.transform);

                            if (owner.playerObject.activeSelf) //if the player object is still active, begin healing
                            {
                                owner.laserLine.SetPosition(0, owner.transform.position);
                                owner.laserLine.SetPosition(1, owner.playerObject.transform.position);
                                owner.laserLine.gameObject.GetComponent <Renderer>().material.color = Color.green;

                                do
                                {
                                    Debug.Log("Healing");
                                    owner.laserLine.enabled = true;
                                    owner.audioS.PlayOneShot(owner.GetComponent <MultipleAudioClips>().clips[2]);
                                    yield return(new WaitForSeconds(owner.healRate));

                                    owner.laserLine.enabled = false;

                                    owner.playerObject.GetComponent <Health>().HealthIncrease();
                                    owner.logD.RecieveLog(owner.displayName + ": " + owner.playerObject.name + " was healed by 1 point", owner.gameObject);
                                }while (owner.playerObject.GetComponent <Health>().health < owner.playerObject.GetComponent <Health>().maxHealth&& owner.playerObject.activeSelf);
                            }
                        }
                    }
                }

                Debug.Log("Healing cycle complete");
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }



            yield return(null);
        }
    }
Exemplo n.º 21
0
 public abstract IEnumerator InState(AIMachine owner);
Exemplo n.º 22
0
    private void Shoot(AIMachine _owner)
    {
        int randomHitChance = Random.Range(0, 100); //use a random int to determine if the shot will land

        if (randomHitChance <= _owner.hitChance && _owner.enemyObject != null)
        {
            int randomCritChance     = Random.Range(0, 100);
            int randomHeavyHitChance = Random.Range(0, 100);

            _owner.enemyObject.GetComponent <AudioSource>().PlayOneShot(_owner.enemyObject.GetComponent <MultipleAudioClips>().clips[0]); //play the hit sound on the enemy bot

            if (randomCritChance <= _owner.critChance)                                                                                    //use a second random int to determine if the shot will be a critical hit
            {
                int damageAmt = 2;

                if (randomHeavyHitChance <= _owner.heavyHitChance)
                {
                    damageAmt += _owner.heavyHitModifier;
                }

                _owner.enemyHealth.HealthReduce(damageAmt);

                if (_owner.enemyObject.GetComponent <StructureInfo>()) //check if the enemy is a structure before giving the log
                {
                    _owner.logD.RecieveLog(_owner.displayName + ": " + _owner.enemyObject.GetComponent <StructureInfo>().displayName + " takes " + damageAmt + " damage, CRITICAL!!!", _owner.gameObject);
                }
                else
                {
                    _owner.logD.RecieveLog(_owner.displayName + ": " + _owner.enemyObject.GetComponent <EnemyAIMachine>().displayName + " takes " + damageAmt + " damage, CRITICAL!!!", _owner.gameObject);
                }
            }
            else
            {
                int damageAmt = 1;

                if (randomHeavyHitChance <= _owner.heavyHitChance)
                {
                    damageAmt += _owner.heavyHitModifier;
                }

                _owner.enemyHealth.HealthReduce(damageAmt);

                if (_owner.enemyObject.GetComponent <StructureInfo>())
                {
                    _owner.logD.RecieveLog(_owner.displayName + ": " + _owner.enemyObject.GetComponent <StructureInfo>().displayName + " takes " + damageAmt + " damage", _owner.gameObject);
                }
                else
                {
                    _owner.logD.RecieveLog(_owner.displayName + ": " + _owner.enemyObject.GetComponent <EnemyAIMachine>().displayName + " takes " + damageAmt + " damage", _owner.gameObject);
                }
            }

            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;
                }
            }
        }
        else
        {
            _owner.logD.RecieveLog(_owner.displayName + " missed.");
        }
    }
Exemplo n.º 23
0
    public override IEnumerator InState(AIMachine owner)
    {
        float timeWaitingToShoot  = 0;
        bool  movingCloserToEnemy = false;

        while (owner.botMachine.currentState == AttackState.instance)
        {
            if (!owner.disabled)
            {
                if (owner.enemyObject == null)
                {
                    owner.canSeeEnemy = false;
                    owner.enemyHealth = null;

                    LeaveState(owner);
                    yield break;
                }
                else
                {
                    //before firing check if there are too many enemies and if the bot should change to the retreat state
                    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.botMachine.ChangeState(RetreatState.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 <AIMachine>().enemyObject == null) //if the current bot doesnt already have a target, set it
                            {
                                owner.friendlyBots[i].GetComponent <AIMachine>().enemyObject = owner.enemyObject;
                                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, owner.enemyObject.GetComponentInChildren <HighlightObject>().gameObject.transform.position);

                        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.thisAgent.SetDestination(owner.transform.position);
                                movingCloserToEnemy = false;
                            }
                        }

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

                            owner.laserLine.SetPosition(0, owner.barrelEnd.position);
                            owner.laserLine.SetPosition(1, owner.contactPoint);

                            owner.laserLine.enabled = true;
                            owner.audioS.PlayOneShot(owner.GetComponent <MultipleAudioClips>().clips[1]);
                            yield return(new WaitForSeconds(owner.fireRate));

                            owner.laserLine.enabled = false;

                            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 <AIMachine>().removingEnemy == true)
                                {
                                    removing = true;
                                    break;
                                }
                            }

                            if (removing == false)
                            {
                                if (owner.enemyObject != null)
                                {
                                    if (owner.enemyHealth.health <= 0)
                                    {
                                        owner.removingEnemy = true;
                                        OnEnemyDeath(owner);
                                    }
                                }
                            }
                        }
                    }
                    else //if the enemy is out of attack range, chase after them
                    {
                        LeaveState(owner);
                        yield break;
                    }
                }
            }
            else
            {
                owner.thisAgent.SetDestination(owner.transform.position);
            }

            yield return(null);
        }
    }
Exemplo n.º 24
0
    private void OnEnemyDeath(AIMachine _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);
            _owner.botStats.KillExpIncrease(tempEnemyHolder);
        }
        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);
            _owner.botStats.KillExpIncrease(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.enemyObject = null;
                    targetBot.enemyHealth = null;
                    targetBot.canSeeEnemy = false;

                    var currentState = targetBot.botMachine.baseState;

                    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.enemyObject = null;
                    targetAlly.enemyHealth = null;
                    targetAlly.canSeeEnemy = false;

                    var currentState = targetAlly.botMachine.currentState;

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

        _owner.removingEnemy = false;
    }