/// <summary> /// /// </summary> private void CreateLevelChunk() { Debug.unityLogger.Log(TAG, "CreateLevelChunk()"); //--For each of the <color,list> objects in the construct data draw a list of objects //--according to the list <x,y> and color parameter. foreach (PixelData pixel in mCachedLevel[mChunkIndex].Values) { if (pixel == null) { Debug.unityLogger.Log(TAG, "pixel is null"); } if (m_ColorToObjectDict == null) { Debug.unityLogger.Log(TAG, "m_ColorToObjectDict is null"); } //--Get the tile data from the color to object fictionary m_ColorToObjectDict.TryGetValue(pixel.color, out TilePainter tilePainter); if (tilePainter == null) { Debug.unityLogger.Log(TAG, "tile data not set in specified map: " + pixel.color); } //--Get the tilemap type from the painter Tilemap tilemapToDrawUnto = mTilemapDefault; if (tilePainter.GetType().Equals(typeof(RuleTilePainter))) { TilemapType tileMapType = ((RuleTilePainter)tilePainter).GetTilemapType(); switch (tileMapType) { case TilemapType.TilemapPlatform: tilemapToDrawUnto = mTilemapPlatform; break; case TilemapType.TilemapSolid: tilemapToDrawUnto = mTilemapSolid; break; } } //--Draw the objects unto the tilemap DrawPixelListInTilemap(ref tilemapToDrawUnto, pixel.points, tilePainter); //Cleam tile data pointer tilePainter = null; } //--After we are done painting the batch we increment the chunk index mChunkIndex++; }
public Cell GetNeighbourOfType(TilemapType tilemapType) { _neighbours = new List <Cell> { Up, Down, Left, Right }; foreach (var neighbour in _neighbours) { if (TilemapController.Instance.HasTile(tilemapType, BitMath.ToVector3Int(neighbour.Position))) { return(neighbour); } } Debug.LogError("No neighbour of type " + tilemapType + " in tile " + Position); return(null); }
public void SetColorOnTilemap(TilemapType tilemapType, Vector2Int tilePosition, Color newTileColor) { if (!_tilemapsInitialized) { InitializeTilemaps(); } TilemapType tilemapTypeFlagged = tilemapType | TilemapType.None; IEnumerable <UnityEngine.Tilemaps.Tilemap> tilemapsToModify = _tilemapTypesToTilemaps.Where(kvp => tilemapTypeFlagged.HasFlag(kvp.Key)).Select(kvp => kvp.Value); foreach (var tilemap in tilemapsToModify) { tilemap.SetTileFlags(tilePosition.ToVector3Int(), TileFlags.None); tilemap.SetColor(tilePosition.ToVector3Int(), newTileColor); } }
/// <summary> /// Returns the tile type of a block in this chunk, in the given Tilemap. /// </summary> /// <param name="position"></param> /// <param name="mapType"></param> /// <returns></returns> public TileType GetChunkTileType(Vector3Int position, TilemapType mapType) { if (isUnloading) { return(TileType.AIR); } TileType[,] data = GetTypeMap(mapType); if (data == null) { return(TileType.AIR); } Vector3Int relativePosition = position - Position; return(data[relativePosition.x, relativePosition.y]); }
/// <summary> /// Sets the tile type of a block in this chunk, in the given Tilemap. /// </summary> /// <param name="position"></param> /// <param name="mapType"></param> /// <param name="type"></param> public void SetChunkTileType(Vector3Int position, TilemapType mapType, TileType type) { if (isUnloading) { return; } TileType[,] data = GetTypeMap(mapType); if (data == null) { return; } Vector3Int relativePosition = position - Position; data[relativePosition.x, relativePosition.y] = type; }
public Tilemap(int p_columns, int p_rows, int p_tilewidth, int p_tileheight, TilemapType p_tilemapType, int p_fill = -1) { columns = p_columns; rows = p_rows; tilewidth = p_tilewidth; tileheight = p_tileheight; map = new int[p_columns, p_rows]; for (int y = 0; y < p_rows; y++) { for (int x = 0; x < p_columns; x++) { map[x, y] = p_fill; } } type = p_tilemapType; }
/// <summary> /// Returns the tile stored in the given Tilemap, on the given position. /// </summary> /// <param name="position"></param> /// <param name="mapType"></param> /// <returns></returns> public TileBase GetChunkTile(Vector3Int position, TilemapType mapType) { if (isUnloading) { return(null); } Tilemap targetMap = GetMap(mapType); if (targetMap == null) { return(null); } Vector3Int relativePosition = position - Position; return(targetMap.GetTile(relativePosition)); }
/// <summary> /// Fills the given position in the given Tilemap with the given Tile. /// </summary> /// <param name="position"></param> /// <param name="mapType"></param> /// <param name="tile"></param> public void SetChunkTile(Vector3Int position, TilemapType mapType, Tile tile) { if (isUnloading) { return; } Tilemap targetMap = GetMap(mapType); if (targetMap == null) { return; } Vector3Int relativePosition = position - Position; targetMap.SetTile(relativePosition, tile); }
/// <summary> /// Returns the right Tilemap for the given TilemapType constant for this chunk. /// </summary> /// <param name="mapType"></param> /// <returns></returns> private Tilemap GetMap(TilemapType mapType) { if (isUnloading) { return(null); } switch (mapType) { case TilemapType.BLOCKS_FRONT: return(tileMapBlocksFront); case TilemapType.BLOCKS_BACK: return(tileMapBlocksBack); default: return(null); } }
/// <summary> /// Sets the color of the tile in the given Tilemap at the given position. /// /// NOTE: Tiles by default have TileFlags that prevent color changes, rotation /// lock etc. Make sure you unlock these tiles by changing its TileFlags or go /// into the inspector and visit your tiles, turn on debug mode and change it there, /// before using this function. /// </summary> /// <param name="position"></param> /// <param name="color"></param> /// <param name="mapType"></param> public void SetChunkTileColor(Vector3Int position, Color color, TilemapType mapType = TilemapType.BLOCKS_FRONT) { if (isUnloading) { return; } Tilemap targetMap = GetMap(mapType); if (targetMap == null) { return; } Vector3Int relativePosition = position - Position; targetMap.SetColor(relativePosition, color); }
public void SetTile(Vector3Int position, TileBase tile, TilemapType tilemapType) { switch (tilemapType) { case TilemapType.Road: roadTilemap.SetTile(position, tile); break; case TilemapType.Town: townTilemap.SetTile(position, tile); break; case TilemapType.Background: backgroundTilemap.SetTile(position, tile); break; default: throw new ArgumentOutOfRangeException(nameof(tilemapType), tilemapType, null); } }
/// <summary> /// Sets the rotation of a given tile at the given position in the given Tilemap. /// /// NOTE: Tiles by default have TileFlags that prevent color changes, rotation /// lock etc. Make sure you unlock these tiles by changing its TileFlags or go /// into the inspector and visit your tiles, turn on debug mode and change it there, /// before using this function. /// </summary> /// <param name="position"></param> /// <param name="mapType"></param> /// <param name="rotation"></param> public void SetChunkTileRotation(Vector3Int position, TilemapType mapType, Vector3 rotation) { if (isUnloading) { return; } Tilemap targetMap = GetMap(mapType); if (targetMap == null) { return; } Vector3Int relativePosition = position - Position; Quaternion matrixRotation = Quaternion.Euler(rotation); Matrix4x4 matrix = Matrix4x4.Rotate(matrixRotation); targetMap.SetTransformMatrix(relativePosition, matrix); }
/// <summary> /// This is the actual level builder API, it is a given the json data holding the construct's information /// the scene the object should be deployed to and a holder. /// </summary> /// <param name="jsonData"></param> /// <param name="scene"></param> /// <param name="holder"></param> /// <returns></returns> private GameObject CreateConstruct(GameEvent chunk, CountDownRoot holder) { //Get Bounds data ConstructData constructData = chunk.ConstructData; //int h = constructData.h; int w = constructData.w; int offset = constructData.Offset + (int)mScene.mScreenWidthInWorld; //float hRatio = scene.mScreenHeightInWorld / h; //float yOffset = scene.mScreenHeightInWorld / 2 + scene.transform.position.y; //float xOffset = scene.mScreenWidthInWorld / 2 + scene.transform.position.x; //--Instantiate a new grid GameObject grid = GameObject.Instantiate(m_GridPrefab, Vector3.zero, Quaternion.identity); //--Set the grid as child of the scene grid.transform.parent = mScene.transform; //GameObject tilemapGameObject = GameObject.Instantiate(m_TilemapPlatformPrefab, scene.transform.position, Quaternion.identity); //Tilemap tilemap = tilemapGameObject.GetComponent<Tilemap>(); //tilemap.SendMessage("SubscribeToRoot", holder); //Tilemap tilemap = grid.GetComponentInChildren<Tilemap>(); //tilemap.transform.parent = grid.transform; //WorldFiniteObject wfo = tilemap.GetComponent<WorldFiniteObject>(); //--For each of the <color,list> objects in the construct data draw a list of objects //--according to the list <x,y> and color parameter. foreach (PixelData pixel in constructData.data) { if (pixel == null) { Debug.Log("pixel is null "); } if (m_ColorToObjectDict == null) { Debug.Log("m_ColorToObjectDict is null "); } //--Get the tile data from the color to object fictionary m_ColorToObjectDict.TryGetValue(pixel.color, out TilePainter tilePainter); if (tilePainter == null) { Debug.Log("tile data not set in specified map: " + pixel.color); } GameObject tilemapTemplate = m_TilemapPrefab_Default; if (tilePainter.GetType().Equals(typeof(RuleTilePainter))) { TilemapType tileMapType = ((RuleTilePainter)tilePainter).GetTilemapType(); switch (tileMapType) { case TilemapType.TilemapPlatform: tilemapTemplate = m_TilemapPrefab_Platform; break; case TilemapType.TilemapSolid: tilemapTemplate = m_TilemapPrefab_Solid; break; } } //get pixel collider type and get the corrseponfing tilemap . //--Generate a tilemap layer to draw the objects unto. GameObject tilemapGameObject = GameObject.Instantiate(tilemapTemplate, Vector3.zero, Quaternion.identity); tilemapGameObject.transform.parent = grid.transform; tilemapGameObject.SendMessage("SubscribeToRoot", holder); tilemapGameObject.SendMessage("Init"); Tilemap tilemap = tilemapGameObject.GetComponent <Tilemap>(); //--Draw the objects unto the tilemap DrawPixelListInTilemap(ref tilemap, pixel.points, tilePainter, holder); //Cleam tile data pointer tilePainter = null; //--This is a hack, if the tilemap is empty (only coins for example which is a prefab) //--then I still want to know when the grid is out of bound, hence I draw this extra pixel PixelData pd = new PixelData("ff00ff", new List <Point>() { new Point(w, 0) }); m_ColorToObjectDict.TryGetValue(pd.color, out tilePainter); DrawPixelListInTilemap(ref tilemap, pd.points, tilePainter, holder); //--offset the tile map position grid.transform.position = mScene.transform.position + new Vector3( offset * grid.GetComponent <Grid>().cellSize.x - 40, mScene.mScreenHeightInWorld / 2 + 1); tilemap.transform.position = grid.transform.position; } ////--This is a hack, if the tilemap is empty (only coins for example which is a prefab) ////--then I still want to know when the grid is out of bound, hence I draw this extra pixel //PixelData pd = new PixelData("ff00ff", new List<Point>() { new Point(w, 0) }); //DrawPixelInTilemap(ref tilemap, pd, holder); //wfo.SubscribeToRoot(holder); //wfo.StartCoroutine("SetAsChildOfGrid", holder); ////--offset the tile map position //grid.transform.position = // scene.transform.position + // new Vector3( // scene.mScreenWidthInWorld / 2, // scene.mScreenHeightInWorld / 2 + 1); //tilemap.transform.position = grid.transform.position; //Debug.Log("Created constructat time: {0} at " + Time.time); Debug.unityLogger.Log(TAG, string.Format("Created chunk {3} in offset {0}, of size {1} at time {2}", offset, w, Time.time, chunk.mName)); //--Updates the offset offset = offset += w; //If the tilemap doesnt have any tiles in it we want to desrtroy it //TODO optimize //if(tilemap.layoutGrid.transform.childCount == 0) //{ // wfo.DestroyMe(); //} return(grid); }
public void DeleteTile(TilemapType tilemapType, Vector3Int position) { _tilemaps[tilemapType].SetTile(position, null); }
public bool HasTile(TilemapType tilemapType, Vector3Int position) { return(_tilemaps[tilemapType].GetTile(position) != null); }
public void SetTile(TilemapType tilemapType, TileBase tile, Vector3Int position) { _tilemaps[tilemapType].SetTile(position, tile); }
public static void PaintTilemap(Tilemap tilemap, float[,] heightMap, TilemapBiomeData tilemapBiomeData, TilemapType tilemapType) { MapGenerator mapGenerator = MapGenerator.instance; EndlessTerrain endlessTerrain = EndlessTerrain.instance; Vector3Int tileCoord; for (int x = tilemap.cellBounds.xMin; x < tilemap.cellBounds.xMax; x++) { for (int y = tilemap.cellBounds.yMin; y < tilemap.cellBounds.yMax; y++) { tileCoord = new Vector3Int(x, y, 0); tilemap.SetTile(tileCoord, DetermineTile(tilemap, heightMap, tileCoord, tilemapBiomeData, mapGenerator, tilemapType)); } } }
static RuleTile DetermineTile(Tilemap tilemap, float[,] heightMap, Vector3Int tileCoord, TilemapBiomeData tilemapBiomeData, MapGenerator mapGenerator, TilemapType tilemapType) { float heightMapValue = heightMap[tileCoord.x, tileCoord.y]; Tile newTile = (Tile)tilemap.GetTile(tileCoord); Vector3 worldPos = tilemap.GetCellCenterWorld(tileCoord); if (tilemapType == TilemapType.Water) { if (heightMapValue <= mapGenerator.regions[1].height) // Deep Water { if (tilemapBiomeData.deepWaterTiles != null) { GameTiles.instance.deepWaterTiles.Add(worldPos, newTile); return(tilemapBiomeData.deepWaterTiles); } } } if (tilemapType == TilemapType.Ground) { if (heightMapValue > mapGenerator.regions[1].height && heightMapValue <= mapGenerator.regions[2].height) // Shallow water { if (tilemapBiomeData.shallowWaterTiles != null) { GameTiles.instance.shallowWaterTiles.Add(worldPos, newTile); return(tilemapBiomeData.shallowWaterTiles); } } if (heightMapValue > mapGenerator.regions[2].height && heightMapValue <= mapGenerator.regions[3].height) // Sand { if (tilemapBiomeData.sandTiles != null) { GameTiles.instance.sandTiles.Add(worldPos, newTile); return(tilemapBiomeData.sandTiles); } } if (heightMapValue > mapGenerator.regions[3].height && heightMapValue <= mapGenerator.regions[4].height) // Grass 1 { if (tilemapBiomeData.shortGrassRuleTile != null) { GameTiles.instance.shortGrassTiles.Add(worldPos, newTile); return(tilemapBiomeData.shortGrassRuleTile); } } if (heightMapValue > mapGenerator.regions[4].height && heightMapValue <= mapGenerator.regions[5].height) // Grass 2 { if (tilemapBiomeData.tallGrassTiles != null) { GameTiles.instance.tallGrassTiles.Add(worldPos, newTile); return(tilemapBiomeData.tallGrassTiles); } } if (heightMapValue > mapGenerator.regions[5].height && heightMapValue <= mapGenerator.regions[6].height) // Rock 1 { if (tilemapBiomeData.rockyGroundTiles != null) { GameTiles.instance.rockyGroundTiles.Add(worldPos, newTile); return(tilemapBiomeData.rockyGroundTiles); } } } if (tilemapType == TilemapType.Wall) { if (heightMapValue > mapGenerator.regions[6].height) { if (tilemapBiomeData.rockyMountainTiles != null) { GameTiles.instance.rockyMountainTiles.Add(worldPos, newTile); return(tilemapBiomeData.rockyMountainTiles); // Rock 2 } } } return(null); }