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

97 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
public class LSystemEvaluator
{
public LSystemNode lSystemString { get; set; }
public List<LSystemRule> lSystemRules { get; set; }
public String[] ignored { get; set; }
public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules)
{
lSystemString = startingString;
lSystemRules = rules;
ignored = new String[0];
}
public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules, String[] ignored)
{
lSystemString = startingString;
lSystemRules = rules;
this.ignored = ignored;
}
private LSystemNode _rewrite(LSystemNode node)
{
foreach (var rule in lSystemRules)
{
if (rule.is_aplicable(node, ignored))
{
return rule.rewrite(node, ignored);
}
}
return new LSystemNode(node.literal);
}
//private void _rewrite_recursive(LSystemNode node, LSystemNode parent) {
// var new_node = _rewrite(node);
// parent.children.Add(new_node);
// new_node.parent = parent;
// var new_parent = new_node.newParent();
// foreach (var child in node.children) {
// _rewrite_recursive(child, new_parent);
// }
//}
private void _rewrite_recursive(LSystemNode node, LSystemNode parent)
{
var new_node = _rewrite(node);
parent.children.Add(new_node);
new_node.parent = parent;
var new_parent = new_node.newParent();
foreach (var child in node.children)
{
_rewrite_recursive(child, new_parent);
}
while (node.mainChild != null)
{
new_node = _rewrite(node.mainChild);
new_node.parent = new_parent;
new_parent.mainChild = new_node;
new_parent = new_node.newParent();
foreach (var child in node.mainChild.children)
{
_rewrite_recursive(child, new_parent);
}
node = node.mainChild;
}
}
public void rewrite()
{
var new_root = _rewrite(lSystemString);
var new_parent = new_root.newParent();
foreach (var child in lSystemString.children)
{
_rewrite_recursive(child, new_parent);
}
var node = lSystemString;
while (node.mainChild != null)
{
var new_node = _rewrite(node.mainChild);
new_node.parent = new_parent;
new_parent.mainChild = new_node;
new_parent = new_node.newParent();
foreach (var child in node.mainChild.children)
{
_rewrite_recursive(child, new_parent);
}
node = node.mainChild;
}
lSystemString = new_root;
}
//private void rewrite(ref LSystemNode currentNode)
}