예제 #1
0
    public void Generate(int numDisasters)
    {
        if (!initialized)
        {
            Init();
        }

        this.numDisasters = numDisasters;                                            // Track the number of disasters
        Clear();                                                                     // Make sure the grid is empty before generating a new map
        List <Vector3> disasterLocations = RandomizeDisasterLocations(numDisasters); // Determine where the disasters will be

        // Create the tiles
        for (int x = 0; x < GRID_WIDTH; x++)
        {
            for (int y = 0; y < GRID_HEIGHT; y++)
            {
                bool isDisasterLoc = false;
                foreach (Vector3 loc in disasterLocations)
                {
                    if (loc.x == x && loc.y == y)
                    {
                        isDisasterLoc = true;
                    }
                }

                //GameObject tileToSpawn = emptyTilePrefab;

                //if (isDisasterLoc)
                //{
                //	DisasterTypes type = (DisasterTypes)Random.Range((int)DisasterTypes.Fire, (int)DisasterTypes.Count);
                //	disasterTilePrefabs.TryGetValue(type, out tileToSpawn);
                //}

                //GameObject instance = Instantiate<GameObject>(tileToSpawn);
                //instance.transform.SetParent(gameObject.transform);
                //instance.transform.position = grid.LocalToWorld(grid.CellToLocalInterpolated(new Vector3(x - 4, y - 5, 0f) + anchor));
                //tiles[x, y] = new WorldTile(instance, isDisasterLoc);

                TileBase tileToSpawn = emptyTile;

                DisasterTypes type = DisasterTypes.Count;

                if (isDisasterLoc)
                {
                    type = (DisasterTypes)Random.Range((int)DisasterTypes.Fire, (int)DisasterTypes.Count);
                    disasterTiles.TryGetValue(type, out tileToSpawn);
                }

                Vector3Int cellLoc = new Vector3Int(x, y, 0);
                tilemap.SetTile(cellLoc, tileToSpawn);
                tiles[x, y] = new WorldTile(type, cellLoc);
            }
        }

        // Give each tile information about its neighbors
        for (int x = 0; x < GRID_WIDTH; x++)
        {
            for (int y = 0; y < GRID_HEIGHT; y++)
            {
                // Set the tile's left (no left if this is the first column)
                if (x > 0)
                {
                    tiles[x, y].SetAdjacentTile(WorldTile.TileDirections.Left, tiles[x - 1, y]);
                }

                // Set the tile's right (no right if this is the last column)
                if (x < GRID_WIDTH - 1)
                {
                    tiles[x, y].SetAdjacentTile(WorldTile.TileDirections.Right, tiles[x + 1, y]);
                }

                // Set the tile's above (no above if this is the first row)
                if (y < GRID_HEIGHT - 1)
                {
                    tiles[x, y].SetAdjacentTile(WorldTile.TileDirections.Above, tiles[x, y + 1]);
                }

                // Set the tile's below (no below if this is the last row)
                if (y > 0)
                {
                    tiles[x, y].SetAdjacentTile(WorldTile.TileDirections.Below, tiles[x, y - 1]);
                }
            }
        }
    }
예제 #2
0
 public WorldTile(DisasterTypes type, Vector3Int loc)
 {
     this.type    = type;
     cellLocation = loc;
 }