示例#1
0
        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;
        }
        private void TryAddLink()
        {
            // A link be between exactly two polygons.
            if (_intersectedEdges.Count != 2)
            {
                return;
            }

            // A polygon can't connect with itself
            if (_intersectedEdges[0].Polygon == _intersectedEdges[1].Polygon)
            {
                return;
            }

            // Can make a connection.
            PolygonLink polyLink = new PolygonLink(
                _intersectedEdges[0].Polygon,
                _intersectedEdges[0].Edge,
                _intersectedEdges[1].Polygon,
                _intersectedEdges[1].Edge);

            _navMesh.AddLink(polyLink);
        }
        /// <summary>
        /// This function will look up the polygon index right away.
        /// This could potentially cause problems if the XML has the polygon defs later in the file.
        /// The values should really be cached and it should be done after.
        /// (or thrown in a closure but the C# syntax is a little too messy for that)
        /// </summary>
        /// <param name="xmlReader"></param>
        /// <param name="navMesh"></param>
        private static void LoadSingleLink(XmlTextReader xmlReader, NavMesh navMesh)
        {
            int startPolygonIndex = -1;
            int startEdgeStart    = -1;
            int startEdgeEnd      = -1;

            int endPolygonIndex = -1;
            int endEdgeStart    = -1;
            int endEdgeEnd      = -1;

            xmlReader.MoveToContent();
            while (xmlReader.Read())
            {
                if ("start" == xmlReader.Name)
                {
                    startPolygonIndex = int.Parse(xmlReader.GetAttribute("polygon"));
                    startEdgeStart    = int.Parse(xmlReader.GetAttribute("edgestart"));
                    startEdgeEnd      = int.Parse(xmlReader.GetAttribute("edgeend"));
                }
                else if ("end" == xmlReader.Name)
                {
                    endPolygonIndex = int.Parse(xmlReader.GetAttribute("polygon"));
                    endEdgeStart    = int.Parse(xmlReader.GetAttribute("edgestart"));
                    endEdgeEnd      = int.Parse(xmlReader.GetAttribute("edgeend"));
                }
                else if ("link" == xmlReader.Name)
                {
                    PolygonLink polygonLink = new PolygonLink(
                        navMesh.PolygonList[startPolygonIndex],
                        new IndexedEdge(startEdgeStart, startEdgeEnd),
                        navMesh.PolygonList[endPolygonIndex],
                        new IndexedEdge(endEdgeStart, endEdgeEnd));
                    navMesh.AddLink(polygonLink);
                    return;
                }
            }
        }
示例#4
0
    // Token: 0x06000B52 RID: 2898 RVA: 0x00051ABC File Offset: 0x0004FCBC
    private void ConnectAlongEdge(List <KeyValuePair <Vector3, NavMeshLinkInstance> > links, Vector3 p0, Vector3 p1, float step, Pathfinding.AgentSettings settings)
    {
        Vector3 normalized = (p1 - p0).normalized;
        Vector3 a          = Vector3.Cross(Vector3.up, normalized);
        float   num        = Vector3.Distance(p0, p1);
        bool    canSwim    = settings.m_canSwim;

        this.tempStitchPoints.Clear();
        for (float num2 = step / 2f; num2 <= num; num2 += step)
        {
            Vector3 p2 = p0 + normalized * num2;
            this.FindGround(p2, canSwim, this.tempStitchPoints, settings);
        }
        if (this.CompareLinks(this.tempStitchPoints, links))
        {
            return;
        }
        this.ClearLinks(links);
        foreach (Vector3 vector in this.tempStitchPoints)
        {
            NavMeshLinkInstance value = NavMesh.AddLink(new NavMeshLinkData
            {
                startPosition = vector - a * 0.1f,
                endPosition   = vector + a * 0.1f,
                width         = step,
                costModifier  = this.m_linkCost,
                bidirectional = true,
                agentTypeID   = settings.m_build.agentTypeID,
                area          = 2
            });
            if (value.valid)
            {
                links.Add(new KeyValuePair <Vector3, NavMeshLinkInstance>(vector, value));
            }
        }
    }
示例#5
0
    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;
    }