Example #1
0
    public GridTile AddTileAtKey(TileKey key)
    {
        GridTile _newTile = Instantiate(TilePrefab, transform);

        _newTile.Init(key);
        GridTiles.Add(key, _newTile);
        _newTile.UpdateSurroundingTiles(this);
        return(_newTile);
    }
Example #2
0
 public void InitGrid()
 {
     grid = new GridTile[width, height];
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             GridTile tile = Instantiate(tilePrefab);
             tile.Init(new Coord(x, y), transform);
             grid[x, y] = tile;
         }
     }
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             GridTile tile = grid[x, y];
             if (x > 0)
             {
                 tile.neighbors.Add(Coord.left, grid[x - 1, y]);
             }
             if (x < width - 1)
             {
                 tile.neighbors.Add(Coord.right, grid[x + 1, y]);
             }
             if (y > 0)
             {
                 tile.neighbors.Add(Coord.down, grid[x, y - 1]);
             }
             if (y < height - 1)
             {
                 tile.neighbors.Add(Coord.up, grid[x, y + 1]);
             }
         }
     }
     //temp
     for (int i = 0; i < 5; i++)
     {
         int x = UnityEngine.Random.Range(0, width);
         int y = UnityEngine.Random.Range(0, height);
         if (grid[x, y].gridObjects.Count == 0)
         {
             Plant plant = Instantiate(plantPrefab);
             plant.Init(grid[x, y],
                        Services.breedManager.breeds[BreedManager.BreedType.BasicPlant]);
         }
     }
 }
Example #3
0
    public void GenerateGrid()
    {
#if UNITY_EDITOR
        for (int xindex = 0; xindex < Xtiles; xindex++)
        {
            for (int zindex = 0; zindex < Ztiles; zindex++)
            {
                TileKey  _newKey  = new TileKey(xindex, zindex);
                GridTile _newTile = (GridTile)PrefabUtility.InstantiatePrefab(TilePrefab, transform);
                _newTile.Init(_newKey);
                GridTiles.Add(_newKey, _newTile);
            }
        }

        foreach (KeyValuePair <TileKey, GridTile> item in GridTiles)
        {
            item.Value.SetSurroundingTiles(this);
        }
#endif
    }
Example #4
0
    public void LoadGridFromWorld()
    {
        foreach (var item in GridTiles)
        {
            item.Value.ClearSurroundingTiles(this);
        }
        GridTiles.Clear();

        GridTile[] _tileArray = FindObjectsOfType <GridTile>();

        // inactive gridtiles should not be included in this fetch
        for (int i = _tileArray.Length - 1; i >= 0; i--)
        {
            GridTile _tile = _tileArray[i];

            TileKey _key = GetKeyFromVector(_tile.transform.position);
            if (GridTiles.ContainsKey(_key))
            {
                DestroyImmediate(_tile.gameObject);
                Debug.Log(_key + " destroyed");
                continue;
            }

            Vector3 _position = Vector3.zero;
            _position.x = Mathf.RoundToInt(_key.X);
            _position.z = Mathf.RoundToInt(_key.Z);
            _tile.transform.position = _position;
            _tile.transform.parent   = transform;
            _tile.name = _key.X + " " + _key.Z;
            _tile.Init(_key);

            GridTiles.Add(_key, _tile);
        }


        foreach (KeyValuePair <TileKey, GridTile> item in GridTiles)
        {
            item.Value.SetSurroundingTiles(this);
        }
        Debug.Log(GridTiles.Count);
    }