// Increases the age of a symbol if necessary and applies the rules if there are any for that symbol public SymbolString ApplyRules(SymbolString s) { SymbolString result = s; // the string to manipulate for (int i = s.Length() - 1; i >= 0; i--) // back to front { Symbol key = result.GetAt(i); // the char to analyze if (key.age >= 0 && key.age < Variables.GetMaxAge(vars, key)) { key.age++; } else if(key.age >= 0) { Debug.Log("MAX AGE REACHED"); } if (rules.ContainsKey(key)) // ensure the char has a rule { result.ReplaceAt(i, rules.GetValue(key)); // apply the rule } } return result; }
// Starts with a symbolstring from an lsystem and creates/updates Stem segments to match the symbolstring public void CreateGameObject(int n) { if (n < numIterations) { if (lSystem != null) { SymbolString blueprint = lSystem.GetResult(n); SymbolString result = blueprint; // The blueprint edited to include the id of the stem segment Stack <Stem> stemStack = new Stack <Stem>(); float roll = 0; float pitch = 0; float yaw = 0; Stem currentStem = root; currentStem.stemPrefab = stemPrefab; for (int i = 0; i < blueprint.Length(); i++) { Symbol sym = blueprint.GetAt(i); switch (sym.character) { case 'F': if (sym.id == -1) { pitch += Random.Range(-3f, 3f); yaw += Random.Range(-3f, 3f); roll += Random.Range(-3f, 3f); Stem newStem = currentStem.AddStemSegment(sym.age, pitch, yaw, roll); stemList.Add(newStem); result.ReplaceAt(i, (SymbolString) new Symbol(sym.character, sym.age, stemList.Count - 1)); newStem.id = result.GetAt(i).id; currentStem = newStem; } else { stemList[sym.id].UpdateStem(sym.age); currentStem = stemList[sym.id]; } pitch = 0; yaw = 0; roll = 0; break; case '&': pitch -= lSystem.pitch; break; case '^': pitch += lSystem.pitch; break; case '\\': yaw -= lSystem.yaw; break; case '/': yaw += lSystem.yaw; break; case '-': roll -= lSystem.roll; break; case '+': roll += lSystem.roll; break; case '[': stemStack.Push(currentStem); break; case ']': currentStem = stemStack.Pop(); break; default: break; } } root.UpdateChildTransforms(0); Debug.Log(string.Format("n({0}) = {1}", n, (string)result)); } else { this.Start(); this.CreateGameObject(n); } } }