Example #1
0
    ////////////////////////   MESSAGE PROPAGATION  ////////////////////////

    //MessagePropagation: Agents stop when they enter the area of Influence of a transmitting agent and become transmitters themselves.
    //                    Communication simulated using blinking colors
    IEnumerator MessagePropagation()
    {
        bool toggle = true;

        while (true)
        {
            toggle = !toggle;
            for (int i = 0; i < listAgents.Count; i++)
            {
                if (listAgents[i].tag == "Moving")
                {
                    Vector3        agentPosition  = listAgents[i].transform.position;
                    Vector3        newDirection   = CM.PerlinVector(steeringForces[i], agentPosition, velocity);
                    Vector3        newPosition    = agentPosition + newDirection;
                    List <Vector3> agentPositions = listAgents.Select(a => a.transform.position).ToList();

                    if (CM.Colliding(AreaInfluence, agentPosition, staticAgents))
                    {
                        listAgents[i].GetComponent <Renderer>().material = blueGlowMaterial;
                        listAgents[i].tag = "Transmitting";
                        staticAgents.Add(agentPosition);
                    }
                    else
                    if (!CM.WillCollide(minDistance, agentPosition, newPosition, agentPositions) &&
                        !CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
                    {
                        listAgents[i].transform.Translate(newDirection);
                        steeringForces[i] = newDirection;
                    }
                    else if (CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
                    {
                        Vector3 correctiveVector = newDirection * 0.2f + CM.BackToBoundaries(agentPosition, newDirection, velocity, AreaMin, AreaMax) * 0.8f;    // Use repellingForce too?
                        listAgents[i].transform.Translate(correctiveVector);
                        steeringForces[i] = correctiveVector;
                    }
                    else
                    {
                        Vector3 colidingAgent           = CM.ClosestAgent(agentPosition, agentPositions);
                        float   distanceToColidingAgent = Vector3.Distance(newPosition, colidingAgent);
                        Vector3 perpendicularToAgent    = CM.AvoidObstacle(agentPosition, colidingAgent, newDirection, velocity);
                        float   repellingForce          = distanceToColidingAgent / AreaInfluence;

                        Vector3 correctiveVector = newDirection * repellingForce + perpendicularToAgent * (1 - repellingForce);
                        listAgents[i].transform.Translate(correctiveVector);
                        steeringForces[i] = correctiveVector;
                    }
                }
                if (listAgents[i].tag == "Transmitting")
                {
                    var renderer = listAgents[i].GetComponent <Renderer>();
                    renderer.material = toggle ? redGlowMaterial : blueGlowMaterial;
                }
            }
            yield return(new WaitForSeconds(0.1f));
        }
    }