示例#1
0
    protected override void DoAIBehaviour()
    {
        Vector3 direction = dir;

        WeightedDirection wd = new WeightedDirection(dir, weight, type);

        myCritter.desiredDirections.Add(wd);
    }
示例#2
0
    void DoAIBehaviour()
    {
        if (myEntity.isInNest)
        {
            //We are in a nest, so nothing to return.
            return;
        }

        if (Entities.entitiesByType.ContainsKey(entityType) == false)
        {
            //Nothing to eat.
            wander.ChooseDirection();
            return;
        }

        //Finds theclosest target.
        Entities   closest = null;
        Perception prc     = new Perception();
        float      dist    = Mathf.Infinity;

        foreach (Entities c in Entities.entitiesByType[entityType])
        {
            float d = Vector2.Distance(this.transform.position, c.transform.position);

            if (closest == null && d < dist)
            {
                closest = c;
                dist    = d;
            }
        }

        if (closest == null)
        {
            //No valid targets exist.
            wander.ChooseDirection();
            return;
        }

        //If the predator is close have high weight
        float weight = 100 / (dist * dist);

        if (dist < 20)
        {
            //Move toward closest existing target.
            Vector2 dir = closest.transform.position - this.transform.position;
            //Rather than swapping the above vectors this method allows for smoother transitions between targets.
            dir *= -1;

            WeightedDirection wd = new WeightedDirection(dir, weight);

            myEntity.desiredDirections.Add(wd);
        }
        else
        {
            wander.ChooseDirection();
        }
    }
示例#3
0
    // Update is called once per frame
    void DoAIBehaviour()
    {
        //this part checks if the enemy type exists
        if (CreatureAI.creaturesByType.ContainsKey(enemyType) == false)
        {
            return;
        }

        //find the closest creature to us
        //set defaults
        CreatureAI closest  = null;
        float      distance = Mathf.Infinity;


        //Loop through all enemy to find the closest
        foreach (CreatureAI c in CreatureAI.creaturesByType[enemyType])
        {
            // if the creature is dead, then move on
            if (c.health <= 0)
            {
                continue;
            }

            //get it's distance from us
            float dist = Vector3.Distance(this.transform.position, c.transform.position);
            //if it's closer that the current closest then it is the closest
            if (closest == null || dist < distance)
            {
                closest  = c;
                distance = dist;
            }
        }

        //if there was no closest then do nothing
        //this part checks if any food in the types list exists
        //or if we need to worry
        if (closest == null || distance > lookOutRange)
        {
            return;
        }


        //if we get this far, there an enemy

        //if not closer move closer
        Vector3 dir = this.transform.position - closest.transform.position;
        //this form of calculation increases the importance the closer it gets
        //to the enemy
        float             descisionmportance = 10 / (distance * distance);
        WeightedDirection wd = new WeightedDirection(dir, descisionmportance);

        creature.desiredDirections.Add(wd);
    }
    void DoAIBehavior()
    {
        // Check Veto to not execute
        if (veto)
        {
            weight = 0.0f; return;
        }


        if (Character.characterByType.ContainsKey(charType) == false)
        {
            return;
        }

        // calculate nearest
        Character closest = null;
        float     dist    = Mathf.Infinity;

        foreach (Character c in Character.characterByType[charType])
        {
            float d = Vector3.Distance(this.transform.position, c.transform.position);
            if (closest == null || d < dist)
            {
                closest = c;
                dist    = d;
            }
        }
        // no Enemy existing
        if (closest == null)
        {
            weight           = 0.0f;
            weightCalculated = 0.0f;
            Vector3           dir = Vector3.zero;
            WeightedDirection wd  = new WeightedDirection(-dir, weight);
            return;
        }

        CalculateWeight(dist);

        // evade if
        if (dist > rangeOfCare)
        {
            return;
        }
        else // if in care Range
        {
            Vector3           dir = closest.transform.position - this.transform.position;
            WeightedDirection wd  = new WeightedDirection(-dir, weight);
            MyCharacter.desiredWeights.Add(wd);

            UIController.MyInstance.SetSliderValue(1.0f);
        }
    }
示例#5
0
    void DoAIBehavior()
    {
        // Check Veto to not execute
        if (veto)
        {
            weight = 0.0f;
        }
        else
        {
            CalculateWeight();
        }


        if (Character.characterByType.ContainsKey(charType) == false)
        {
            //nothing to do
            return;
        }

        // calculate nearest
        Character closest = null;
        float     dist    = Mathf.Infinity;

        foreach (Character c in Character.characterByType[charType])
        {
            float d = Vector3.Distance(this.transform.position, c.transform.position);
            if (closest == null || d < dist)
            {
                closest = c;
                dist    = d;
            }
        }
        // no Potion existing but my weight stays the same because my Mana is the condition
        if (closest == null)
        {
            return;
        }

        if (dist < collectingRange)
        {
            MyCharacter.RestoreMana(potionSize);
            Destroy(closest.gameObject);
        }
        else
        {
            Vector3           dir = closest.transform.position - this.transform.position;
            WeightedDirection wd  = new WeightedDirection(dir, weight);
            MyCharacter.desiredWeights.Add(wd);
            UIController.MyInstance.SetSliderValue(0.5f);
        }
    }
    void DoEnemyBehavior()
    {
        if (Character.characterByType.ContainsKey(charType) == false)
        {
            //nothing to do
            return;
        }

        // calculate nearest
        Character closestChar = null;
        float     dist        = Mathf.Infinity;

        foreach (Character c in Character.characterByType[charType])
        {
            float d = Vector3.Distance(this.transform.position, c.transform.position);
            if (closestChar == null || d < dist)
            {
                closestChar = c;
                dist        = d;
            }
        }
        // no Hero existing
        if (closestChar == null)
        {
            return;
        }

        //MyCharacter.LookAt(closestChar.transform);

        if (dist <= attackRange)
        {
            MyCharacter.animator.SetTrigger("isAttacking");
            attackCooldown -= Time.deltaTime;
            if (attackCooldown <= 0.0f)
            {
                attackCooldown = attackSpeed;
                closestChar.Hit(closestChar, damage);
            }
        }
        else
        {
            Vector3           dir = closestChar.transform.position - this.transform.position;
            WeightedDirection wd  = new WeightedDirection(dir, weight);
            MyCharacter.enemyAIList.Add(wd);
        }
    }
示例#7
0
    void DoAIBehavior()
    {
        // Check Veto to not execute.... MAYBE RETURN IS OK, to not calc everything
        if (veto)
        {
            weight = 0.0f;
        }
        // Go on and execute AI_Behavior
        else
        {
            CalculateWeight();
        }

        // get Target from HeroAI_Controller LookAt()
        target = HeroAI_Controller.MyInstance.MyTargetEnemy;

        if (Character.characterByType.ContainsKey(charType) == false)
        {
            return;
        }

        if (target == null)
        {
            weight           = 0.0f;
            weightCalculated = 0.0f;
            Vector3           dir = Vector3.zero;
            WeightedDirection wd  = new WeightedDirection(dir, weight);
        }
        else
        {
            AttackTarget();
            Vector3           dir = Vector3.zero;
            WeightedDirection wd  = new WeightedDirection(dir, weight);
            MyCharacter.desiredWeights.Add(wd);
        }
    }
示例#8
0
    //Alter update value if pathfinding is enabled such as A*
    void DoAIBehaviour()
    {
        if (Entities.entitiesByType.ContainsKey(entityType) == false)
        {
            //Nothing to eat.
            wander.ChooseDirection();
            return;
        }

        //Finds the closest target.
        Entities closest = null;
        float    dist    = Mathf.Infinity;

        foreach (Entities c in Entities.entitiesByType[entityType])
        {
            if (c.isInNest)
            {
                //This target is hiddent
                wander.ChooseDirection();
                continue;
            }

            float d = Vector2.Distance(this.transform.position, c.transform.position);
            if (closest == null || d < dist)
            {
                closest = c;
                dist    = d;
            }

            else
            {
                //Destroy(gameObject);
                wander.ChooseDirection();
            }
        }

        if (closest == null)
        {
            //No valid targets exist.
            Animal_Wander wander = new Animal_Wander();
            wander.ChooseDirection();
            return;
        }

        if (dist < eatingRange)
        {
            //myEntity.huntingSpeed = 0f;
            //closest.speed = 0f;
            float hpEaten = Mathf.Clamp(eatHPPerSecond * Time.deltaTime, 0, closest.health);
            closest.health  -= hpEaten;
            myEntity.hunger += hpEaten * eatHP2Hunger;
        }
        else
        {
            myEntity.huntingSpeed = myEntity.speed;
        }

        //Move toward closest existing target.
        Vector2 dir = closest.transform.position - this.transform.position;

        WeightedDirection wd = new WeightedDirection(dir, 10);

        if (myEntity.hunger <= 40)
        {
            myEntity.desiredDirections.Add(wd);
        }
    }
示例#9
0
    void DoAIBehaviour()
    {
        if (Entities.entitiesByType.ContainsKey(entityType) == false)
        {
            //No predator near by.
            return;
        }

        bool predatorNearby = false;

        foreach (Entities c in Entities.entitiesByType[entityType])
        {
            float d = Vector2.Distance(this.transform.position, c.transform.position);

            if (d < fearDistance)
            {
                predatorNearby = true;
                break;
            }
        }

        if (predatorNearby == false)
        {
            //Nothing to fear.
            return;
        }

        /*
         *-------------------------
         * LEAVING IT HERE!!!
         * ------------------------
         */

        //Move towards the closest nest.
        Hare_Nest[] nests = GameObject.FindObjectsOfType <Hare_Nest>();

        Hare_Nest closest = null;
        float     dist    = Mathf.Infinity;

        foreach (Hare_Nest n in nests)
        {
            float d = Vector2.Distance(this.transform.position, n.transform.position);

            if (closest == null || d < dist)
            {
                closest = n;
                dist    = d;
            }
        }

        if (closest == null)
        {
            //no nests
            return;
        }

        Vector2 dir = closest.transform.position - this.transform.position;

        WeightedDirection wd = new WeightedDirection(dir, 50);

        myEntity.desiredDirections.Add(wd);
    }
示例#10
0
    // Update is called once per frame
    void DoAIBehaviour()
    {
        //this part checks if the food type exists
        if (CreatureAI.creaturesByType.ContainsKey(foodType) == false)
        {
            return;
        }

        //find the closest creature to us
        //set defaults
        CreatureAI closest  = null;
        float      distance = Mathf.Infinity;


        //Loop through all food to find the closest
        foreach (CreatureAI c in CreatureAI.creaturesByType[foodType])
        {
            // if the creature is dead, then move on
            if (c.health <= 0)
            {
                continue;
            }

            //get it's distance from us
            float dist = Vector3.Distance(this.transform.position, c.transform.position);
            //if it's closer that the current closest then it is the closest
            if (closest == null || dist < distance)
            {
                closest  = c;
                distance = dist;
            }
        }

        //if there was no closest then do nothing
        //this part checks if any food in the types list exists
        if (closest == null)
        {
            return;
        }


        //if not hungry or beside food, do nothing
        if (creature.hunger >= hungerTolerance && distance > eatingRange)
        {
            return;
        }

        //if we get this far, there is food
        //if we are close enough, take a byte else get closer
        if (distance < eatingRange)
        {
            float bite = Mathf.Clamp(biteDamage * Time.deltaTime, 0, closest.health);
            closest.health  -= bite;
            creature.hunger += bite * eatingGains;
        }
        else
        {
            //if not closer move closer
            Vector3           dir = closest.transform.position - this.transform.position;
            WeightedDirection wd  = new WeightedDirection(dir, descisionmportance);
            creature.desiredDirections.Add(wd);
        }
    }