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

Did you know that today is #InternationalJazzDay? 🎷😎

No? 😯 Well, we couldn't pass up this date!

And what better to celebrate this day than #Pecaminosa #OST? 🎷

Let us know what you think about it and have a great weekend!

Bandana Dee the Dream Friend

We are under attack!

Successful landing

#screenshotsaturday

Today I was setting up the location of the first boss. His name is "Father" and he is the first of the Patagonians. His task is to guard the road to the House.🛡🗡 Bookmark pre-launch page🔖🔖🔖

https://www.kickstarter.com/projects/rdvindiegame/the-patagonian…

Spaaaace~

These are background sprites I've created for a game I'm working on at school ^w^ Click on the post to see how the sprites connect. You won't regret it! (personally, I think it's pretty heheh)

Drawn in Piskel using my mouse. Whaddya think?

Development of my game Juno. This is the rune equipment system.

Usually update over on twitter: https://twitter.com/synrec_kaisyl

:] me sigue sin gustar

:] I still don't like it

What Are You Doing In My Swamp!?

Now spruce forest monsters can vomit forest shields, but Brutal Castle will not stop!

#gamedev #pixelart #indie