public void SetColor(E_TileType type) { Sprite selected = null; switch (type) { case E_TileType.BlueStone: selected = blue; break; case E_TileType.GreenStone: selected = green; break; case E_TileType.RedStone: selected = red; break; case E_TileType.YellowStone: selected = yellow; break; default: break; } img.overrideSprite = selected; }
public Block(int x, int y, E_TileType tileType, MonoBlock monoBlock) { mapPosition.x = x; mapPosition.y = y; this.monoBlock = monoBlock; this.tileType = tileType; this.monoBlock.parent = this; }
private Block GenerateNewBlock(int y, int x) { //generate game object var newMonoBlock = Instantiate(blockPrefab, this.transform, true); //createdObject.mapPosition = new POSITION(x, y); newMonoBlock.transform.position = Util.ConvertMapCoordToWorldCoord(new POSITION(x, y), this); newMonoBlock.SetSize(cellSize.x, cellSize.y); E_TileType randomType = (E_TileType)UnityEngine.Random.Range(0, (int)E_TileType.Count); newMonoBlock.TileType = randomType; newMonoBlock.name = $"Block_{x}_{y}"; //generate block object Block newBlock = new Block(x, y, randomType, newMonoBlock); return(newBlock); }
//generate a room from a wall on its dir bool CreateRoomFromMapElement(Transform mapElem, E_RoomType roomType, E_TileMaterialType tileMaterialType, int index = -1) { List <Transform> chosenWalls = mapElem.GetComponent <MapElement> ().walls; Transform testedWall; if (index == -1) { testedWall = chosenWalls[Random.Range(0, chosenWalls.Count - 1)]; } else { testedWall = chosenWalls[index]; } int wallX = Mathf.RoundToInt(testedWall.position.x) / tileWidth; int wallZ = Mathf.RoundToInt(testedWall.position.z) / tileWidth; int deltaX, deltaZ; E_Direction WallDir = CheckWallDirection(testedWall); //corridor direction if (WallDir == E_Direction.Unset) { return(false); } switch (WallDir) { case E_Direction.Up: deltaX = 0; deltaZ = 1; break; case E_Direction.Down: deltaX = 0; deltaZ = -1; break; case E_Direction.Left: deltaX = -1; deltaZ = 0; break; case E_Direction.Right: deltaX = 1; deltaZ = 0; break; default: deltaX = 0; deltaZ = 0; return(false); } int roomWidth = Random.Range(minRoomWidth, maxRoomWidth); int roomLength = Random.Range(minRoomLength, maxRoomLength); int roomStartX, roomStartZ, doorDistance; //calculate room startpos and doorpos 根据门离两边的距离算出房间的起始位置 if (deltaX == 0) { doorDistance = Random.Range(1, roomWidth - 2); //避开房间拐角 roomStartX = wallX - doorDistance; if (deltaZ == 1) { roomStartZ = wallZ + deltaZ; } else { roomStartZ = wallZ + deltaZ * roomLength; } } else { doorDistance = Random.Range(1, roomLength - 2); roomStartZ = wallZ - doorDistance; if (deltaX == 1) { roomStartX = wallX + deltaX; } else { roomStartX = wallX + deltaX * roomWidth; } } //calculate a small entrance corridor space and offset roompos //random corridor length int corridorEntranceLength = Random.Range(1, 7); int corridorStartX = wallX + deltaX; int corridorStartZ = wallZ + deltaZ; //offset room roomStartX += deltaX * corridorEntranceLength; roomStartZ += deltaZ * corridorEntranceLength; //material type Transform[] floorTiles, wallTiles; SetTileMaterial(tileMaterialType, out floorTiles, out wallTiles); //create room entrance corridor if (CreateCorridor(corridorStartX, corridorStartZ, corridorEntranceLength, WallDir, tileMaterialType)) { layerCorridors[layerCorridors.Count - 1].GetComponent <MapElement>().removedWall = RemoveWallFromMapElement(wallX + deltaX * corridorEntranceLength, wallZ + deltaZ * corridorEntranceLength, deltaX, deltaZ, layerCorridors[layerCorridors.Count - 1].GetComponent <MapElement>()); } else { return(false); } //为了使房间能顺利建立将入口处layerTile临时替换为empty E_TileType tmpTileType = layerTile [wallX + deltaX * corridorEntranceLength, wallZ + deltaZ * corridorEntranceLength]; layerTile [wallX + deltaX * corridorEntranceLength, wallZ + deltaZ * corridorEntranceLength] = E_TileType.empty; //create room if (CreateRoom(roomStartX, roomStartZ, roomWidth, roomLength, roomType, tileMaterialType)) { RemoveWallFromMapElement(wallX + deltaX * (corridorEntranceLength + 1), wallZ + deltaZ * (corridorEntranceLength + 1), -deltaX, -deltaZ, layerRooms[layerRooms.Count - 1].GetComponent <MapElement>()); } else { GameObject recentCorridorGO = layerCorridors[layerCorridors.Count - 1].gameObject; CancelRecentMapElement(E_MapElementType.Corridor, recentCorridorGO); return(false); } //入口处恢复 layerTile [wallX + deltaX * corridorEntranceLength, wallZ + deltaZ * corridorEntranceLength] = tmpTileType; mapElem.GetComponent <MapElement>().exitCount++; chosenWalls.Remove(testedWall); Destroy(testedWall.gameObject); return(true); }
HashSet <POSITION> GetRemovedPositions() { willBeRemovedBlocks.Clear(); //check line //will be optimized positions.Clear(); for (int y = 0; y < mg.Height; y++) { for (int x = 0; x < mg.Width; x++) { if (null == mg.Map[y, x]) { continue; } positions.Add(mg.Map[y, x].GetPosition()); } } for (int i = 0; i < positions.Count; i++) { for (int x = 0; x < mg.Width - 2; ++x) { POSITION px1 = new POSITION(x, positions[i].y); POSITION px2 = new POSITION(x + 1, positions[i].y); POSITION px3 = new POSITION(x + 2, positions[i].y); if (null == mg.Map[px1.y, px1.x] || null == mg.Map[px2.y, px2.x] || null == mg.Map[px3.y, px3.x]) { continue; } //yield return new WaitForSeconds(debugDelay); mg.Map[px1.y, px1.x].SetMarking(true); mg.Map[px2.y, px2.x].SetMarking(true); mg.Map[px3.y, px3.x].SetMarking(true); E_TileType t1 = mg.Map[px1.y, px1.x].GetTileType(); E_TileType t2 = mg.Map[px2.y, px2.x].GetTileType(); E_TileType t3 = mg.Map[px3.y, px3.x].GetTileType(); if (t1 == t2 && t1 == t3) { willBeRemovedBlocks.Add(px1); willBeRemovedBlocks.Add(px2); willBeRemovedBlocks.Add(px3); } //yield return new WaitForSeconds(debugDelay); mg.Map[px1.y, px1.x].SetMarking(false); mg.Map[px2.y, px2.x].SetMarking(false); mg.Map[px3.y, px3.x].SetMarking(false); } //check column for (int y = 0; y < mg.Height - 2; ++y) { POSITION p1 = new POSITION(positions[i].x, y); POSITION p2 = new POSITION(positions[i].x, y + 1); POSITION p3 = new POSITION(positions[i].x, y + 2); if (null == mg.Map[p1.y, p1.x] || null == mg.Map[p2.y, p2.x] || null == mg.Map[p3.y, p3.x]) { continue; } //yield return new WaitForSeconds(debugDelay); mg.Map[p1.y, p1.x].SetMarking(true); mg.Map[p2.y, p2.x].SetMarking(true); mg.Map[p3.y, p3.x].SetMarking(true); E_TileType t1 = mg.map[p1.y, p1.x].GetTileType(); E_TileType t2 = mg.map[p2.y, p2.x].GetTileType(); E_TileType t3 = mg.map[p3.y, p3.x].GetTileType(); if (t1 == t2 && t1 == t3) { willBeRemovedBlocks.Add(p1); willBeRemovedBlocks.Add(p2); willBeRemovedBlocks.Add(p3); } //yield return new WaitForSeconds(debugDelay); mg.Map[p1.y, p1.x].SetMarking(false); mg.Map[p2.y, p2.x].SetMarking(false); mg.Map[p3.y, p3.x].SetMarking(false); } } return(willBeRemovedBlocks); }
public void SetTileType(E_TileType tileType) { this.tileType = tileType; }