Beispiel #1
0
    void Start()
    {
        // Get Grid Cell Position
        Vector3Int position = GridRef.WorldToCell(transform.position);

        position.x += mapCreator.MapWidth / 2;
        position.y += mapCreator.MapHeight / 2;

        LastPosition = position;

        // Update Pathfinder Nodes Grid
        var          gg   = AstarPath.active.data.gridGraph;
        int          x    = position.x;
        int          y    = position.y;
        GridNodeBase node = gg.GetNode(x, y);

        AstarPath.active.AddWorkItem(ctx => {
            var PfGridGraph = AstarPath.active.data.gridGraph;

            // Mark a single node as unwalkable
            PfGridGraph.GetNode(x, y).Walkable = false;

            // Recalculate the connections for that node as well as its neighbours
            PfGridGraph.CalculateConnectionsForCellAndNeighbours(x, y);
        });
    }
Beispiel #2
0
        public bool RandomizeObjectPosition_2(Spawnable spnbl)
        {
            Vector2Int cellSize = new Vector2Int
            {
                x = Mathf.CeilToInt(spnbl.physicsCollider.bounds.extents.x / mainGraph.nodeSize),
                y = Mathf.CeilToInt(spnbl.physicsCollider.bounds.extents.y / mainGraph.nodeSize)
            };

            var offset = new Vector2Int
            {
                x = Mathf.CeilToInt(spnbl.physicsCollider.offset.x / mainGraph.nodeSize),
                y = Mathf.CeilToInt(spnbl.physicsCollider.offset.y / mainGraph.nodeSize)
            };

            var randomPerlinNode = RandomPerlinNode + offset;
            var selectedNode     = mainGraph.GetNode(randomPerlinNode.x, randomPerlinNode.y);

            if (selectedNode == null)
            {
                Debug.Log("null selected node");
                return(false);
            }


            IntRect bounds = new IntRect
            {
                xmin = randomPerlinNode.x - cellSize.x,
                ymin = randomPerlinNode.y - cellSize.y,
                xmax = randomPerlinNode.x + cellSize.x,
                ymax = randomPerlinNode.y + cellSize.y
            };


            var buffer = new GridNodeBase[10];
            var pts    = mainGraph.GetNodesInRegion(bounds, buffer);

            for (int i = 0; i < pts; i++)
            {
                buffer[i].Walkable = false;
            }

            Debug.Log(selectedNode.position);
            Debug.Log((Vector3)selectedNode.position);

            spnbl.transform.position = (Vector3)selectedNode.position;
            return(true);
        }
Beispiel #3
0
    private void Start()
    {
        Vector3Int position = Grid.WorldToCell(transform.position);

        position.x += 18;
        position.y += 17;

        LastPosition = position;

        var          gg   = AstarPath.active.data.gridGraph;
        int          x    = position.x;
        int          y    = position.y;
        GridNodeBase node = gg.GetNode(x, y);

        AstarPath.active.AddWorkItem(ctx => {
            var PfGridGraph = AstarPath.active.data.gridGraph;

            // Mark a single node as unwalkable
            PfGridGraph.GetNode(x, y).Walkable = false;

            // Recalculate the connections for that node as well as its neighbours
            PfGridGraph.CalculateConnectionsForCellAndNeighbours(x, y);
        });
    }