protected virtual IEnumerator WriteTestOutput(ArrayGrid <MapElement> map) { string testOutputPath = "Assets/common/lib/MapGeneration/"; Texture2D debugOutput = map.ToTexture(WriteColor); yield return(WriteToFile(debugOutput, testOutputPath + this.GetType().Name + ".png")); }
public IEnumerator Create1000by1000andPathFrom0_0to999_999AndWriteToPNGFile() { float size = 1000f; var mapRect = new Rect(-size / 2f, -size / 2f, size, size); var map = new ArrayGrid <int>(mapRect.size.ToInt()); var start = new Vector2Int(0, 0); var end = map.MaxValidPosition; bool searchDiagonal = true; bool debug = false; AStar pathFinder = new AStar(); yield return(pathFinder.FindPath(map, start, end, null, searchDiagonal, debug)); Assert.That(pathFinder.result != null, Is.True, "Path failed to generate."); var path = pathFinder.result; map.SetElements(path, 3); Texture2D mapTex; mapTex = map.ToTexture(WriteColor); yield return(WriteToFile(mapTex, testOutputPath + "1000by1000BasicTestOutputPath.png")); Assert.That(File.Exists(testOutputPath + "1000by1000BasicTestOutputPath.png"), Is.True, "Failed to write test output to file."); yield break; }
IEnumerator Start() { //yield return ReadFromFile(Application.dataPath + "/TestMap.png"); if (mapTex == null) { float size = 100f; mapRect = new Rect(-size / 2f, -size / 2f, size, size); List <Vector2Int> startPositions = new List <Vector2Int>() { new Vector2Int(10, 0), new Vector2Int(50, 98) }; yield return(Setup(mapRect, startPositions)); yield return(Map()); //path finding code //if(false) { List <Vector2Int> path = new List <Vector2Int>(); //FindAPath<int> pathFinder = new FindAPath<int>(); //Node start = new Node(startPositions[0], (int)startPositions[0].x, (int)startPositions[0].y, 1f); //Node end = new Node(startPositions[1], (int)startPositions[1].x, (int)startPositions[1].y, 1f); debugMap = new ArrayGrid <int>(map); //path = pathFinder.GetPath(map, start, end).Select(x=> x.localPosition).ToList(); AStar pathFinder = new AStar(); pathFinder.throttle = 100; //pathFinder.throttleIterations = 100; IEnumerator runningPathFinder = pathFinder.FindPath(map, startPositions[0], startPositions[1], null, true, true); runningPathFinder.MoveNext(); while (pathFinder.result == null) { //clear previous path //map.SetElements(path, 0); //get next path runningPathFinder.MoveNext(); if (pathFinder.debugPath != null) { mapTex = debugMap.ToTexture(WriteColor); debugMap.SetElements(pathFinder.debugPath, 2); UpdateSprite(); } yield return(new WaitForEndOfFrame()); } path = pathFinder.result; map.SetElements(path, 3); } mapTex = map.ToTexture(WriteColor); //yield return WriteToFile(Application.dataPath + "/TestMap.png"); } UpdateSprite(); yield break; }
IEnumerator Setup(Rect mapBounds, List <Vector2Int> startPositions) { bounds = mapBounds; map = new ArrayGrid <int>(mapBounds.size.ToInt()); LayerMask wallMask = new LayerMask(); wallMask.value = 8; //TODO: update this with the correct value for walls float raycastDistance = cellSize; //List<Vector2> visited = new List<Vector2>(); int throttle = 0; for (int j = 0; j < (int)map.h; ++j) { for (int i = 0; i < (int)map.w; ++i) { throttle++; if (throttle % setupThrottle == 0) { mapTex = map.ToTexture(WriteColor); UpdateSprite(); yield return(new WaitForEndOfFrame()); } Vector2Int localFrom = new Vector2Int(i, j); Vector2 from = MapToWorldPosition(localFrom); List <Vector2Int> nearby = map.GetAdjacentPositions(localFrom, false); //if(nearby == null) // continue; bool isInWall = true; foreach (Vector2Int v in nearby) { Vector2 worldV = MapToWorldPosition(v); Vector2 to = worldV; //TODO: use wallmask.... if (Physics2D.Raycast(from, (to - from).normalized, raycastDistance, debugLayerMask)) { map[localFrom] = 1; } else { if (map[localFrom] != 1) { map[localFrom] = 0; } isInWall = false; break; } if (isInWall) { map[localFrom] = 1; } } } } //map[startPositions[0]] = 2; //map[startPositions[1]] = 2; yield break; }