Rework on Maze Generation

I spent most of today rewriting the maze generation algorithm. Somehow I figure out how to make it, so the array, which stores the rooms doesn’t need to be as big, and all the rooms are more or less of equal size. Previously there were some really big rooms, even though the roomSize was supposed to be small. Unfortunately the rewrite does more calculation, but I didn’t test it that well, maybe it’s about the same. I also wrote a little bit of code, which combines all the tiles, so that I reduce the draw calls from 200 to 3 ( 8 with hard shadow lighting), which is great. I did this with a lot of help of the unity script reference and some other sources. But since there is not really that much explained documentation, I will share the code here. But first a little picture:

mazeNEU2I also started to draw some textures, but I’m currently too lazy to upload them. Also it’s 2am.

Here is the new generation code ( MazeGenNew Code ) and here the combine stuff:

void Combine(){
    // I have a game object named tileHolder, which stores the other tileholder(wall, door, floor...)
        foreach(Transform child in tileHolder.transform){

            MeshFilter[] meshFilter = child.GetComponentsInChildren<MeshFilter>();    // create an array and store all the child MeshFilters in it
            MeshRenderer meshRenderer = child.GetComponentInChildren<MeshRenderer>();    // Create on MeshRender thingy from on child to store the material for later
            Material mat = meshRenderer.renderer.material;    // save the material in this variable.

            child.gameObject.AddComponent<MeshFilter>();    // Add a MeshFilter component to the holder child
            child.gameObject.AddComponent<MeshRenderer>();    // Add a MeshRenderer component to the holder child (needed to actually see the mesh)

            CombineInstance[] combine = new CombineInstance[meshFilter.Length];    //create an array, which is used to store the meshed from the MeshFilters.

            int i = 0;
            while( i < meshFilter.Length ){        // Go through every child and assign its mesh to the combine array.
                combine[i].mesh = meshFilter[i].sharedMesh;
                combine[i].transform = meshFilter[i].transform.localToWorldMatrix;    //probably stores the position of the child mesh
                Destroy (meshFilter[i].transform.parent.gameObject);    // delete the child, because it's no longer needed
                i++;
            }
            child.GetComponent<MeshFilter>().mesh = new Mesh();    // create a new mesh in the child MeshFilter
            child.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);    //assign the combined mesh to the MeshFilter mesh.

            child.gameObject.AddComponent<MeshCollider>();    // create a MeshCollider.
            Mesh mech = child.GetComponent<MeshFilter>().mesh;    // store the combined mesh in this variable.
            child.GetComponent<MeshCollider>().sharedMesh = null;    // set the collider mesh to null (somewhere on the internet it sais to do this).
            child.GetComponent<MeshCollider>().sharedMesh = mech;    // assign the mesh to the collider
            child.transform.GetComponent<MeshRenderer>().renderer.material = mat;    // assign the previously stored material to the mesh.
        }
    }

Unity Game Programming Stuff

mazeRender

I started working on the Herbert Game again. But now I wanted to implement controller support. And I’m writing all the code new, since there was some kind of problem, which I couldn’t solve. So I said “screw everything, I’m starting from the beginn!”. And as I was coding at a friends place, he got me sidetracked with a maze generator. And this is what I have done the last week.

First, I researched some maze algorithms. While researching, I found this site: http://www.jamisbuck.org/mazes/ , which explains the most common, I think, algorithms. I decided to try my hands on the Recursive Division one. And as the coding rookie I am, this actually took a while to figure out. Well, it wasn’t hard to figure out, how it works, but it was difficult for me to convert all of this knowledge to code.

But now I have something to show.

räumeThis picture is from a few days ago and shows kind of the final algorithm. But there was a slight mistake in the data assignment, which I had to fix. I couldn’t figure out, how to get doors in there properly without overlapping with other walls. But I figured it out. Then I started to assign each tile a type, where it was a L, T or I wall etc. The whole Generator is currently setup in two files. One, which will create the raw data, and the other one, which will build the maze. And with the new tilemodels, this is how the maze currently looks like:

maze

And for the really curious, here is the code: MazeGenerator . It’s written in C# with help of Unity 3D. So I used MonoDevelop 4. In order to upload the file here, I had to rename it to “.key”. If you downloaded it, just rename it to “.cs” and everything will be fine. However, I think it will only work with Unity. But you can still look at it, and figure out, how I did it. I’m really not good at programming, but with this, I’m kind of proud of myself.