Ejemplo n.º 1
0
    /// <summary>
    /// Set level data, initialize pathfinding and node cache.
    /// </summary>
    /// <param name="grid"></param>
    //public void SetupLevel(Node[,] grid)
    public void SetupLevel()
    {
        if (FindObjectOfType <GridLayout>())
        {
            TileGrid = FindObjectOfType <GridLayout>();
        } //Get the levels' gridLayout

        TilemapCollider2D[] tilemapColliders = TileGrid.GetComponentsInChildren <TilemapCollider2D>();
        foreach (TilemapCollider2D tilemapCollider in tilemapColliders)
        {
            TilemapRenderer tilemapRenderer = tilemapCollider.GetComponent <TilemapRenderer>(); //Find the attached renderer.
            Tilemap         tilemap         = tilemapCollider.GetComponent <Tilemap>();         //Find the attached tilemap.

            if (tilemapRenderer != null && tilemap != null)
            {
                TileMapComponent thisTileMapComponent = new TileMapComponent()
                {
                    Tilemap    = tilemap,
                    Collider2D = tilemapCollider,
                    Renderer   = tilemapRenderer
                };

                tileMapComponents.Add(thisTileMapComponent);

                if (tilemapRenderer.sortingLayerName == "Default")
                {
                    WalkableTilemap = thisTileMapComponent;

                    NavGrid = GenerateGrid(thisTileMapComponent.Tilemap);

                    NavGrid.Init();
                }
            }
        }

        //MethodInfo method = GetType().GetMethod(DebugNavGrid);

        InvokeRepeating("DebugNavGrid", 0, 10f); //TODO: Replace this so it only updates on a new turn.

        Pathfinding = new Pathfinding(NavGrid);
        nodeCache   = new List <Node>(NavGrid.GetLength(0) * NavGrid.GetLength(1));
    }