void AddLink() { #if UNITY_EDITOR if (m_LinkInstance.valid) { Debug.LogError("Link is already added: " + this); return; } #endif var link = new NavMeshLinkData(); link.startPosition = m_StartPoint; link.endPosition = m_EndPoint; link.width = m_Width; link.costModifier = m_CostModifier; link.bidirectional = m_Bidirectional; link.area = m_Area; link.agentTypeID = m_AgentTypeID; m_LinkInstance = NavMesh.AddLink(link, transform.position, transform.rotation); if (m_LinkInstance.valid) { m_LinkInstance.owner = this; } m_LastPosition = transform.position; m_LastRotation = transform.rotation; }
public void Generate(EndPoint end) { //buildMap.SetMapValues(size, gridSize, height); TerrainData data = new TerrainData(); //due to Terrian Magic =( the heightmapResolution is always 2**n + 1 for some n //so we get a resolution as close to our input size as possible but the world unit size is the same. TILE_TYPE[,] tiles = new TILE_TYPE[gridSize, gridSize]; data.heightmapResolution = size; data.size = new Vector3(size, height, size); int rsize = data.heightmapResolution; data.alphamapResolution = rsize; Vector2Int current = Vector2Int.zero; Func <Vector2Int, bool> isFinished = (c) => { return(true); }; Func <Vector2Int, Vector2Int, bool> inLimits = (c, d) => { return (c.x + d.x < 1 || c.x + d.x > gridSize - 2 || c.y - d.y >= gridSize - 2 || c.y - d.y > 1); }; //change this to mathy solution //side is the side to start generation, 0 = right, 1 = down, 2 = left, 3 = up switch (end.MapDirection) { case 0: //right current = new Vector2Int(gridSize, end.gridPoint.y); break; case 1: //down isFinished = (c) => { return(c.y <= 0); }; inLimits = (c, d) => { return(c.x + d.x < 1 || c.x + d.x > gridSize - 2 || c.y - d.y < 1); }; current = new Vector2Int(end.gridPoint.x, gridSize); break; case 2: //left current = new Vector2Int(-1, 0); break; case 3: //up current = new Vector2Int(end.gridPoint.x, -1); inLimits = (c, d) => { return(c.x + d.x < 1 || c.x + d.x > gridSize - 2 || c.y - d.y >= gridSize - 2); }; isFinished = (c) => { return(c.y >= gridSize - 1); }; break; } Vector2Int Start = current; int steps; Vector2Int[] dirs = new Vector2Int[] { this.dirs[(end.MapDirection - 1) % 4], this.dirs[(end.MapDirection - 1) % 4] }; Vector2Int dir = this.dirs[end.MapDirection]; int failSafe = 0; while (!isFinished(current) && failSafe < 100) { steps = UnityEngine.Random.Range(2, maxRun); while (steps > 0 && failSafe < 100) { failSafe++; if (inLimits(current, dir) || isFinished(current)) { print("hsah"); break; } current += dir; if (end.MapDirection != 3) { print(current); } tiles[current.x, current.y] = TILE_TYPE.PATH; steps--; } if (dir == this.dirs[end.MapDirection]) { if (current.x == 1) { dir = Vector2Int.right; } else if (current.x == gridSize - 2) { dir = Vector2Int.left; } else { dir = dirs.Random(); } } else { dir = this.dirs[end.MapDirection]; } } float[,] heights = new float[rsize, rsize]; scale = rsize / (float)gridSize; gridScale = size / (float)gridSize; for (int x = 0; x < gridSize; x++) { for (int y = 0; y < gridSize; y++) { if (tiles[x, y] != TILE_TYPE.PATH) { // if tile is buildable // tiles[x, y] = TILE_TYPE.BUILDABLE; continue; } int sx = Mathf.RoundToInt(x * scale); int sy = Mathf.RoundToInt(y * scale); int ex = Mathf.RoundToInt((x + 1) * scale); int ey = Mathf.RoundToInt((y + 1) * scale); for (int cx = sx; cx < ex; cx++) { for (int cy = sy; cy < ey; cy++) { heights[cx, cy] = 1; } } } } //assign Textures and flip heights float[,,] textures = new float[rsize, rsize, 2]; for (int x = 0; x < rsize; x++) { for (int y = 0; y < rsize; y++) { if (heights[x, y] == 0) { heights[x, y] = 1; textures[x, y, 0] = 0; textures[x, y, 1] = 1; } else { heights[x, y] = 0; textures[x, y, 0] = 1; textures[x, y, 1] = 0; } } } //Set Terrian Textures TerrainLayer pathLayer = new TerrainLayer(); pathLayer.diffuseTexture = path; TerrainLayer buildableLayer = new TerrainLayer(); buildableLayer.diffuseTexture = buildable; data.terrainLayers = new TerrainLayer[] { pathLayer, buildableLayer }; data.SetAlphamaps(0, 0, textures); //Set Terrian Heights data.SetHeights(0, 0, heights); GameObject MapTerrain = Terrain.CreateTerrainGameObject(data); this.current = MapTerrain.GetComponent <Terrain>(); MapTerrain.transform.SetParent(buildMap.transform, false); Vector3 offset = new Vector3(-0.5f + end.terrian.y + this.dirs[end.MapDirection].y, 0, -0.5f + end.terrian.x + this.dirs[end.MapDirection].x) * gridScale * gridSize; MapTerrain.transform.localPosition += offset; MapSection ms = MapTerrain.AddComponent <MapSection>(); ms.tiles = tiles; ms.Section = end.terrian + this.dirs[end.MapDirection]; MapTerrain.layer = buildMap.MapLayer; buildMap.FinishSetup(); //Generate Navmesh stuff GeanerateNavMeshData(); NavMeshLinkData nmld = new NavMeshLinkData(); nmld.agentTypeID = Spawner.nma[0].agentTypeID; nmld.area = 0; nmld.bidirectional = true; nmld.width = gridScale; nmld.startPosition = new Vector3(Start.y + 0.5f, 0, Start.x + 0.5f) * gridScale + offset; Start += this.dirs[end.MapDirection]; nmld.endPosition = new Vector3(Start.y + 0.5f, 0, Start.x + 0.5f) * gridScale + offset; NavMesh.AddLink(nmld); //move end point Vector3 endPos = new Vector3(current.y + 0.5f, 0, current.x + 0.5f) * gridScale + offset; end.transform.position = endPos; end.transform.localScale = Vector3.one * gridScale; end.gridPoint = current; end.terrian = ms.Section; }