private void UpdateMapLayout(int[,] map, Vector3 cellSize) { int index = 0; for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { if (map[x, y] != 0) { var location = new Vector3Int(x, y, 0); int xDir, zDir; FlipCell(cellSize, x, y, out xDir, out zDir); var position = new Vector3Int(xDir, 0, zDir); ThreeDTileData threeDTileData = new ThreeDTileData(); m_tileMap.GetTileData(location, this, ref threeDTileData); m_tiles[location].transform = threeDTileData.transform; var entity = m_entityArray[index]; UpdateEntity(entity, position, threeDTileData); index++; } } } }
private void UpdateTile(Vector3Int location, ITileGrid tileMap, ref ThreeDTileData tileData) { tileData.transform = Matrix4x4.identity; int mask = TileValue(tileMap, location + new Vector3Int(0, 1, 0)) ? (int)Neighbor.North : 0; // North mask += TileValue(tileMap, location + new Vector3Int(1, 1, 0)) ? (int)Neighbor.NorthEast : 0; // NorthEast mask += TileValue(tileMap, location + new Vector3Int(1, 0, 0)) ? (int)Neighbor.East : 0; // East mask += TileValue(tileMap, location + new Vector3Int(1, -1, 0)) ? (int)Neighbor.SouthEast : 0; // SouthEast mask += TileValue(tileMap, location + new Vector3Int(0, -1, 0)) ? (int)Neighbor.South : 0; // South mask += TileValue(tileMap, location + new Vector3Int(-1, -1, 0)) ? (int)Neighbor.SouthWest : 0; // SouthWest mask += TileValue(tileMap, location + new Vector3Int(-1, 0, 0)) ? (int)Neighbor.West : 0; // West mask += TileValue(tileMap, location + new Vector3Int(-1, 1, 0)) ? (int)Neighbor.NorthWest : 0; // NorthWest Debug.Log($"{(Neighbor)mask} ---- {mask}"); byte original = (byte)mask; if ((original | 254) < 255) { mask = mask & 125; } if ((original | 251) < 255) { mask = mask & 245; } if ((original | 239) < 255) { mask = mask & 215; } if ((original | 191) < 255) { mask = mask & 95; } int index = GetIndex((byte)mask); if (index >= 0 && index < m_tileObjects.Length && TileValue(tileMap, location)) { tileData.gameObject = m_tileObjects[index]; tileData.transform = GetTransform((byte)mask); } }
private void UpdateEntity(Entity entity, Vector3Int position, ThreeDTileData threeDTileData) { m_entityManager.SetComponentData(entity, new Translation { Value = new float3(position.x, 0, position.z) }); m_entityManager.SetComponentData(entity, new Rotation { Value = new quaternion(threeDTileData.transform) }); m_entityManager.SetComponentData(entity, new Scale { Value = 1 }); m_entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = threeDTileData.gameObject.GetComponentInChildren <MeshFilter>().sharedMesh, material = m_material }); }
private void GenerateMap_Normal() { m_mapHolder = new GameObject("MapHolder"); m_tiles = new Dictionary <Vector3Int, Tile>(); var seed = UnityEngine.Random.Range(0, 99999); var map = MapFunctions.GenerateCellularAutomata(m_width, m_length, seed, m_fillPercent, m_edgesAreWalls); map = MapFunctions.SmoothMooreCellularAutomata(map, m_edgesAreWalls, m_smoothCount); if (m_randomWalkTopEnabled == true) { map = MapFunctions.RandomWalkTop(map, seed); if (m_randomWalkTopSmoothedEnabled == true) { map = MapFunctions.RandomWalkTopSmoothed(map, seed, m_minSectionWidth); } } for (int i = 0; i < m_numberOfRivers; i++) { if (m_directionTunnelEnabled == true) { var rand = UnityEngine.Random.Range(0, 2) * 2 - 1; map = MapFunctions.DirectionalTunnel(map, m_minPathWidth, m_maxPathWidth, m_maxPathChange * rand, m_roughness, m_windyness, UnityEngine.Random.Range(m_width / 4, m_width - (m_width / 4) - 1)); } } var cellSize = m_tileMap.m_tileObjects[0].GetComponentInChildren <Renderer>().bounds.size; for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { if (map[x, y] != 0) { var location = new Vector3Int(x, y, 0); m_tiles.Add(location, new Tile(null, Matrix4x4.identity)); } } } for (int x = 0; x < map.GetUpperBound(0); x++) { for (int y = 0; y < map.GetUpperBound(1); y++) { if (map[x, y] != 0) { var location = new Vector3Int(x, y, 0); int xDir = (int)(x * cellSize.x); int zDir = (int)(y * cellSize.z); if (m_flipXAxis == true) { xDir *= -1; } if (m_flipZAxis == true) { zDir *= -1; } var position = new Vector3Int(xDir, 0, zDir); ThreeDTileData threeDTileData = new ThreeDTileData(); m_tileMap.GetTileData(location, this, ref threeDTileData); var go = Instantiate(threeDTileData.gameObject, position, threeDTileData.transform.rotation, m_mapHolder.transform); m_tiles[location].gameObject = go; m_tiles[location].transform = threeDTileData.transform; } } } }
public void GetTileData(Vector3Int location, ITileGrid tileMap, ref ThreeDTileData tileData) { UpdateTile(location, tileMap, ref tileData); }