Game
#11 L-System
10 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

art comission.

Fan art for Foolish I'm feeling kinda better so I drew this

I was bored, so... ManutKat.

Microsoft Windows XP Unprofessional (windows logo prototype)

Call it 'wrong turn'!🚫 The feeling of running into a house with only one exit🚪, and being doomed to die. #pixelart #pixelartist #pixelartwork #art #pixel #indiegame #IndieGameDev #indieartist

Szayel Aporro Granz - Bleach

Some time ago, we posted a screenshot of the Mexico level. A curiosity about it is that, like some other scenarios of the game, it was inspired by a real landscape, The Basaltic Prisms of Santa María Regla. It is one of the natural wonders of the country!

A 1h30 painting, coffee break.

My twitter: https://twitter.com/loupil100

Respect (Only TLOZ Players Will Understand)