private char TilemapDataToCharacter(TilemapData tilemapData, bool fullWidth = false) { if (fullWidth) { switch (tilemapData) { default: return '.'; case TilemapData.Wall: return '#'; case TilemapData.Object: return '@'; } } else { switch (tilemapData) { default: return '.'; case TilemapData.Wall: return '#'; case TilemapData.Object: return '@'; } } }
TilemapData GenerateDungeon() { TilemapData data = new TilemapData() { tilePositions = GenericHelper.Vector2IntMap((int)buildingSize, new Vector2Int((int)transform.position.x, (int)transform.position.y)), tiles = new UnityEngine.Tilemaps.TileBase[(int)buildingSize * (int)buildingSize] }; int[,] map = new int[(int)buildingSize, (int)buildingSize]; GenerationHelpers.BuildingGenerator.GenerateOutline(ref map, (int)buildingSize, GenerationHelpers.BuildingGenerator.GenerateDoorPosition((int)buildingSize, Direction.Down), new Vector2Int((int)buildingSize / 2, (int)buildingSize / 2)); GenerationHelpers.BuildingGenerator.GenerateBuilding(ref map, (int)buildingSize - 4, GenerationHelpers.BuildingGenerator.GenerateDoorPosition((int)buildingSize - 4, Direction.Down), new Vector2Int((int)buildingSize / 2, (int)buildingSize / 2)); for (int x = 0; x < (int)buildingSize; x++) { for (int y = 0; y < (int)buildingSize; y++) { Debug.Log(map[x, y]); if (map[x, y] == 1) { data.tiles[y * (int)buildingSize + x] = wall; } else if (map[x, y] == 2) { data.tiles[y * (int)buildingSize + x] = floor; } } } ObjectStore.instance.mapDisplay.DrawVillage(data); return(data); }
private bool IsEmpty(TilemapData tilemapData, int x, int y) { if (x < 0 || y < 0 || x >= tilemapData.Width || y >= tilemapData.Height) { return(true); } return(tilemapData[x, y] == 0); }
public static TilemapData AddTilemapData(string name) { if (TILEMAPDATAS.ContainsKey(name)) { return(TILEMAPDATAS[name]); } TilemapData load = Program.Content.Load <TilemapData>(name); TILEMAPDATAS.Add(name, load); return(load); }
public void DrawTerrain(TilemapData terrain) { ObjectStore store = ObjectStore.instance; foreach (var pos in terrain.tilePositions) { store.terrainMap.SetTileFlags(pos, TileFlags.None); store.terrainMap.SetColor(pos, Color.white); } store.terrainMap.SetTiles(terrain.tilePositions, terrain.tiles); }
public static TilemapData Convert(Tilemap tilemap) { TilemapData newTMD = new TilemapData(); newTMD.size = tilemap.size; Debug.Log(tilemap.size); foreach (var position in tilemap.cellBounds.allPositionsWithin) { newTMD.tiles.Add(tilemap.GetTile(position).name); } return(newTMD); }
protected override IEnumerator Generate(WorldManager worldManager) { villageName = NameGenerator.GenerateVillageName(seed, villageRadius); TilemapData roadData = GenerateRoadGrid(out List <BuildingPoint> points); GenerateBuildings(points); //Display ObjectStore.instance.mapDisplay.DrawVillage(roadData); yield return(null); }
public void Generate(TilemapPrefab prefab) { if (prefab.isSet && prefab != null) { TilemapData data = new TilemapData() { tilePositions = new Vector3Int[prefab.tilePositions.Length], tiles = prefab.tiles }; for (int i = 0; i < prefab.tilePositions.Length; i++) { var oldPos = prefab.tilePositions[i]; Vector3Int newPos; //Rotate switch (direction) { default: case Village.Direction.Up: newPos = oldPos; break; case Village.Direction.Down: newPos = oldPos * -1; break; case Village.Direction.Right: newPos = new Vector3Int(oldPos.y, -oldPos.x, 0); break; case Village.Direction.Left: newPos = new Vector3Int(-oldPos.y, oldPos.x, 0); break; } newPos += Vector3Int.RoundToInt(transform.position); data.tilePositions[i] = newPos; } ObjectStore.instance.mapDisplay.DrawVillage(data); } else { Debug.LogWarning("prefab is not suitable"); } }
/// <summary> /// Initializes the sector. /// </summary> public void InitDefaultSector(TilemapData TileMapData) { for (int I = 0; I < TileMapData.Objects.Count; I++) { CsvData Data = TileMapData.Objects[I].Data; if (Data.Type != 42) { GameObject GameObject = GameObjectFactory.CreateGameObjectByData(Data); GameObject.SetPosition(500 * TileMapData.Objects[I].X, 500 * TileMapData.Objects[I].Y, 0); if (GameObject.Type == 4) { // TODO : GameObject.Type == 4. } } } }
private HashSet <TileSide> CreateTileSides(TilemapData tilemapData) { var sides = new HashSet <TileSide>(); for (int y = 0; y < tilemapData.Height; y++) { for (int x = 0; x < tilemapData.Width; x++) { int tile = tilemapData[x, y]; if (tile == 0) { // empty tile continue; } int realY = tilemapData.Height - y - 1; if (this.IsEmpty(tilemapData, x - 1, y)) { sides.Add(new TileSide(new Vector2i(x, realY + 1), Direction2D.Left)); } if (this.IsEmpty(tilemapData, x + 1, y)) { sides.Add(new TileSide(new Vector2i(x, realY + 1), Direction2D.Right)); } if (this.IsEmpty(tilemapData, x, y - 1)) { sides.Add(new TileSide(new Vector2i(x, realY + 1), Direction2D.Up)); } if (this.IsEmpty(tilemapData, x, y + 1)) { sides.Add(new TileSide(new Vector2i(x, realY - 1), Direction2D.Down)); } } } return(sides); }
public void DrawCollidableDetail(TilemapData data) { ObjectStore.instance.detailMap2.SetTiles(data.tilePositions, data.tiles); }
//Draws the tiles to a tilemap public void DrawVillage(TilemapData villageData) { ObjectStore.instance.villageMap.SetTiles(villageData.tilePositions, villageData.tiles); }
protected override IEnumerator Generate(WorldManager worldManager) { largeBuildingsToGenerate = new List <Building>(); if (rand == null) { rand = new System.Random(seed + (int)transform.position.x + (int)transform.position.y); } //Generate the maps var roadMap = GenerateRoads(); var buildingMap = GenerateBuildings(roadMap); //Initiate the tilemap data TilemapData village = new TilemapData(); List <Vector3Int> positions = new List <Vector3Int>(); List <TileBase> tiles = new List <TileBase>(); //Implement the tilemap data for (int y = 0; y < BorderDimension; y++) { for (int x = 0; x < BorderDimension; x++) { if (roadMap[x, y] > 0 || buildingMap[x, y] > 0) { var offset = BorderDimension / 2; positions.Add(new Vector3Int((int)transform.position.x + x - offset, (int)transform.position.y + y - offset, 0)); if (roadMap[x, y] == 1) { tiles.Add(majorRoadTile); } else if (roadMap[x, y] == 2) { tiles.Add(minorRoadTile); } else if (buildingMap[x, y] == 1 || buildingMap[x, y] == 4) { tiles.Add(wall); } else if (buildingMap[x, y] == 2 || buildingMap[x, y] == 5) { tiles.Add(floor); } } } } village.tiles = tiles.ToArray(); village.tilePositions = positions.ToArray(); villageData = village; if (drawVillage) { FindObjectOfType <MapDisplay>().DrawVillage(villageData); foreach (var building in largeBuildingsToGenerate) { building.Initialise(worldManager); DestroyImmediate(building.gameObject); } } yield return(null); }
public void SetData(int x, int y, TilemapData value) => tilemapDatas[y, x] = value;
//private Vector3 GetEndPositionOfMap(string mapName) //{ // foreach (var data in MapDatas) // { // int index = 0; // foreach (var nextMapName in data.Value.NextMapName) // { // if (nextMapName == CurrentMapName) // { // return data.Value.PlayerEndPosition[index]; // } // index++; // } // } // return Vector3.zero; //} #endregion //Tilemap 정보갱신 private Tilemap UpdateTilemapDataWithCreate(TilemapData mapData) { var maps = transform.GetComponentsInChildren <Tilemap>(); foreach (var m in maps) { //자식중에 같은게 있으면 리턴 if (m.name == mapData.Name) { return(m); } } //없으면 새로만들어서 정보업데이트후 리턴 GameObject go = new GameObject(); go.transform.SetParent(transform); //데이터들 오브젝트와 연결 TilemapRendererLinking(mapData.TilemapRenderer, go); TilemapColliderLinking(mapData.TilemapCollider, go); Rigidbody2DLinking(mapData.RigidBody2D, go); CompositeColliderLinking(mapData.CompositeCollider, go); PlatformEffectorLinking(mapData.PlatformEffector, go); BoxCollider2DLinking(mapData.BoxCollider, go); Tilemap map = go.GetComponent <Tilemap>(); //정보 업데이트 //데이터테이블 변경시 같이 변경해야함 if (string.IsNullOrEmpty(mapData.Name)) { Log.PrintWarning("MapData Name is NULL or Empty"); } else { map.transform.name = mapData.Name; } if (string.IsNullOrEmpty(mapData.Tag)) { Log.PrintWarning("MapData Tag is NULL or Empty"); } else { map.tag = mapData.Tag; } if (LayerMask.NameToLayer(mapData.Tag) == -1) { map.gameObject.layer = 0; } else { map.gameObject.layer = LayerMask.NameToLayer(mapData.Tag); } map.transform.position = mapData.Position; map.transform.rotation = mapData.Rotation; map.transform.localScale = mapData.Scale; map.animationFrameRate = mapData.AnimationFrameRate; map.color = mapData.Color; map.tileAnchor = mapData.TileAnchor; map.orientation = mapData.Orientation; return(map); }
protected override IEnumerator Generate(WorldManager worldManager) { UIManager.UpdateLoadScreenText("Placing trees and rocks."); TilemapData data = new TilemapData(); System.Random rand = new System.Random(seed); var worldDimension = (int)worldManager.worldSize; switch (generationMethod) { case GenerationMethod.Random: RandomPlacement(); break; case GenerationMethod.Grid: GridPlacement(); break; } yield return(null); ObjectStore.instance.mapDisplay.DrawCollidableDetail(data); FinishGenerating(worldManager); void RandomPlacement() { List <Vector3Int> positions = new List <Vector3Int>(); List <TileBase> tiles = new List <TileBase>(); int number = 0; int attempts = 0; while (number < maxNumberOfDecorations && attempts < 50) { bool canPlace = false; Vector3Int potentialPosition = new Vector3Int(rand.Next(0, worldDimension - 1), rand.Next(0, worldDimension - 1), 0); if (ObjectStore.instance.villageMap.GetTile(potentialPosition) != null) { continue; } BiomeType currentBiome; foreach (var biome in allowedBiomes) { if (ObjectStore.instance.terrainMap.GetTile(potentialPosition) == biome.tile) { currentBiome = biome; canPlace = true; if (!positions.Contains(potentialPosition)) { positions.Add(potentialPosition); TileBase tile = currentBiome.GetDecorTile(rand, false, -1); tiles.Add(tile); attempts = 0; number++; } break; } } if (!canPlace) { attempts++; } } data.tilePositions = positions.ToArray(); data.tiles = tiles.ToArray(); } void GridPlacement() { List <Vector3Int> positions = new List <Vector3Int>(); List <TileBase> tiles = new List <TileBase>(); Village[] villages = FindObjectsOfType <Village>(); const int checkFrequency = 2; int currentCheckCount = 0; float currentDistanceToVillage = GetDistanceToNearestVillage(villages, Vector3Int.zero); for (int x = 0; x < worldDimension - minDetailDistanceInGrid; x += minDetailDistanceInGrid) { for (int y = 0; y < worldDimension - minDetailDistanceInGrid; y += minDetailDistanceInGrid) { var dist = (minDetailDistanceInGrid - 1) / 2; if (++currentCheckCount >= checkFrequency) { currentDistanceToVillage = GetDistanceToNearestVillage(villages, new Vector3Int(x, y, 0)); currentCheckCount = 0; } Vector3Int randomOffset = new Vector3Int(rand.Next(-dist, dist), rand.Next(-dist, dist), 0); Vector3Int potentialPosition = new Vector3Int(x, y, 0) + randomOffset; if (ObjectStore.instance.villageMap.GetTile(potentialPosition) != null) { continue; } TileBase currentTile = ObjectStore.instance.terrainMap.GetTile(potentialPosition); BiomeType currentBiome; foreach (var biome in allowedBiomes) { if (currentTile == biome.tile) { currentBiome = biome; positions.Add(potentialPosition); TileBase tile = currentBiome.GetDecorTile(rand, true, currentDistanceToVillage); tiles.Add(tile); break; } } } } data.tilePositions = positions.ToArray(); data.tiles = tiles.ToArray(); } }
public void CreateFrom(TilemapData tilemapData, TilesetManager tilesetManager) { this.TilemapData = tilemapData; this.TilesetManager = tilesetManager; this.Create(); }