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

Bandana Dee the Dream Friend

Chiaki Nanami!

Protege el conocimiento, salva la historia. Guardian of Lore es un platformer 2D en el que debes luchar para mantener viva la memoria de la mitología latinoamericana. El juego llegará a Steam el 18 de mayo: https://steam.pm/app/1211740 #ScreenshotSaturday

What do you think guys in my robot?Just a beginner in 3d modeling..

My instagram https://www.instagram.com/rojhonbb/

Animation test. Not an actaul conclusion to who wins sonic vs Mario. Just a test animation combo.

A 1h30 painting, coffee break.

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

Hi all, Quick update with a bunch of fixes and changes.

Updated save system, optimisation and screen tear fixes, a new shoutout room and more sounds.

Don't forget to follow the kickstarter going live on the 20th! https://www.kickstarter.com/projects/9fingergames/zapling-bygone

Enjoy!

Super Sonic Pixel art.

It's now possible to destroy some of the obstacles and gain access to new locations or create shortcuts. #gamedev #indiegame #rpg

Art made for my friend @ungerbibs . Much love for this <3