Vector3 GetTargetPosition()
    {
        float[,] map = m_influenceMap.GetInfluenceMap();
        float minPosValue     = 100.0f;
        float currentPosValue = 0.0f;

        List <Vector3> positions  = new List <Vector3>();
        Vector3        currentPos = transform.position;

        // Kernel of 3x3 square
        if (m_kernelSize == 3)
        {
            // Loop through all to find the position with the smallest value
            for (int y = 1; y < m_mapSize - 1; y = y + m_kernelSize)
            {
                for (int x = 1; x < m_mapSize - 1; x = x + m_kernelSize)
                {
                    currentPosValue = map[x - 1, y + 1] + map[x, y + 1] + map[x + 1, y + 1]
                                      + map[x - 1, y] + map[x, y] + map[x + 1, y]
                                      + map[x - 1, y - 1] + map[x, y - 1] + map[x + 1, y - 1];

                    // Check if current value is the smallest one and replace list with the new value
                    if (currentPosValue < minPosValue)
                    {
                        minPosValue = currentPosValue;
                        positions.Clear();
                        positions.Add(m_influenceMap.Indices2World(x, y));
                    }
                    // Check if position is close enough and add to list
                    else if (Mathf.Abs(Mathf.Min(currentPosValue, minPosValue)) < 0.5f)
                    {
                        positions.Add(m_influenceMap.Indices2World(x, y));
                    }
                    // Do nothing if the position is not of interest
                }
            }
        }

        // Randomise between the values in the list of positions
        currentPos = positions[Mathf.RoundToInt((Random.Range(0, positions.Count)))];
        return(currentPos);
    }
Beispiel #2
0
    Vector3 GetNearbyPosition(Vector2 mapPos)
    {
        float minX = Mathf.Max(1, mapPos.x - 4.0f);
        float maxX = Mathf.Min(m_influenceMap.GetSize() - 1, mapPos.x + 4.0f);
        float minZ = Mathf.Max(1, mapPos.y - 4.0f);
        float maxZ = Mathf.Min(m_influenceMap.GetSize() - 1, mapPos.y + 4.0f);

        int newX = Mathf.RoundToInt(Random.Range(minX, maxX));
        int newY = Mathf.RoundToInt(Random.Range(minZ, maxZ));

        Vector2 temp = m_influenceMap.Indices2World(newX, newY);

        return(new Vector3(temp.x, transform.position.y, temp.y));
    }