示例#1
0
        public void GenerateStep()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Generation Step");
            UnityEditor.Undo.RecordObject(this, "Generation Step");
            UnityEditor.Undo.RecordObject(patternView, "Generation Step");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif

            ReplacementEngine re = GetComponent <ReplacementEngine>();
            re.ReplaceMatch();
            PatternView.UpdateView();
        }
示例#2
0
        public void Generate()
        {
            ReplacementEngine re = GetComponent <ReplacementEngine>();

            InitializeGeneration();

            if (re != null)
            {
                for (int i = 0; i < maxGenerationSteps; i++)
                {
                    if (!re.ReplaceMatch())
                    {
                        break;
                    }
                }
            }
        }
        private GenerationStepResult GenerationStepInternal()
        {
            ReplacementEngine re = GetComponent <ReplacementEngine>();

            if (printDebugInfo)
            {
                Debug.Log("Generation step, instruction " + currentInstruction + ", applications left: " + applicationCounter);
            }

            if (currentInstruction >= instructions.Length)
            {
                return(GenerationStepResult.EndOfFile);
            }

            bool        visibleChange = false;
            Instruction instr         = instructions[currentInstruction];

            switch (instr.type)
            {
            case InstructionType.Skip:
                // skip to after next return
                while (currentInstruction < instructions.Length &&
                       instructions[currentInstruction].type != InstructionType.Return)
                {
                    currentInstruction++;
                }
                currentInstruction++;
                break;

            case InstructionType.Return:
                if (gotoStack.Count > 0)
                {
                    ProgramState ps = gotoStack.Pop();
                    currentInstruction = ps.instruction;
                    applicationCounter = ps.applicationCounter;
                }
                break;

            case InstructionType.Repeat:
                // we just set the counter, the execution is handled by the repeatable instructions
                System.Random sysRandom = GetComponent <ReplacementEngine>().systemRandom;
                int           repeats;
                if (sysRandom != null)
                {
                    repeats = instr.minRepeats + Mathf.FloorToInt((float)sysRandom.NextDouble() * (instr.maxRepeats - instr.minRepeats));
                }
                else
                {
                    repeats = Random.Range(instr.minRepeats, instr.maxRepeats);
                }
                applicationCounter = Mathf.Max(1, repeats);
                currentInstruction++;
                break;

            // repeatable instructions
            case InstructionType.Goto:
                if (UseApplication())
                {
                    gotoStack.Push(new ProgramState(currentInstruction, applicationCounter));
                    currentInstruction = instr.gotoPosition;
                }
                else
                {
                    currentInstruction++;
                }
                break;

            case InstructionType.ApplyRule:
            case InstructionType.ApplyRuleRange:
                if (UseApplication())
                {
                    // apply rule once and count down
                    if (instr.isSweep)
                    {
                        visibleChange = re.SweepReplace(instr.filter, instr.type == InstructionType.ApplyRuleRange);
                    }
                    else
                    {
                        visibleChange = re.ReplaceMatch(instr.filter, instr.type == InstructionType.ApplyRuleRange);
                    }
                }
                else
                {
                    currentInstruction++;
                }
                break;

            case InstructionType.Subdivide:
                visibleChange = true;
                re.DoSubdividision(instr.dimensions);
                currentInstruction++;
                break;

            case InstructionType.Error:
                currentInstruction++;
                break;
            }

            return(visibleChange? GenerationStepResult.VisibleChange : GenerationStepResult.NoVisibleChange);
        }