public void InitFoW(Map map_) { map = map_; if (fogOfWarPlane != null) Destroy(fogOfWarPlane); string directory = "FogOfWar/PRE_FogOfWarPlane"; GameObject tempPlane = Resources.Load(directory) as GameObject; fogOfWarPlane = (GameObject)Instantiate(tempPlane); fogTexture = new Texture2D(map.size_X, map.size_Y); for (int i = 0; i < map.size_X; i++) for (int j = 0; j < map.size_Y; j++) fogTexture.SetPixel(i, j, unvisited); fogTexture.Apply(); fogTexture.filterMode = filterMode; //previewTexture = dataLayer.ConvertMapToTexture(map, false); if (ProDManager.Instance.topDown) { fogOfWarPlane.transform.localScale = new Vector3(map.size_X, layer, map.size_Y); fogOfWarPlane.transform.position = new Vector3(map.size_X / 2, layer, map.size_Y / 2); } else { fogOfWarPlane.transform.Rotate(-90f, 0.0f, 0.0f, Space.World); fogOfWarPlane.transform.localScale = new Vector3(map.size_X, layer, map.size_Y); fogOfWarPlane.transform.position = new Vector3(map.size_X / 2, map.size_Y / 2, -layer); } fogOfWarPlane.GetComponent<Renderer>().material.SetTexture("_MainTex", fogTexture); fogOfWarPlane.GetComponent<Renderer>().material.color = Color.white; }
public PathfindingAlgorithm(Map map) { walkableCellTypes = new List<string>(); _map = map; _world = _map.cellsOnMap; _openList = new Heap<Cell>(); _closedList = new List<Cell>(); }
public Map ConvertTextureToMap(Texture2D mapTexture) { Map result = new Map(mapTexture.width, mapTexture.height); for (int i = 0; i < result.size_X; i++) { for (int j = 0; j < result.size_Y; j++) { Color c = mapTexture.GetPixel(i, j); result.cellsOnMap[i, j].SetCellType(getCellType(c)); } } return result; }
public List<Cell> GetCellsInRoom(Map map) { if (cellsInRoom != null && cellsInRoom.Count > 0) return cellsInRoom; else if (Type == RoomType.Square) { cellsInRoom = new List<Cell>(); for (int j = roomStart_Y + 1; j < roomEnd_Y; j++) for (int i = roomStart_X + 1; i < roomEnd_X; i++) cellsInRoom.Add(map.cellsOnMap[i, j]); return cellsInRoom; } else return new List<Cell>(); }
public Texture2D ConvertMapToTexture(Map map, bool forcePowerOfTwo) { Texture2D generatedTexture = null; if (forcePowerOfTwo) { int powerOfTwo_X = 1; int powerOfTwo_Y = 1; while (powerOfTwo_X < map.size_X) powerOfTwo_X *= 2; while (powerOfTwo_Y < map.size_Y) powerOfTwo_Y *= 2; generatedTexture = new Texture2D(powerOfTwo_X, powerOfTwo_Y, TextureFormat.ARGB32, false); } else { generatedTexture = new Texture2D(map.size_X, map.size_Y, TextureFormat.ARGB32, false); } generatedTexture.filterMode = FilterMode.Point; //Set pixels and apply it to the mapPlane. //Debug.Log("tempCellsInMap is at " + i + " " + j + " in world map array."); for (int i = 0; i < map.size_X; i++) { for (int j = 0; j < map.size_Y; j++) { generatedTexture.SetPixel ( i, j, getCellColor(map.cellsOnMap[i, j].type) ); if (map.cellsOnMap[i, j].type.Equals("Abyss")) { generatedTexture.SetPixel ( i, j, Color.clear ); } } } generatedTexture.Apply(); return generatedTexture; }
public void InitPathfinding(Map map_) { if (pathPlane != null) Destroy(pathPlane); map = map_; string directory = "Pathfinding/PRE_PathfindingPlane"; GameObject tempPlane = Resources.Load(directory) as GameObject; pathPlane = (GameObject)Instantiate(tempPlane); pathTexture = new Texture2D(map.size_X, map.size_Y); for (int i = 0; i < map.size_X; i++) for (int j = 0; j < map.size_Y; j++) pathTexture.SetPixel(i, j, emptyColor); pathTexture.Apply(); pathTexture.filterMode = filterMode; if (ProDManager.Instance.topDown) { pathPlane.transform.localScale = new Vector3(map.size_X, layer, map.size_Y); pathPlane.transform.position = new Vector3(map.size_X / 2, layer, map.size_Y / 2); } else { pathPlane.transform.Rotate(-90f, 0.0f, 0.0f, Space.World); pathPlane.transform.localScale = new Vector3(map.size_X, layer, map.size_Y); pathPlane.transform.position = new Vector3(map.size_X / 2, map.size_Y / 2, -layer); } pathPlane.renderer.material.SetTexture("_MainTex", pathTexture); pathPlane.renderer.material.color = Color.white; algorithm = new PathfindingAlgorithm(map); algorithm.walkableCellTypes = walkableTypes; path = new Stack<Cell>(); InputManager.Instance.pathFinding = this; isWalking = false; }
/// <summary> /// Gets the world position of an address on a specified map. /// </summary> /// <returns> /// The world position. /// </returns> /// <param name='map'> /// The map that the address is on. /// </param> /// <param name='mapAddress'> /// Address on the map you want the world position of. /// </param> public Address GetAddressWorldPosition(Map map, Address mapAddress) { Address result = mapAddress; if (_Maps == null) return result; else { Address tempAddress = map.addressOnWorldMap; int tempX = 0; for (int i = 0; i < tempAddress.x; i++) tempX += _Maps[i, 0].size_X; int tempY = 0; for (int j = 0; j < tempAddress.y; j++) tempY += _Maps[0, j].size_Y; result = new Address(tempX + mapAddress.x, tempY + mapAddress.y); } return result; }
public static WorldMap Generate(List<string> mapTypes, int map_Size_X, int map_Size_Y) { int tempX = 1; int tempY = 1; while (tempX * tempY < mapTypes.Count) { tempX++; if (tempX * tempY < mapTypes.Count) tempY++; } _worldMap_Size_X = tempX; _worldMap_Size_Y = tempY; _map_Size_X = map_Size_X; _map_Size_Y = map_Size_Y; worldMap = new WorldMap(_worldMap_Size_X, _worldMap_Size_Y); int typeIndex = 0; for (int j = 0; j < _worldMap_Size_Y; j++) { for (int i = 0; i < _worldMap_Size_X; i++) { string generatorName = "Empty"; if (typeIndex < mapTypes.Count) generatorName = mapTypes[typeIndex]; Map tempMap = new Map(); //tempMap = Generator_Dungeon.Generate(); switch (generatorName) { case "Cavern": Generator_Cavern.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Cavern.Generate(); break; case "Dungeon": Generator_Dungeon.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Dungeon.Generate(); break; case "DungeonRuins": Generator_DungeonRuins.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_DungeonRuins.Generate(); break; case "Hill": Generator_RockyHill.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_RockyHill.Generate(); break; case "Maze": Generator_Maze.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Maze.Generate(); break; case "ObstacleBiome": Generator_ObstacleBiome.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_ObstacleBiome.Generate(); break; case "PerlinLikeBiome": Generator_PerlinLikeBiome.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_PerlinLikeBiome.Generate(); break; case "StickBiome": Generator_StickDungeon.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_StickDungeon.Generate(); break; default: Generator_EmptyMap.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_EmptyMap.Generate(); break; } tempMap.addressOnWorldMap = new Address(i,j); tempMap.worldMap = worldMap; worldMap.maps[i,j] = tempMap; typeIndex++; } } return worldMap; }
private bool isOpaque(Map map, Address origin, int x, int y, int octant) { Address temp = TranslateOctant(new Address(x, y), octant); x = temp.x; y = temp.y; return opaqueCells.Contains(map.cellsOnMap[origin.x + x, origin.y + y].type); }
private List<Address> checkColumn(ColumnPortion cp, Address origin, Map map, Queue<ColumnPortion> queue, int octant, int range) { List<Address> result = new List<Address>(); DirectionVector topVector = cp.TopVector; DirectionVector bottomVector = cp.BottomVector; int x = cp.X; int topY; if (cp.X == 0) topY = 0; else { int quotient = (2 * cp.X + 1) * cp.TopVector.Y / (2 * cp.TopVector.X); int remainder = (2 * cp.X + 1) * cp.TopVector.Y % (2 * cp.TopVector.X); topY = quotient; if (remainder > cp.TopVector.X) topY = quotient++; } int bottomY; if (cp.X == 0) bottomY = 0; else { int quotient = (2 * cp.X - 1) * cp.BottomVector.Y / (2 * cp.BottomVector.X); int remainder = (2 * cp.X - 1) * cp.BottomVector.Y % (2 * cp.BottomVector.X); bottomY = quotient; if (remainder >= cp.BottomVector.X) bottomY = quotient++; } bool? wasLastCellOpaque = null; for (int y = topY; y >= bottomY; --y) { bool inRadius = IsInRadius(x, y, range); if (inRadius) { Address temp = TranslateOctant(new Address(x, y), octant); // The current cell is in the field of view. result.Add(new Address(origin.x + temp.x, origin.y + temp.y)); } // A cell that was too far away to be seen is effectively // an opaque cell; nothing "above" it is going to be visible // in the next column, so we might as well treat it as // an opaque cell and not scan the cells that are also too // far away in the next column. bool currentIsOpaque = !inRadius || isOpaque(map, origin, x, y, octant); if (wasLastCellOpaque != null) { if (currentIsOpaque) { // We've found a boundary from transparent to opaque. Make a note // of it and revisit it later. if (!wasLastCellOpaque.Value) { // The new bottom vector touches the upper left corner of // opaque cell that is below the transparent cell. queue.Enqueue(new ColumnPortion( x + 1, new DirectionVector(x * 2 - 1, y * 2 + 1), topVector)); } } else if (wasLastCellOpaque.Value) { // We've found a boundary from opaque to transparent. Adjust the // top vector so that when we find the next boundary or do // the bottom cell, we have the right top vector. // // The new top vector touches the lower right corner of the // opaque cell that is above the transparent cell, which is // the upper right corner of the current transparent cell. topVector = new DirectionVector(x * 2 + 1, y * 2 + 1); } } wasLastCellOpaque = currentIsOpaque; } // Make a note of the lowest opaque-->transparent transition, if there is one. if (wasLastCellOpaque != null && !wasLastCellOpaque.Value) queue.Enqueue(new ColumnPortion(x + 1, bottomVector, topVector)); return result; }
private List<Address> findVisibleCellsColumn(Map map, Address position, int range) { List<Address> visibleCells = new List<Address>(); for (int i = 0; i < 8; i++) { Queue<ColumnPortion> workQueue = new Queue<ColumnPortion>(); workQueue.Enqueue(new ColumnPortion(0, new DirectionVector(1, 0), new DirectionVector(1, 1))); while (workQueue.Count > 0) { ColumnPortion currentPortion = workQueue.Dequeue(); visibleCells.AddRange(checkColumn(currentPortion, position, map, workQueue, i, range)); } } return visibleCells; }
private static List<Address> CastRay(int x0, int y0, int x1, int y1, Map map, List<string> types) { // Optimization: it would be preferable to calculate in // advance the size of "result" and to use a fixed-size array // instead of a list. List<Address> result = new List<Address>(); bool steep = false; if (Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0)) steep = true; //if (steep) { // Swap(ref x0, ref y0); // Swap(ref x1, ref y1); //} //if (x0 > x1) { // Swap(ref x0, ref x1); // Swap(ref y0, ref y1); //} int deltax = Mathf.Abs(x1 - x0); int deltay = Mathf.Abs(y1 - y0); int error = 0; int ystep; int xstep; int x = x0; int y = y0; if (x0 < x1) xstep = 1; else xstep = -1; if (y0 < y1) ystep = 1; else ystep = -1; if (!steep) { for (int i = 0; i <= deltax; i++) { if (map.Contains(x, y) == false) return result; result.Add(new Address(x, y)); if (types.Contains(map.cellsOnMap[x, y].type)) return result; x += xstep; error += deltay; if (2 * error >= deltax) { y += ystep; error -= deltax; } } } else { for (int i = 0; i <= deltay; i++) { if (map.Contains(x, y) == false) return result; result.Add(new Address(x, y)); if (types.Contains(map.cellsOnMap[x, y].type)) return result; y += ystep; error += deltax; if (2 * error >= deltay) { x += xstep; error -= deltay; } } } return result; }
private List<Address> findVisibleCellsRayRound(Map map, Address position, int range) { List<Address> result = new List<Address>(); //bresenham circle: //set up the values needed for the algorithm int f = 1 - range; //used to track the progress of the drawn circle (since its semi-recursive) int ddFx = 1; //step x int ddFy = -2 * range; //step y int x = 0; int y = range; //this algorithm doesn't account for the farthest points, //so we have to set them manually result.AddRange(CastRay(position.x, position.y, position.x + range, position.y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - range, position.y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x, position.y + range, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x, position.y - range, map, opaqueCells)); while (x < y) { if (f >= 0) { y--; ddFy += 2; f += ddFy; } x++; ddFx += 2; f += ddFx; //build our current arc result.AddRange(CastRay(position.x, position.y, position.x + x, position.y + y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - x, position.y + y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + x, position.y - y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - x, position.y - y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + y, position.y + x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - y, position.y + x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + y, position.y - x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - y, position.y - x, map, opaqueCells)); } return result; }
private List<Address> findVisibleCellsFlood(Map map, Address position, int range) { int arrSize = range * 2 + 1; int[,] arr = new int[arrSize, arrSize]; for (int i = 0; i < arrSize; i++) for (int j = 0; j < arrSize; j++) arr[i, j] = range + 1; //init array with high distance arr[range, range] = 0; // center cell is target Stack<Address> stack = new Stack<Address>(); stack.Push(new Address(range, range)); while (stack.Count != 0) { Address current = stack.Pop(); Address mapAddress = new Address(position.x + current.x - range, position.y + current.y - range); if (!map.Contains(mapAddress) || opaqueCells.Contains(map.cellsOnMap[mapAddress.x, mapAddress.y].type)) continue; int newDist = arr[current.x, current.y] + 1; if (current.x > 0 && newDist < arr[current.x - 1, current.y]) { stack.Push(new Address(current.x - 1, current.y)); arr[current.x - 1, current.y] = newDist; } if (current.x < arrSize - 1 && newDist < arr[current.x + 1, current.y]) { stack.Push(new Address(current.x + 1, current.y)); arr[current.x + 1, current.y] = newDist; } if (current.y > 0 && newDist < arr[current.x, current.y - 1]) { stack.Push(new Address(current.x, current.y - 1)); arr[current.x, current.y - 1] = newDist; } if (current.y < arrSize - 1 && newDist < arr[current.x, current.y + 1]) { stack.Push(new Address(current.x, current.y + 1)); arr[current.x, current.y + 1] = newDist; } } List<Address> result = new List<Address>(); for (int i = 0; i < arrSize; i++) { for (int j = 0; j < arrSize; j++) { Address mapAddress = new Address(position.x + i - range, position.y + j - range); if (map.Contains(mapAddress) && arr[i, j] <= range) result.Add(mapAddress); } } return result; }
private static void PrepareMap() { map = new Map(map_Size_X, map_Size_Y); map.theme = theme; }
public static WorldMap Generate(string generatorName, int world_Size_X, int world_Size_Y, int map_Size_X, int map_Size_Y) { _worldMap_Size_X = world_Size_X; _worldMap_Size_Y = world_Size_Y; _map_Size_X = map_Size_X; _map_Size_Y = map_Size_Y; worldMap = new WorldMap(_worldMap_Size_X, _worldMap_Size_Y); for (int i = 0; i < _worldMap_Size_X; i++) { for (int j = 0; j < _worldMap_Size_Y; j++) { Map tempMap = new Map(); //tempMap = Generator_Dungeon.Generate(); switch (generatorName) { case "Cavern": Generator_Cavern.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Cavern.Generate(); break; case "Dungeon": Generator_Dungeon.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Dungeon.Generate(); break; case "AlternativeDungeon": Generator_AlternativeDungeon.SetGenericProperties(_map_Size_X, _map_Size_Y, theme); tempMap = Generator_AlternativeDungeon.Generate(); break; case "DungeonRuins": Generator_DungeonRuins.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_DungeonRuins.Generate(); break; case "RockyHill": Generator_RockyHill.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_RockyHill.Generate(); break; case "Maze": Generator_Maze.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Maze.Generate(); break; case "ObstacleBiome": Generator_ObstacleBiome.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_ObstacleBiome.Generate(); break; case "PerlinLikeBiome": Generator_PerlinLikeBiome.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_PerlinLikeBiome.Generate(); break; case "StickDungeon": Generator_StickDungeon.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_StickDungeon.Generate(); break; case "StickBiome": Generator_StickBiome.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_StickBiome.Generate(); break; case "RoundRooms": Generator_RoundRooms.SetGenericProperties(_map_Size_X, _map_Size_Y, theme); tempMap = Generator_RoundRooms.Generate(); break; case "DwarfTown": Generator_DwarfTown.SetGenericProperties(_map_Size_X, _map_Size_Y, theme); tempMap = Generator_DwarfTown.Generate(); break; case "Castle": Generator_Castle.SetGenericProperties(_map_Size_X, _map_Size_Y, theme); tempMap = Generator_Castle.Generate(); break; default: Generator_Maze.SetGenericProperties(_map_Size_X,_map_Size_Y, theme); tempMap = Generator_Maze.Generate(); break; } tempMap.addressOnWorldMap = new Address(i,j); tempMap.worldMap = worldMap; worldMap.maps[i,j] = tempMap; } } return worldMap; }
private List<Address> findVisibleCellsRaySquare(Map map, Address position, int range) { List<Address> result = new List<Address>(); for (int i = position.x - range; i <= position.x + range; i++) result.AddRange(CastRay(position.x, position.y, i, position.y + range, map, opaqueCells)); for (int i = position.y + range; i >= position.y - range; i--) result.AddRange(CastRay(position.x, position.y, position.x + range, i, map, opaqueCells)); for (int i = position.x + range; i >= position.x - range; i--) result.AddRange(CastRay(position.x, position.y, i, position.y - range, map, opaqueCells)); for (int i = position.y - range; i <= position.y + range; i++) result.AddRange(CastRay(position.x, position.y, position.x - range, i, map, opaqueCells)); return result; }