예제 #1
0
        /// <summary>
        ///
        /// </summary>
        public void Generate()
        {
            var additiveX = GameConstants.SceneWidth / 2.0f / RayInterval;
            var additiveY = GameConstants.SceneHeight / 2.0f / RayInterval;

            var colCount = Mathf.CeilToInt(GameConstants.SceneWidth / RayInterval);
            var rowCount = Mathf.CeilToInt(GameConstants.SceneHeight / RayInterval);

            _nodeContainer = new NavMeshNodeContainer(
                new Vector2Int(colCount, rowCount),
                new Vector2(additiveX, additiveY),
                RayInterval
                );

            var circleCastMask = LayerMask.GetMask("Solid");
            var astarNodes     = new List <List <AStarSharp.Node> >();

            for (var i = 0; i < colCount; ++i)
            {
                var row = new List <AStarSharp.Node>();

                for (var j = 0; j < rowCount; ++j)
                {
                    var vec        = new System.Numerics.Vector2(i, j);
                    var node       = _nodeContainer.GetNodeAt(i, j);
                    var walkable   = true;
                    var raycastHit = Physics2D.CircleCast(node.worldPosition, 13.0f, Vector2.zero, 0.0f, circleCastMask);

                    walkable = raycastHit.collider == null;
                    if (!walkable)
                    {
                        if (raycastHit.collider.GetComponent <Construct>() != null)
                        {
                            walkable = true;
                        }
                    }

                    var navMeshHelpers = GameObject.FindObjectsOfType <NavMeshHelper>()
                                         .Where(x => x.bounds.Contains(node.worldPosition));
                    if (navMeshHelpers.Any())
                    {
                        walkable = navMeshHelpers.All(x => x.walkable);
                    }

                    node.walkable = walkable;

                    row.Add(node.node);
                }

                astarNodes.Add(row);
            }

            _astar = new AStarSharp.Astar(astarNodes);
        }
예제 #2
0
        protected void InitializeAStarMap(TileReferences spriteTileReferences)
        {
            Debug.Assert(TheMap != null);
            Debug.Assert(TheMap.Length > 0);
            int nXTiles = TheMap[0].Length;
            int nYTiles = TheMap.Length;

            // load the A-Star compatible map into memory
            aStarNodes = Utils.Init2DList <AStarSharp.Node>(nXTiles, nYTiles);

            for (int x = 0; x < nXTiles; x++)
            {
                for (int y = 0; y < nYTiles; y++)
                {
                    TileReference currentTile = spriteTileReferences.GetTileReference(TheMap[x][y]);
                    bool          bIsWalkable = currentTile.IsWalking_Passable || currentTile.Index == 184 || currentTile.Index == 186;

                    AStarSharp.Node node = new AStarSharp.Node(new System.Numerics.Vector2(x, y), bIsWalkable);
                    aStarNodes[x].Add(node);
                }
            }
            astar = new AStarSharp.Astar(aStarNodes);
        }