Esempio n. 1
0
 private void InitLayers()
 {
     GroundLayer = new TilemapLayer
     {
         layer   = (int)Layers.GROUND,
         tilemap = groundTilemap,
     };
     ObjectsLayer = new TilemapLayer
     {
         layer   = (int)Layers.OBJECTS,
         tilemap = objectsTilemap,
     };
     SelectionLayer = new TilemapLayer
     {
         layer   = (int)Layers.SELECTION,
         tilemap = selectionTilemap,
     };
 }
Esempio n. 2
0
        public Dictionary <Vector3, IGameTile> CreatePopulatedTilemap(TilemapLayer tilemapLayer)
        {
            int     layer   = tilemapLayer.layer;
            Tilemap tilemap = tilemapLayer.tilemap;
            Dictionary <Vector3, IGameTile> localTilemap = new Dictionary <Vector3, IGameTile>();

            foreach (Vector3Int pos in tilemap.cellBounds.allPositionsWithin)
            {
                Vector3Int localPlace = new Vector3Int(pos.x, pos.y, layer);

                bool isTileEmpty = !tilemap.HasTile(localPlace);

                if (!isTileEmpty)
                {
                    print("Tile at: " + localPlace + " already exists!");
                    continue;
                }

                var worldLocation        = tilemap.CellToWorld(localPlace);
                var layeredWorldPosition = new Vector3(worldLocation.x, worldLocation.y, layer);

                IGameTile tileFromLibrary = GetTileByAssetName("grass_001");

                IGameTile tile = new GameTile
                {
                    LocalPlace    = localPlace,
                    WorldLocation = layeredWorldPosition,
                    TileBase      = tileFromLibrary.TileBase,
                    TilemapMember = tilemap,
                    Description   = tileFromLibrary.Description,
                    Cost          = 0,
                    TileData      = tileFromLibrary.TileData,
                };

                localTilemap.Add(layeredWorldPosition, tile);
            }

            return(localTilemap);
        }
Esempio n. 3
0
        public void PlaceTile(Vector3 pos, string assetName, TilemapLayer tilemapLayer)
        {
            Tilemap tilemap = tilemapLayer.tilemap;
            int     layer   = tilemapLayer.layer;

            Vector3Int tilemapPos           = tilemap.WorldToCell(pos);
            Vector3    layeredWorldPosition = new Vector3(tilemapPos.x, tilemapPos.y, layer);

            Vector3Int localPlace = new Vector3Int(tilemapPos.x, tilemapPos.y, layer);

            IGameTile newTile = TileLibrary.instance.GetClonedTile(assetName);

            newTile.LocalPlace    = localPlace;
            newTile.WorldLocation = layeredWorldPosition;
            newTile.TilemapMember = tilemap;

            // if a tile already exists there, just replace it.
            bool tileExistsInPos = tiles.ContainsKey(layeredWorldPosition);

            if (tileExistsInPos)
            {
                tiles[layeredWorldPosition] = newTile;
            }
            else
            {
                tiles.Add(layeredWorldPosition, newTile);
            }

            bool isACrop = newTile.GetType() == typeof(CropTile);

            if (isACrop)
            {
                (newTile as CropTile).StartGrowing();
            }

            SetGameTile(tilemapLayer, newTile);
        }
Esempio n. 4
0
 private void SetGameTile(TilemapLayer tilemapLayer, IGameTile gameTile)
 {
     tilemapLayer.tilemap.SetTile(gameTile.LocalPlace, gameTile.TileBase);
 }