コード例 #1
0
ファイル: AI_Entity.cs プロジェクト: skoam/fumiko-ai
    bool check_for_contact(AI_Settings settings)
    {
        if (off)
        {
            return(false);
        }

        if (deathController != null && deathController.hasDied())
        {
            return(false);
        }

        if (contact == AI_Contact.RANDOM_FROM_TEAM)
        {
            if (contactAIController != null && !contactAIController.off && contactAIController.deathController != null &&
                contactAIController.deathController.hasDied())
            {
                updateContact();
            }
            else if (contactRepresentation == null)
            {
                updateContact();
            }
        }

        if (contact == AI_Contact.NEAREST_FROM_TEAM)
        {
            if (contactAIController != null && !contactAIController.off && contactAIController.deathController != null &&
                contactAIController.deathController.hasDied())
            {
                updateContact();
            }
            else if (contactRepresentation == null)
            {
                updateContact();
            }
        }

        if (contactRepresentation == null)
        {
            return(false);
        }

        if (failRandomly)
        {
            if (Random.Range(0, 100) < failChance)
            {
                currentFailLength = failLength;
                return(false);
            }
        }

        if (settings.condition == AI_Condition.ALWAYS)
        {
            return(true);
        }

        if (settings.condition == AI_Condition.IN_RANGE ||
            settings.condition == AI_Condition.OUT_OF_RANGE)
        {
            // get all objects in the nearest area
            hits = Physics.OverlapSphere(this.transform.position, settings.distance);

            for (int i = 0; i < hits.Length; i++)
            {
                if (contact == AI_Contact.PLAYER)
                {
                    if (hits[i].gameObject.tag == "Player" && !foundContact)
                    {
                        // Has detected contact
                        if (hits[i].gameObject.GetComponent <CanDie>().hasDied())
                        {
                            return(false);
                        }

                        if (settings.condition == AI_Condition.IN_RANGE)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else if (contact == AI_Contact.PREDEFINED || contact == AI_Contact.TAG ||
                         contact == AI_Contact.RANDOM_FROM_TEAM || contact == AI_Contact.NEAREST_FROM_TEAM)
                {
                    if (hits[i].gameObject == contactRepresentation)
                    {
                        if (hits[i].gameObject.GetComponent <CanDie>() != null && hits[i].gameObject.GetComponent <CanDie>().hasDied())
                        {
                            return(false);
                        }

                        if (settings.condition == AI_Condition.IN_RANGE)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            if (settings.condition == AI_Condition.OUT_OF_RANGE)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }

        if (settings.condition == AI_Condition.BELOW)
        {
            if (this.transform.position.y - contactRepresentation.transform.position.y < settings.distance)
            {
                return(true);
            }
        }

        if (settings.condition == AI_Condition.AT_HOME)
        {
            if (Vector3.Distance(this.transform.position, home) < settings.distance)
            {
                return(true);
            }
        }


        if (settings.condition == AI_Condition.AWAY_FROM_HOME)
        {
            if (Vector3.Distance(this.transform.position, home) > settings.distance)
            {
                return(true);
            }
        }

        // could not find condition
        return(false);
    }
コード例 #2
0
ファイル: AI_Entity.cs プロジェクト: skoam/fumiko-ai
    void executeBehaviors()
    {
        for (int p = 0; p < maximumPriority; p++)
        {
            for (int i = 0; i < behaviors.Length; i++)
            {
                behavior = behaviors[i];

                if (behavior.type == AI_Behavior.FOLLOW_PLAYER)
                {
                    AI_Controller_FollowPlayer controller = this.gameObject.GetComponent <AI_Controller_FollowPlayer>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_FollowPlayer>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    if (ManagesGame.getInstance().noHostileAI)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);
                    controller.contact       = contactRepresentation;
                    controller.movementSpeed = behavior.movementSpeed;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.RETURN)
                {
                    AI_Controller_Return controller = this.gameObject.GetComponent <AI_Controller_Return>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_Return>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check                    = check_for_contact(behavior);
                    controller.home          = home;
                    controller.movementSpeed = behavior.movementSpeed;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.MOVE_RANDOMLY)
                {
                    AI_Controller_RandomMovement controller = this.gameObject.GetComponent <AI_Controller_RandomMovement>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_RandomMovement>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);
                    controller.movementSpeed = behavior.movementSpeed;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.PUSH_PLAYER)
                {
                    AI_Controller_PushPlayer controller = this.gameObject.GetComponent <AI_Controller_PushPlayer>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_PushPlayer>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    if (ManagesGame.getInstance().noHostileAI)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);
                    controller.contact       = contactRepresentation;
                    controller.movementSpeed = behavior.movementSpeed;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.ACTIVATE_OBJECT)
                {
                    AI_Controller_ActivateGameObject controller = this.gameObject.GetComponent <AI_Controller_ActivateGameObject>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_ActivateGameObject>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.DASH)
                {
                    AI_Controller_Dash controller = this.gameObject.GetComponent <AI_Controller_Dash>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_Dash>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.MULTIJUMP)
                {
                    AI_Controller_Multijump controller = this.gameObject.GetComponent <AI_Controller_Multijump>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_Multijump>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.RUNAWAY)
                {
                    AI_Controller_RunAway controller = this.gameObject.GetComponent <AI_Controller_RunAway>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_RunAway>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);
                    controller.contact       = contactRepresentation;
                    controller.movementSpeed = behavior.movementSpeed;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.PULL)
                {
                    AI_Controller_Pull controller = this.gameObject.GetComponent <AI_Controller_Pull>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_Pull>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    if (ManagesGame.getInstance().noHostileAI)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);
                    controller.contact       = contactRepresentation;
                    controller.movementSpeed = behavior.movementSpeed;
                    controller.distance      = behavior.distance;

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
                else if (behavior.type == AI_Behavior.REMOVE_ABILITIES)
                {
                    AI_Controller_RemoveAbilities controller = this.gameObject.GetComponent <AI_Controller_RemoveAbilities>();

                    if (controller == null)
                    {
                        this.gameObject.AddComponent <AI_Controller_RemoveAbilities>();
                        continue;
                    }

                    if (behavior.priority != p)
                    {
                        continue;
                    }

                    if (ManagesGame.getInstance().noHostileAI)
                    {
                        continue;
                    }

                    check = check_for_contact(behavior);

                    if (controller.doBehavior(check))
                    {
                        reachedMaximumPriority = true;
                    }
                }
            }

            if (reachedMaximumPriority)
            {
                p = maximumPriority;
                reachedMaximumPriority = false;
            }
        }
    }