Exemple #1
0
    void DropPheromones()
    {
        foreach (GameObject pheromoneObj in pheromones)
        {
            Pheromone pheromone = pheromoneObj.GetComponent <Pheromone>();
            bool      drop      = false;
            float     strength  = 0;
            switch (pheromone.GetPheromoneType())
            {
            case Pheromone.PheromoneType.Food:
                if (hasFood)
                {
                    drop     = true;
                    strength = pheromone.GetStartingStrength() - (pheromone.dropRate * foodCount * 2);
                }
                break;

            case Pheromone.PheromoneType.Home:
                if (!hasFood && !goHome)
                {
                    drop     = true;
                    strength = pheromone.GetStartingStrength() - (pheromone.dropRate * homeCount * 2);
                }
                break;
            }
            if (strength < 0)
            {
                strength = 0;
            }

            if (drop)
            {
                Debug.Log(strength);
                GameObject droppedPheromone = Instantiate(pheromoneObj);
                droppedPheromone.GetComponent <Pheromone>().SetColony(colony);
                droppedPheromone.GetComponent <Pheromone>().strength = (int)strength;
                droppedPheromone.transform.position = transform.position;
            }
        }
    }
Exemple #2
0
    void UpdateDirection()
    {
        Vector3 prevDirection = direction;

        // Momentum
        direction = direction * momentumRatio;

        // Target
        int     topStrength  = 0;
        Vector3 topDirection = Vector3.zero;

        foreach (GameObject pheromoneObj in world.GetPheromones())
        {
            if (Vector3.Distance(transform.position, pheromoneObj.transform.position) < range)
            {
                Pheromone pheromone = pheromoneObj.GetComponent <Pheromone>();
                switch (pheromone.GetPheromoneType())
                {
                case Pheromone.PheromoneType.Food:
                    if (!hasFood && !goHome && !explorer)
                    {
                        if (pheromone.GetStrength() > topStrength)
                        {
                            topStrength  = pheromone.GetStrength();
                            topDirection = (pheromoneObj.transform.position - transform.position).normalized;
                        }
                    }
                    break;

                case Pheromone.PheromoneType.Home:
                    if (hasFood || goHome)
                    {
                        if (pheromone.GetStrength() > topStrength)
                        {
                            topStrength  = pheromone.GetStrength();
                            topDirection = (pheromoneObj.transform.position - transform.position).normalized;
                        }
                    }
                    break;
                }
            }
        }
        direction += topDirection * targetRatio;

        /*
         * foreach(GameObject ant in world.GetAnts())
         * {
         *  if (Vector3.Distance(transform.position, ant.transform.position) < 2)
         *  {
         *      direction += (transform.position - ant.transform.position).normalized*2;
         *  }
         * }
         */


        //Randomize slightly
        direction += new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f)) * randomRatio;

        //Wall avoidance
        Vector3    myPosition = transform.position;
        RaycastHit hit;

        if (Physics.Raycast(myPosition, prevDirection, out hit, range))
        {
            if (hit.collider.gameObject.tag == "Wall")
            {
                direction += (transform.position - hit.point).normalized * hitRatio;
            }
        }

        //No climbing
        if (direction.y > 0)
        {
            direction.y = 0;
        }

        direction.Normalize();
    }