ZMWSLI0-SL2021-GR11/Projekt/MWSProjekt/Assets/Scripts/TreeTurtle.cs

78 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using T = System.Tuple<UnityEngine.GameObject, UnityEngine.Matrix4x4>;
public class TreeTurtle : TurtleLSystemEnvironment
{
public GameObject branch;
public float angle;
private Matrix4x4 RootGrowth(float age)
{
return Matrix4x4.TRS(
new Vector3(
0.0f,
0.3f,
0.0f),
Quaternion.identity,
new Vector3(
0.02f + age * 0.1f,
0.02f + age * 0.1f,
0.02f + age * 0.1f));
}
private Matrix4x4 BranchGrowth(float age)
{
return Matrix4x4.TRS(
new Vector3(
0.0f,
1.8f,
0.0f),
Quaternion.identity,
new Vector3(
0.01f + age * 0.2f,
0.5f + age * 0.5f,
0.02f + age * 0.12f));
}
private Func<float[], Matrix4x4> 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;
}
protected override void initLiteralInterpretation()
{
turtleInterpretation = new Dictionary<string, Func<float[], T>>
{
{ "+", (float[] args) => new T(null, Roation(Vector3.back)(args)) },
{ "-", (float[] args) => new T(null, Roation(Vector3.forward)(args)) },
{ "\\", (float[] args) => new T(null, Roation(Vector3.down)(args)) },
{ "/", (float[] args) => new T(null, Roation(Vector3.up)(args)) },
{ "^", (float[] args) => new T(null, Roation(Vector3.left)(args)) },
{ "&", (float[] args) => new T(null, Roation(Vector3.right)(args)) },
{ "f", (float[] args) => new T(null, Matrix4x4.Translate(Vector3.up * args[0])) },
{ "I", (float[] args) => new T(null, Matrix4x4.identity) },
{ "R", (float[] args) => new T(branch, RootGrowth(args[0]))},
{ "B", (float[] args) => new T(branch, BranchGrowth(args[0]))},
//Wildcard how to represent any other symbol
{ "*.*", (float[] args) => new T(branch, BranchGrowth(args[0]))}
};
}
}