0
\$\begingroup\$

So I have a generator code that renderes a TileMap on the scene, but It keeps creating a sollid block of walls A block

Where I want it to have empty cells(This is not the final algorythm(Recursive Backtracker), just the begining where we fill the maze), I'm following this tutorial,trying to addjust it to the Tilemap (https://www.youtube.com/watch?v=ya1HyptE5uc&t=870s), here is my code for the renderer

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class MazeRenderer : MonoBehaviour {
    [SerializeField]
    [Range(1, 50)]
    private int width = 10; // width of the maze
    [SerializeField]
    [Range(1, 50)]
    private int height = 10; // height of the maze
    [SerializeField]
    private Tilemap floorMap; // Tilemap for the floor(without collider, render layer 0)
    [SerializeField]
    private Tilemap collidableMap; //  Tilemap for the walls(with the collider, render layer 1)
    [SerializeField]
    private TileBase floorTile = null; // base tile for the floor
    [SerializeField]
    private TileBase wallTile = null; // base tile for a wall
    [SerializeField]
    private Transform wallPrefab = null; // unused code for a regular wall prefab
    [SerializeField]
    private int size = 1;// offset for tile placement
    private void Start()
    {
    }
    private void Update()
    {
        var maze = MazeGenerator.Generate(width, height);// creating a maze with width and height
        Draw(maze); // rendering the maze with the Draw() method
    }
    private void Draw(Node[,] maze)// Draw method, takes in an 
    {
        for (int i = 0; i < width; i++) // simple floor generation 
        {
            for(int j = 0; j < height; j++)
            {
                var position = new Vector3Int(-i, -j, 0); // floor generation from the negative position
                floorMap.SetTile(position, floorTile); //setting floor tiles
            }
        }
        for(int i = 0; i < width; i++) //maze generation
        {
            for(int j = 0; j < height; j++)
            {
                var  cell = maze[i,j]; //creates connections between cells
                var position = collidableMap.WorldToCell(new Vector3(-width + i, -height + j, 0));//sets  cells positions
                if(cell.TopWall) //sets top tile for the cell
                {
                    position = collidableMap.WorldToCell(position + new Vector3Int(0, size, 0));
                    collidableMap.SetTile(position, wallTile);
                }
                if (cell.LeftWall) // sets left tile for the cell
                {
                    position = position  + new Vector3Int(-size, 0, 0);
                    collidableMap.SetTile(position, wallTile);
                }
                if (i == width - 1) // checks if we reached the right side of the maze
                {
                    if (cell.RightWall) //generates right wall
                    {
                        position = position + new Vector3Int(size, 0, 0);
                        collidableMap.SetTile(position, wallTile);
                    }
                }

                if (j == 0) // checkes if we reached bottom
                {
                    if (cell.BottomWall) // generates bottom wall
                    {
                        position = position + new Vector3Int(0, -size, 0);
                        collidableMap.SetTile(position, wallTile);
                    }
                }

            }
        }
    }
}

And for the generator:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct Node // setups up all possible wall placements + if the cell has been visited or not 
{
    public bool LeftWall;
    public bool RightWall;
    public bool TopWall;
    public bool BottomWall;
    public bool isVisited;
}
public struct Position // position of the neighbouring cell
{
    public int x;
    public int y;
}
public struct Neighbour // creates a full neighbour position with possible wall placements
{
    public Position position;
    public Node sharedNode;
}
public static class MazeGenerator{
    public static Node[,] Generate(int width, int height)//generates possible wall  placements in a maze
    {
        Node initial = new Node(); //creating initial tile with all true possible wall placements
        initial.LeftWall = true;
        initial.RightWall = true;
        initial.TopWall = true;
        initial.BottomWall = true;
        Node[,] maze = new Node[width, height]; //creating an array of wall placements
        for(int i = 0; i < width; i++)
        {
            for(int j = 0; j < height; j++)
            {
                maze[i, j] = initial; // inputing initial wall placements into array
            }
        }
        return maze; //returning array
    }
    

}

picture of the objects in the sceneenter image description here

MazeRenderer is the only object in the scene that has a script(MazeRenderer),

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

It is a solid block of walls because you tell it to generate a solid block of walls.

if(cell.TopWall)  {
    position = collidableMap.WorldToCell(position + new Vector3Int(0, size, 0));
    collidableMap.SetTile(position, wallTile);
}

every single cells is having cell.TopWall true according to your MazeGenerator since you never change it. This should be always true, so your loop just places a tile everywhere.

As a side node, you want to generate your maze once and not in Update()

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .