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.
        }
    }
Advertisements

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.

Herbert Game Part 6.5

In den letzten Tagen habe ich eine vorläufiges Hauptmenü erstellt, ansonsten aber nicht viel Fortschritt gemacht. Ich probiere gerade aus, wie ich den Code schreiben muss, damit Herbert kleine Kisten aufhebt, große Kisten aber schiebt. Die großen Kisten aber nur nach vorne oder hinten. Daran bin ich gerade ein bisschen am basteln, habe aber schon eine Idee, wie das funktionieren sollte. Dieses Wochenende bin ich aber nicht zuhause, weshalb ich nicht an dem Spiel arbeiten kann. Hier eine kleine Todo-Liste, die ich bisher erstellt habe:

reihenfolge, was gemacht werden sollte:

-push_idle animation + code
-jump animation + code
(-vllt fall animation und einbinden)
-herbert model einen neuen knochen hinzufügen, oder in unity ein empty, an das die große kiste geparented wird.
-push code schreiben und testen. kiste sollte nur entlang einer achse zu bewegen sein.
-angucken von wegen rigidbody/charController oder so, damit man kleine kisten umherstoßen kann.
-textur große kiste
-fenster + textur
-tür + textur
-fabriktor + textur
-boden und wandtexturen
-menü designen
-menü bauen
-gabelstapler + textur
-kiste für stapler.
-stapler animationen + code

 

Hier ein kleines bild von dem Menü:

 

 

 

 

ENGLISH: In the last few days, I created a temporary main menu. But else, didn’t do much. I won’t be at home for the weekend, so I can’t make progress. Up there is a to do-list for the next few tasks I should tackle.

Herbert Game Part 6 + Video

Okay, gestern hatte ich es geschafft, dass Herbert Kisten aufnimmt. Aber leider gab es im Script ein Problem, wegen dem er die Kisten nicht immer wieder hinstellen wollte. Jedenfalls wurde die Animation nicht abgespielt und sie ist dass von ihm weggeflogen als er seinen state wieder in “idle” geändert hat. Ich habe herausgefunden, dass er in einem anderen state hängenblieb, wenn ich ihm gesagt hatte, er soll die Kiste wieder hinsetzen. Hat eine Weile gedauert bis ich das Problem in den Griff bekommen hab. Es hatte was mit yield statements zu tun, die irgendwie nicht funktioniert hatten. Wer kleines bisschen mehr darüber wissen will, kann auf diesen Link klicken: http://answers.unity3d.com/questions/339120 . Ich habe den Code jetzt so angepasst, dass wenn Herbert eine Kiste aufhebt oder ablegt wartet, bis die Animation zuende ist, bis der state sich ändert. Und das funktioniert soweit recht gut. Ich habe dann, wie quasi versprochen ein kleines Gameplay video aufgenommen um zu zeigen, wie weit ich bisher bin.

 

ENGLISH: Okay, so yesterday I got Herbert to pickup boxes. But unfortunately there was a problem in the script, where he refused to put them back down. Well, the animation didn’t play and the box just flew away from him, when he changed his state back to” idle”. I found out, that he got somehow stuck in a different state, when I told him to put the box down. It took a while until I fixed the problem. It hat something to do with the yield statement, which didn’t work. You can read a little bit more about it here: http://answers.unity3d.com/questions/339120 . I now changed the code, so that when Herbert picks up a box or puts it down, he waits until the animation is finished to change the state. And that works perfectly for now. So then I recorded a little gameplay video, as kinda promised, to show how far I am.