Ejemplo n.º 1
0
        // Parse the sentence, load symbol implementations and find the next module to execute.
        // One module is executed at a time, and an internal pointer is adjusted to for the next iteration.
        public bool ProcessNextModule(Module previous, Sentence sentence,
                                      SerializableDictionary <char, GameObject> implementation, RuleSet rules, ParameterBundle bundle, bool baked)
        {
            if (dead)
            {
                return(false);
            }
            Profiler.BeginSample("LSystem.Module.ProcessNextModule");

            GameObject module;
            char       symbol = '\0';

            do
            {
                if (!sentence.HasNext())
                {
                    sentence = rules.NextGeneration(sentence);

                    if (!bundle.Set("Sentence", sentence))
                    {
                        Debug.LogError("Cannot set 'Sentence' parameter in GetAndExecuteModule", gameObject);
                    }

                    int generation;
                    int iterations;
                    if (bundle.Get("Generation", out generation))
                    {
                        generation++;
                        if (bundle.Get("Iterations", out iterations))
                        {
                            if (generation > iterations)
                            {
                                //Max iterations reached.
                                return(false);
                            }
                        }
                        if (!bundle.Set("Generation", generation))
                        {
                            Debug.LogError("Cannot set 'Generation' parameter in GetAndExecuteModule", gameObject);
                        }
                    }
                    else
                    {
                        Debug.LogError("Cannot get 'Generation' parameter in GetAndExecuteModule", gameObject);
                    }
                }
                symbol = sentence.Next();
                if (symbol == '\0')
                {
                    return(false);                 //Sentence is empty! Caused if rules do not generate anything from previous
                }
            } while (!implementation.TryGetValue(symbol, out module));
            KeyValuePair <GameObject, Sentence> newPair = new KeyValuePair <GameObject, Sentence>(module, sentence);

            Profiler.EndSample();

            ExecuteModule(previous, newPair, bundle, symbol, baked);

            return(true);
        }
Ejemplo n.º 2
0
        public void AnyExecute(ParameterBundle bundle)
        {
            Profiler.BeginSample("LSystem.Seed.Execute");

            if (previous != null)
            {
                transform.position = previous.transform.position;
            }

            Sentence sentence = new Sentence(axiom);

            if (generateMode == GenMode.PreEdgeRewrite)
            {
                //Pre calculate final sentence.
                for (int i = 0; i < iterations; i++)
                {
                    sentence = rules.NextGeneration(sentence);
                }
                rules.Fertile = false;
                bundle.SetOrPut("Iterations", 0);
            }
            else // if(preGrow == GenMode.IterateNodeRewrite)
            {
                rules.Fertile = true;
                bundle.SetOrPut("Iterations", iterations);
            }

            bundle.SetOrPut("Generation", 0);
            bundle.SetOrPut("Sentence", sentence);
            bundle.SetOrPut("Implementations", implementations);
            bundle.SetOrPut("RuleSet", rules);


            if (!bundle.Exists("Position"))
            {
                bundle.Put("Position", transform.position);
            }

            if (!inheritRotation)
            {
                bundle.SetOrPut("Rotation", transform.rotation);
            }
            else if (!bundle.Exists("Rotation"))
            {
                bundle.Put("Rotation", transform.rotation);
            }

            // execute modules in the axiom
            if (!executed)
            {
                executed = true;

                if (baked)
                {
                    BakeNextModule(transform, sentence, implementations, rules, bundle);
                }
                else
                {
                    EnqueueProcessNextModule(transform, sentence, implementations, rules, bundle);
                }
            }
            Profiler.EndSample();
        }