Game
#11 L-System
11 years ago

The Code..


You wanted the code.. Here it is; the crappy NON optimized code for the flower generator. When doing #30daydev mini games I use all the shortcuts I can, so as you can see in the code, I destroy every gameObject in the flower every time I make a new.. Would never do that in a real game.

Take it, study it, modify it, use it.

Now, go make game ;)

  • INCD021

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using System.Security.Cryptography;

public class CreateFlower : MonoBehaviour

{

		
			string currentText = string.Empty;

public Sprite stem1;

public Sprite stem2;

public Sprite flower;

public string stringToEdit;

public GUISkin theSkin;

// Use this for initialization

void MakeIt(string theString)

{

    Stack stack = new Stack();

    // Create recipe

    Dictionary<string, string> rules = new Dictionary<string, string>();

    rules.Add("a", "as-als+alb");

    rules.Add("b", "b");

    rules.Add("+", "+");

    rules.Add("-", "-");

    rules.Add("s", "s");

    rules.Add("l", "l");

    string recipe = "a";

    // Setup from SHA512.

    byte[] hash;

    using (SHA512 shaM = new SHA512Managed())

    {

        var data = Encoding.UTF8.GetBytes(theString);

        hash = shaM.ComputeHash(data);

    }

    float LineLengthA = 2f * (float)hash[0] / 255f;

    float LineLengthB = 2f * (float)hash[1] / 255f;

    float LineLengthC = 2f * (float)hash[2] / 255f;

    float zRotA = (float)hash[3] * (90f / 255f);

    float zRotB = (float)hash[4] * (90f / 255f);

    float zRotC = (float)hash[5] * (90f / 255f);

    Color colA = new Color((float)hash[6] / 255f, (float)hash[7] / 255f, (float)hash[8] / 255f);

    Color colB = new Color((float)hash[9] / 255f, (float)hash[10] / 255f, (float)hash[11] / 255f);

    Color colC = new Color((float)hash[12] / 255f, (float)hash[13] / 255f, (float)hash[14] / 255f);

    float FlowSize = ((float)hash[15] / 255f);

    Color FlowerColor = new Color((float)hash[16] / 255f, (float)hash[17] / 255f, (float)hash[18] / 255f);

    int iterations = (int)(3 + ((float)hash[19] / 255f * 5f));

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < iterations; i++)

    {

        sb.Length = 0;

        for (int j = 0; j < recipe.Length; j++)

        {

            string key = recipe[j].ToString();

            sb.Append(rules[key]);

        }

        recipe = sb.ToString();

    }

    //   Debug.Log(recipe);

    foreach (var item in GameObject.FindGameObjectsWithTag("Dynamic"))

    {

        Destroy(item);

    }

    // Build it.

    Vector4 currentPosAndRot = Vector4.zero;

    for (int j = 0; j < recipe.Length; j++)

    {

        switch (recipe[j])

        {

            case 'a':

                currentPosAndRot = CreateLine(currentPosAndRot, LineLengthA, colA, stem1);

                break;

            case 'b':

                currentPosAndRot = CreateLine(currentPosAndRot, LineLengthB, colB, stem2);

                break;

            case '+':

                currentPosAndRot.w += zRotA;

                break;

            case '-':

                currentPosAndRot.w -= zRotB;

                break;

            case 's':

                stack.Push(currentPosAndRot);

                break;

            case 'l':

                DrawFlower(currentPosAndRot, FlowSize, FlowerColor);

                currentPosAndRot = (Vector4)stack.Pop();

                break;

            default:

                break;

        }

    }

}

private Vector4 CreateLine(Vector4 par, float LineLength, Color col, Sprite spr)

{

    GameObject go = new GameObject("Stem");

    go.tag = "Dynamic";

    go.transform.parent = null;

    SpriteRenderer sr = go.AddComponent<SpriteRenderer>();

    sr.renderer.material.color = col;

    sr.sprite = spr;

    go.transform.position = new Vector3(par.x, par.y, par.z);

    go.transform.localScale = new Vector3(1f, LineLength, 1f);

    go.transform.localEulerAngles = new Vector3(0f, 0f, par.w);

    Vector4 resultingPAR = go.transform.position + go.transform.up * LineLength;

    resultingPAR.w = par.w;

    return resultingPAR;

}

public void DrawFlower(Vector4 par, float FlowSize, Color col)

{

    GameObject go = new GameObject("Flower");

    go.tag = "Dynamic";

    go.transform.parent = null;

    SpriteRenderer sr = go.AddComponent<SpriteRenderer>();

    sr.renderer.material.color = col;

    sr.sprite = flower;

    sr.renderer.sortingOrder = 1000;

    go.transform.localScale = new Vector3(FlowSize, FlowSize, FlowSize);

    go.transform.position = new Vector3(par.x, par.y, par.z);

    go.transform.localEulerAngles = new Vector3(0f, 0f, par.w);

}

void OnGUI()

{

    GUI.skin = theSkin;

    stringToEdit = GUI.TextField(new Rect(10, 520-64, 940, 64), stringToEdit, 255);

    if (!currentText.Equals(stringToEdit))

    {

        currentText = stringToEdit;

        MakeIt(currentText.Replace(" ", ""));

    }

}
		
	

}



3 comments

Loading...

Next up

We made a lot of improvements on the Freezing Plains visual. Things like pine trees, tiny bushes, some rocks, and others game props!

#IndieGame | #IndieDev | #GameDev | #PixelArt | #WaifuQuest | #WifeQuest | #screenshotsaturday

🍒Did you like the game? A demo is available for free on Steam.

✌️ Thanks for playing IndieGameiacs!

⚙️ Free Demo: http://bit.ly/UniDuni

📺 Full Video: https://youtu.be/5qEbVXg7GaQ

#steam #uniduni #clops #screenshotsaturday

Heya there! I really wanted to show you all a little gameplay preview of the first boss fight i'm currently working on i hope you like it ^^

I have added rewards for quests so that villagers can give you something in return for your hard work helping them. 🥳

I want to know - what's your favourite quest reward?

Family band complete!

What you all think

Sometimes the suction cup get sticky. Small animation I made in Blender after doing my tutorial on picking up/dropping objects in animations. Crate model by jQueary (https://sketchfab.com/3d-models/game-ready-sci-fi-crate-d98deca6…).

Back in my art school days I used to ride the 710 COPSA line from Parque Del Plata to Montevideo almost everyday. This is the Marcopolo Viaggio G4 Mercedes Benz model from the late 80s, one of the older bus models that was running on the line.

Finished work on the emotions of a new character for the tank universe