Example #1
0
    buildingSpawn tryToPlaceBuilding(GameObject building, List <Cube> area)
    {
        buildingSpawn     spawn = null;
        List <GameObject> walls = new List <GameObject>();

        foreach (Transform child in building.transform)
        {
            if (child.gameObject.layer == LayerMask.NameToLayer("BlockingLayer"))
            {
                walls.Add(child.gameObject);
            }
        }
        if (walls.Count == 0)
        {
            return(null);
        }

        for (int i = 0; i < area.Count; ++i)
        {
            bool found = true;
            Cart diff  = Utils.toCartesian(walls[0].transform.position) - area[i].cartPos;
            foreach (GameObject wall in walls)
            {
                Cart pos    = Utils.toCartesian(wall.transform.position) - diff;
                bool inArea = false;
                foreach (Cube cube in area)
                {
                    if (pos == cube.cartPos && cube.instance.layer == LayerMask.NameToLayer("PlaceHolder"))
                    {
                        inArea = true;
                        break;
                    }
                }
                if (!inArea)
                {
                    found = false;
                    break;
                }
            }
            if (found)
            {
                spawn          = new buildingSpawn();
                spawn.building = building;
                spawn.diff     = diff;
            }
        }
        return(spawn);
    }
Example #2
0
    void placeBuildings()
    {
        islands     = new List <List <Cube> >();
        visited     = new bool[columnCount, rowCount];
        islandCount = 0;
        for (int x = 0; x < map.Length; ++x)
        {
            for (int y = 0; y < map[x].Length; ++y)
            {
                if (!visited[x, y] && map[x][y].instance.layer == LayerMask.NameToLayer("PlaceHolder"))
                {
                    visit(x, y, LayerMask.NameToLayer("PlaceHolder"));
                    islandCount++;
                }
            }
        }

        for (int i = 0; i < islandCount; ++i)
        {
            bool spaceLeft = true;
            while (spaceLeft)
            {
                List <buildingSpawn> possibleBuildings = new List <buildingSpawn>();
                foreach (GameObject building in buildings)
                {
                    buildingSpawn bSpawn = tryToPlaceBuilding(building, islands[i]);
                    if (bSpawn != null)
                    {
                        possibleBuildings.Add(bSpawn);
                    }
                }
                if (possibleBuildings.Count > 0)
                {
                    System.Random rd    = new System.Random();
                    int           index = rd.Next(0, possibleBuildings.Count);
                    GameObject    build = Instantiate <GameObject>(possibleBuildings[index].building);

                    foreach (Transform child in build.transform)
                    {
                        Cart pos = Utils.toCartesian(child.position) - possibleBuildings[index].diff;
                        if (child.gameObject.layer == LayerMask.NameToLayer("BlockingLayer"))
                        {
                            Destroy(map[pos.x][pos.y].instance);
                            map[pos.x][pos.y].instance = child.gameObject;
                            for (int x = pos.x - 1; x <= pos.x + 1; ++x)
                            {
                                for (int y = pos.y - 1; y <= pos.y + 1; ++y)
                                {
                                    if (x < map.Length && x > 0 && y < map[0].Length && y > 0 && map[x][y].instance.layer == LayerMask.NameToLayer("PlaceHolder"))
                                    {
                                        Cube cube = map[x][y];
                                        Destroy(cube.instance);
                                        cube.instance = Instantiate(tilemaps[0].GetTile(UnityEngine.Random.Range(0f, 100f)), cube.cartPos.toIsometric(), Quaternion.identity) as GameObject;
                                    }
                                }
                            }
                        }
                        child.position = pos.toIsometric();
                    }
                }
                else
                {
                    spaceLeft = false;
                }
            }
            foreach (Cube cube in islands[i])
            {
                if (cube.instance.layer == LayerMask.NameToLayer("PlaceHolder"))
                {
                    Destroy(cube.instance);
                    cube.instance = Instantiate(tilemaps[0].GetTile(UnityEngine.Random.Range(0f, 100f)), cube.cartPos.toIsometric(), Quaternion.identity) as GameObject;
                }
            }
        }
    }