using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class TurtleContextTask : TurtleLSystem { public GameObject obj; public GameObject flower; public GameObject leaf; public float angle; private Func _roation(Vector3 axis) { Matrix4x4 f(float[] args) { if (args.Length == 0) { return Matrix4x4.Rotate(Quaternion.AngleAxis(angle, axis)); } else { return Matrix4x4.Rotate(Quaternion.AngleAxis(args[0], axis)); } } return f; } GameObject setColor(GameObject prefab, int index) { var result = Instantiate(prefab); switch (index) { case 1: result.GetComponent().material.SetColor("_Color", Color.red); break; case 2: result.GetComponent().material.SetColor("_Color", Color.blue); break; case 3: result.GetComponent().material.SetColor("_Color", Color.white); break; } return result; } protected override void initLiteralInterpretation() { turtleInterpretation = new Dictionary>>(); //turtleInterpretation var transformation = Matrix4x4.Translate(new Vector3(0.0f, 0.1f, 0)) * Matrix4x4.Scale(new Vector3(0.05f, 0.1f, 0.05f)); turtleInterpretation.Add("+", (float[] args) => new Tuple(null, _roation(Vector3.back)(args))); turtleInterpretation.Add("-", (float[] args) => new Tuple(null, _roation(Vector3.forward)(args))); turtleInterpretation.Add("\\", (float[] args) => new Tuple(null, _roation(Vector3.down)(args))); turtleInterpretation.Add("/", (float[] args) => new Tuple(null, _roation(Vector3.up)(args))); turtleInterpretation.Add("^", (float[] args) => new Tuple(null, _roation(Vector3.left)(args))); turtleInterpretation.Add("&", (float[] args) => new Tuple(null, _roation(Vector3.right)(args))); turtleInterpretation.Add("f", (float[] args) => new Tuple(null, Matrix4x4.Translate(Vector3.up * args[0]))); turtleInterpretation.Add("W", (float[] args) => new Tuple(flower, Matrix4x4.Scale(Vector3.one*0.1f))); turtleInterpretation.Add("B", (float[] args) => new Tuple(leaf, Matrix4x4.Scale(Vector3.one))); turtleInterpretation.Add("F", (float[] args) => new Tuple(setColor(obj, Mathf.FloorToInt(args[0])), transformation * Matrix4x4.Scale(new Vector3(args[1], 1.0f, args[1])))); //Wildcard how to represent any other symbol turtleInterpretation.Add("*.*", (float[] args) => new Tuple(obj, transformation)); } }