Пример #1
0
    // PlaceAgentsInRowsDictionary: Places agents in a regular grid using a dictionary for spatial subdivision
    public Dictionary <Vector2Int, List <GameObject> > PlaceAgentsInRowsDictionary(Vector3 placement)
    {
        int rows    = (int)Mathf.Sqrt(NumberOfAgents) + 1;
        int columns = (int)Mathf.Sqrt(NumberOfAgents) + 1;

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (listAgents.Count < NumberOfAgents)
                {
                    Vector3    position   = new Vector3(i + i, 0, j + j) + placement;
                    Vector2Int cell       = Subdivision.GridLocation(position);
                    GameObject placeAgent = Object.Instantiate(agent, position, Quaternion.identity);
                    placeAgent.GetComponent <Renderer>().material = material;
                    placeAgent.tag = "Moving";
                    listAgents.Add(placeAgent);

                    if (dictionaryAgents.TryGetValue(cell, out List <GameObject> agentsInCell))
                    {
                        agentsInCell.Add(placeAgent);
                    }
                    else
                    {
                        agentsInCell = new List <GameObject>();
                        agentsInCell.Add(placeAgent);
                        dictionaryAgents.Add(cell, agentsInCell);
                    }
                }
            }
        }
        return(dictionaryAgents);
    }
Пример #2
0
    // RandomWalkNoCollisionsDictionary: Same but with a dictionary for spatial subdivision  ->  SOMETHING IS WRONG WITH THIS ONE...
    public void RandomWalkNoCollisionsDictionary()
    {
        foreach (var cell in dictionaryAgents.Keys.ToList())
        {
            foreach (GameObject agent in dictionaryAgents[cell].ToList())
            {
                Vector3 newDirection  = CM.RandomVector(velocity);
                Vector3 agentPosition = agent.transform.position;
                Vector3 newPosition   = agentPosition + newDirection;

                Vector2Int        newCell           = Subdivision.GridLocation(newPosition);
                List <Vector2Int> neighbouringCells = Subdivision.ClosestCells(newPosition, newCell, AreaInfluence);

                if (!CM.WillCollide(dictionaryAgents, minDistance, agentPosition, newPosition, neighbouringCells) &&
                    !CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
                {
                    agent.GetComponent <Renderer>().material = whiteGlowMaterial;
                    agent.transform.Translate(newDirection);

                    if (cell != newCell)
                    {
                        Subdivision.UpdateDictionary(dictionaryAgents, cell, newCell, agent);
                    }
                }
                else
                {
                    agent.GetComponent <Renderer>().material = redGlowMaterial;
                }
            }
        }
    }