protected override IEnumerator Execute()
    {
        SetState(NodeState.RUNNING);

        // Fail action if item is out of reach
        if (!senses.IsItemInReach(itemToCollect))
        {
            SetState(NodeState.FAILURE);
            yield break;
        }

        actions.CollectItem(itemToCollect);

        // Check if agent acidentally thinks he picked up item but is not actually in his inventory
        if (!inventory.HasItem(itemToCollect.name.ToString()))
        {
            SetState(NodeState.FAILURE);
            yield break;
        }

        PlayerCache.SetFlagCarriers(itemToCollect);

        SetState(NodeState.SUCCESS);

        yield return(null);
    }
Exemple #2
0
    // a function that allows the ai to pick up a flag if they are close enough and see it within view
    public bool PickUpFlag(Sensing sight, AgentActions actions, AgentData data)
    {
        List <GameObject> temp = new List <GameObject>();

        foreach (GameObject g in sight.GetObjectsInViewByTag("Flag"))
        {
            temp.Add(g);
        }

        for (int i = 0; i < temp.Count; ++i)
        {
            if (temp[i].name == "Red Flag") //checks to make sure the item within range is the red flag
            {
                if (data.FriendlyTeamTag == Tags.BlueTeam)
                {
                    //when the Ai is within view distance
                    actions.MoveTo(temp[i]);
                    // then it collects the item
                    actions.CollectItem(temp[i]);
                }
            }
            else if (temp[i].name == "Blue Flag") //checks to make sure the item within range is the blue flag
            {
                if (data.FriendlyTeamTag == Tags.RedTeam)
                {
                    //when the Ai is within view distance
                    actions.MoveTo(temp[i]);
                    // then it collects the item
                    actions.CollectItem(temp[i]);
                }
            }
        }

        if (data.HasEnemyFlag)
        {
            return(true);
        }



        return(false);
    }
Exemple #3
0
    // a function that gets the ai to try and get thier own flag back and return it to thier base
    public bool RetrieveFlag(Sensing sight, AgentActions actions, AgentData data, AI theai)
    {
        List <GameObject> Temp = new List <GameObject>();

        foreach (GameObject G in sight.GetObjectsInViewByTag("Flag"))
        {
            Temp.Add(G);
        }

        for (int i = 0; i < Temp.Count; ++i)
        {
            if (Temp[i].name == "Red Flag") //checks to make sure the item within range is the red flag
            {
                if (data.FriendlyTeamTag == "Red Team")
                {
                    //when the Ai is within view distance
                    actions.MoveTo(Temp[i]);
                    // then it collects the item
                    actions.CollectItem(Temp[i]);
                }
            }
            else if (Temp[i].name == "Blue Flag") //checks to make sure the item within range is the blue flag
            {
                if (data.FriendlyTeamTag == "Blue Team")
                {
                    //when the Ai is within view distance
                    actions.MoveTo(Temp[i]);
                    // then it collects the item
                    actions.CollectItem(Temp[i]);
                }
            }
        }

        if (data.HasFriendlyFlag)
        {
            theai.GotFriendlyFlag = true;
        }

        return(true);
    }
Exemple #4
0
    private void Checks()
    {
        if (_agentInventory.HasItem(Names.HealthKit))
        {
            if (_agentData.CurrentHitPoints <= 50)
            {
                _agentActions.UseItem(_agentInventory.GetItem(Names.HealthKit));
            }
        }

        GameObject ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.HealthKit);

        if (ObjectToCheck != null)
        {
            if (!_agentInventory.HasItem(Names.HealthKit))
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    _agentActions.CollectItem(ObjectToCheck);
                    Simulation((int)nodeOptions);
                }
                else if (Vector3.Distance(transform.position, ObjectToCheck.transform.position) < 7.0f)
                {
                    _agentActions.MoveTo(ObjectToCheck);
                }
            }
        }

        ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.PowerUp);
        if (ObjectToCheck != null)
        {
            if (!_agentInventory.HasItem(Names.PowerUp))
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    _agentActions.CollectItem(ObjectToCheck);
                    Simulation((int)nodeOptions);
                }
                else if (Vector3.Distance(transform.position, ObjectToCheck.transform.position) < 7.0f)
                {
                    _agentActions.MoveTo(ObjectToCheck);
                }
            }
        }

        if (this.tag == Tags.BlueTeam)
        {
            ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.RedFlag);

            if (ObjectToCheck != null)
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    _agentActions.CollectItem(ObjectToCheck);
                    Simulation((int)nodeOptions);
                }
            }

            ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.BlueFlag);

            if (ObjectToCheck != null)
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    ObjectToCheck.GetComponent <Flag>().ResetPositionBlue();
                    Simulation((int)nodeOptions);
                }
            }

            if (_agentData.HasEnemyFlag)
            {
                if (Vector3.Distance(transform.position, _agentData.FriendlyBase.transform.position) <= 5.0f)
                {
                    _agentActions.DropItem(_agentInventory.GetItem(Names.RedFlag));
                    hasWon = true;
                    ResetPosition();
                    hasWon      = false;
                    currentNode = rootNode;
                    Debug.Log("win" + this.gameObject);
                }
            }
        }

        if (this.tag == Tags.RedTeam)
        {
            ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.BlueFlag);

            if (ObjectToCheck != null)
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    _agentActions.CollectItem(ObjectToCheck);
                    Simulation((int)nodeOptions);
                }
            }

            ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.RedFlag);

            if (ObjectToCheck != null)
            {
                if (_agentSenses.IsItemInReach(ObjectToCheck))
                {
                    ObjectToCheck.GetComponent <Flag>().ResetPositionRed();
                    Simulation((int)nodeOptions);
                }
            }

            if (_agentData.HasEnemyFlag)
            {
                if (Vector3.Distance(transform.position, _agentData.FriendlyBase.transform.position) <= 5.0f)
                {
                    _agentActions.DropItem(_agentInventory.GetItem(Names.BlueFlag));
                    hasWon = true;
                    ResetPosition();
                    hasWon = false;
                    Debug.Log("win" + this.gameObject);
                }
            }
        }
    }