private IEnumerator EvaludateSleep(GameLevel gameLevel)
        {
            LevelVariable variable  = gameLevel.GetVariable(this.Arguments[0]);
            int           sleepTime = (variable != null)
                ? int.Parse(variable.DataValue)
                : int.Parse(this.Arguments[0]);

            yield return(new WaitForSeconds(sleepTime));
        }
Esempio n. 2
0
        public bool ShouldContinue(GameLevel gameLevel)
        {
            if (this.Type == LoopType.None)
            {
                return(true);
            }

            bool breakConditionMet = this.Type == LoopType.For
                                         ? this.BreakCondition.ShouldContinue(gameLevel, this.Iterator.ToString())
                                         : this.BreakCondition.ShouldContinue(gameLevel);

            return(breakConditionMet);
        }
Esempio n. 3
0
        public string ToString(GameLevel gameLevel, string forIterator = null)
        {
            string conditionVariable = forIterator != null
                                           ? forIterator
                                           : this.VariableName;
            string        valueString   = this.Value;
            LevelVariable valueVariable = gameLevel.GetVariable(this.Value);

            if (valueVariable != null)
            {
                valueString = valueVariable.Name;
            }

            return
                ($"{conditionVariable} {StringValueAttribute.StringEnum.GetStringValue(this.Is)} {valueString}");
        }
Esempio n. 4
0
        public IEnumerator Evaluate(GameLevel gameLevel, int loopDepth, LevelLoop parentLoop)
        {
            // Main loop should always break out immediately upon finishing.
            if (!gameLevel.MainLoop.ShouldContinue(gameLevel))
            {
                gameLevel.CompleteLevel();
                yield break;
            }

            switch (this.Type)
            {
            case LoopType.Do:
                do
                {
                    yield return(EvaluateChildren(gameLevel, loopDepth, this));
                }while (ShouldContinue(gameLevel));
                yield break;

            case LoopType.For:
                for (this.Iterator = 0; ShouldContinue(gameLevel); this.Iterator++)
                {
                    yield return(EvaluateChildren(gameLevel, loopDepth, this));
                }
                yield break;

            case LoopType.While:
                while (ShouldContinue(gameLevel))
                {
                    yield return(EvaluateChildren(gameLevel, loopDepth, this));
                }
                yield break;

            case LoopType.If:
                if (ShouldContinue(gameLevel))
                {
                    yield return(EvaluateChildren(gameLevel, loopDepth, this));
                }
                yield break;

            case LoopType.None:
                yield return(EvaluateChildren(gameLevel, loopDepth, this));

                yield break;
            }

            gameLevel.UpdateLevelCode();
        }
        private IEnumerator EvaluateAssignment(GameLevel gameLevel)
        {
            LevelVariable variable           = gameLevel.GetVariable(this.Arguments[0]);
            LevelVariable assignmentVariable = gameLevel.GetVariable(this.Arguments[1]);

            if (assignmentVariable != null)
            {
                variable.DataValue   = assignmentVariable.DataValue;
                variable.ObjectValue = assignmentVariable.ObjectValue;
            }
            else
            {
                variable.DataValue = this.Arguments[1];
            }

            yield return(null);
        }
        private IEnumerator EvaluateMethod(GameLevel gameLevel)
        {
            LevelVariable[] variables = new LevelVariable[this.Arguments.Length];
            for (int i = 0; i < variables.Length; i++)
            {
                variables[i] = gameLevel.GetVariable(this.Arguments[i]);
                if (variables[i] == null)
                {
                    variables[i]           = new LevelVariable();
                    variables[i].Name      = "argument";
                    variables[i].DataValue = this.Arguments[i];
                }
            }

            this.InvokeMethod.Invoke(variables);
            yield return(null);
        }
        public IEnumerator Evaluate(GameLevel gameLevel)
        {
            if (this.Type == CodeStatementType.Assignment)
            {
                yield return(EvaluateAssignment(gameLevel));
            }

            else if (this.Type == CodeStatementType.Method)
            {
                yield return(EvaluateMethod(gameLevel));
            }

            else if (this.Type == CodeStatementType.Sleep)
            {
                yield return(EvaludateSleep(gameLevel));
            }

            gameLevel.UpdateLevelCode();
        }
Esempio n. 8
0
        private IEnumerator EvaluateChildren(GameLevel gameLevel, int loopDepth, LevelLoop parentLoop)
        {
            // Main loop should always break out immediately upon finishing.
            if (!gameLevel.MainLoop.ShouldContinue(gameLevel))
            {
                gameLevel.CompleteLevel();
                yield break;
            }

            foreach (CodeStatement statement in this.Statements)
            {
                yield return(statement.Evaluate(gameLevel));
            }

            foreach (LevelLoop nestedLoop in this.NestedLoops)
            {
                yield return(nestedLoop.Evaluate(gameLevel, loopDepth + 1, parentLoop));
            }
            gameLevel.UpdateLevelCode();
            yield return(null);
        }
Esempio n. 9
0
        public bool ShouldContinue(GameLevel gameLevel, string leftValue = null)
        {
            // TODO: Shoddy for loop.
            if (this.VariableName == null)
            {
                return(false);
            }

            if (leftValue == null)
            {
                LevelVariable leftVariable = gameLevel.GetVariable(this.VariableName);
                if (leftVariable == null)
                {
                    return(false);
                }
                leftValue = leftVariable.AsString();
            }

            LevelVariable rightVariable = gameLevel.GetVariable(this.Value);
            string        rightValue    = rightVariable != null
                                    ? rightVariable.AsString()
                                    : this.Value;

            switch (this.Is)
            {
            case Evaluator.Equals:
                return(leftValue == rightValue);

            case Evaluator.LessThan:
                return(int.Parse(leftValue) < int.Parse(rightValue));

            case Evaluator.MoreThan:
                return(int.Parse(leftValue) > int.Parse(rightValue));

            case Evaluator.Not:
                return(leftValue != rightValue);
            }

            return(false);
        }
Esempio n. 10
0
        public string ToCode(GameLevel gameLevel, int loopDepth, LevelLoop parentLoop)
        {
            void GetScopeColor(bool breaks, StringBuilder stringBuilder)
            {
                // Determine color of loop code.
                string colorCode = "#0F0";

                if (breaks)
                {
                    switch (this.Type)
                    {
                    case LoopType.None:
                    case LoopType.Do:
                        if (parentLoop != null &&
                            parentLoop.ShouldContinue(gameLevel))
                        {
                            colorCode = "#FF0";
                        }
                        else
                        {
                            colorCode = "#F00";
                        }

                        break;

                    case LoopType.For:
                    case LoopType.While:
                    case LoopType.If:
                        colorCode = "#F00";
                        break;
                    }
                }

                stringBuilder.Append($"<color={colorCode}>");
            }

            bool breakConditionMet = !ShouldContinue(gameLevel);

            StringBuilder code = new StringBuilder();

            if (parentLoop != null &&
                parentLoop.ShouldContinue(gameLevel) &&
                gameLevel.MainLoop.ShouldContinue(gameLevel))
            {
                GetScopeColor(breaks: breakConditionMet, stringBuilder: code);
            }

            // Determine how much to indent the loop code.
            string indentTab = "";

            for (int i = 0; i < loopDepth; i++)
            {
                indentTab += GameLevel.IndentString;
            }

            // Text for beginning of loop.
            switch (this.Type)
            {
            case LoopType.Do:
                code.Append("do\n")
                .Append("{\n");
                break;

            case LoopType.While:
                code.Append($"while ({this.BreakCondition.ToString(gameLevel)})\n")
                .Append("{\n");
                break;

            case LoopType.For:
                string iterator = ((char)(104 + loopDepth)).ToString();     // Starts at i
                code.Append($"for ({iterator} = 0; {this.BreakCondition.ToString(gameLevel, iterator)}; {iterator}++)\n")
                .Append("{\n");
                break;

            case LoopType.If:
                code.Append($"if ({this.BreakCondition.ToString(gameLevel)})\n")
                .Append("{\n");
                break;
            }

            // Fill loop body with statements.
            foreach (CodeStatement statement in this.Statements)
            {
                // Hack, first loop doesn't indent statements like it should.
                string extraIndent = loopDepth == 0
                                         ? GameLevel.IndentString
                                         : indentTab;
                code.Append($"{extraIndent}{statement.ToCode()}\n");
            }

            // Add additional line between statements and any nested loops.
            if (this.Statements.Count > 0 &&
                this.NestedLoops.Count > 0)
            {
                code.Append("\n");
            }

            // Create code for nested loops.
            foreach (LevelLoop nestedLoop in this.NestedLoops)
            {
                code.Append($"{nestedLoop.ToCode(gameLevel, loopDepth + 1, this)}\n");
            }



            // End of loop.
            GetScopeColor(breaks: breakConditionMet, stringBuilder: code);
            if (this.Type == LoopType.Do &&
                this.BreakCondition != null)
            {
                code.Append("} while (" + this.BreakCondition.ToString(gameLevel) + ")");
            }
            else if (this.Type == LoopType.None)
            {
            }
            else
            {
                code.Append("}");
            }
            code.Append("</color>");

            // Apply indent to each line.
            string[] codeLines = code.ToString().Split('\n');
            for (int i = 0; i < codeLines.Length; i++)
            {
                codeLines[i] = indentTab + codeLines[i];
            }

            // Final code.
            string result = string.Join("\n", codeLines);

            if (parentLoop != null &&
                !parentLoop.ShouldContinue(gameLevel))
            {
                result += "</color>";
            }

            return(result);
        }