IEnumerator SimulationBehavior()
    {
        Level lvl               = GameManager.Instance.GetDataManager().currentLevelData;
        int   currentStep       = 0;
        int   maxStep           = 0;
        int   maxGoalsCompleted = 0;
        Dictionary <int, List <StepData> > stepDictionary           = new Dictionary <int, List <StepData> >();
        Dictionary <int, List <int> >      componentStepsDictionary = new Dictionary <int, List <int> >();

        for (int i = 0; i < lvl.execution.Count; i++)
        {
            StepData step = lvl.execution[i];

            if (step.timeStep > maxStep)
            {
                maxStep = step.timeStep;
            }

            if (step.eventType == "M")
            {
                if (!componentStepsDictionary.ContainsKey(step.componentID))
                {
                    componentStepsDictionary.Add(step.componentID, new List <int>()); componentStepsDictionary[step.componentID].Add(i);
                }
                else
                {
                    componentStepsDictionary[step.componentID].Add(i);
                }
            }

            if (stepDictionary.ContainsKey(step.timeStep))
            {
                if (step.eventType == "D")
                {
                    stepDictionary[step.timeStep].Insert(0, step);
                }
                else
                {
                    stepDictionary[step.timeStep].Add(step);
                }
            }
            else
            {
                stepDictionary[step.timeStep] = new List <StepData>();
                stepDictionary[step.timeStep].Add(step);
            }
        }

        foreach (int componentId in componentStepsDictionary.Keys)
        {
            //componentStepsDictionary[componentId].Sort();
            for (int listIndex = 0; listIndex < componentStepsDictionary[componentId].Count - 1; listIndex++)
            {
                int executionIndex     = componentStepsDictionary[componentId][listIndex];
                int nextExecutionIndex = componentStepsDictionary[componentId][listIndex + 1];
                lvl.execution[executionIndex].SetNextStep(nextExecutionIndex);
            }
        }

        while (interactionPhase == InteractionPhases.simulation && currentStep <= maxStep)
        {
            if (stepDictionary.ContainsKey(currentStep))
            {
                foreach (StepData step in stepDictionary[currentStep])
                {
                    if (step.componentID == 0)
                    {
                        if (step.componentStatus == null)
                        {
                            continue;
                        }
                        if (step.componentStatus.goals_completed != null)
                        {
                            if (maxGoalsCompleted < step.componentStatus.goals_completed)
                            {
                                maxGoalsCompleted = step.componentStatus.goals_completed;
                            }
                        }
                        if (step.componentStatus.final_condition != null && step.componentStatus.final_condition != -1)
                        {
                            string goalString = "";
                            switch (step.componentStatus.final_condition)
                            {
                            case 2:
                            case 8:
                            case 10:
                                goalString = "Successfully completed the level!";
                                break;

                            default:
                                goalString = "";
                                if ((step.componentStatus.final_condition & 1) != 0)
                                {
                                    goalString += "You hit a dead end. ";
                                }
                                if ((step.componentStatus.final_condition & 16) != 0)
                                {
                                    goalString += "You missed some deliveries. ";
                                }
                                if ((step.componentStatus.final_condition & 32) != 0)
                                {
                                    goalString += "You " +
                                                  "have starvation. ";
                                }
                                goalString += "Sorry, try again!";
                                break;
                            }

                            yield return(StartCoroutine(playerInteraction_UI.TriggerGoalPopUp(goalString)));
                        }
                    }
                    else
                    {
                        GridObjectBehavior g = GameManager.Instance.GetGridManager().GetGridObjectByID(step.componentID);
                        if (g != null)
                        {
                            g.DoStep(step);
                        }
                        else
                        {
                            Debug.Log("Could not find " + step.componentID);
                        }
                    }
                }
                yield return(new WaitForSeconds(0.5f));
            }
            currentStep++;
            Debug.Log(currentStep);
        }


        //yield return new WaitForSeconds(1f);
        //todo: switch statement of the selected goal option

        ResetStartValues();

        switch (playerInteraction_UI.goalOverlay.userInput)
        {
        case PlayerInteraction_UI.Goal_UIOverlay.UserInputs.exit:
        case PlayerInteraction_UI.Goal_UIOverlay.UserInputs.levels:
            TriggerPlayPhaseEnd();
            EndSimulation();
            break;

        case PlayerInteraction_UI.Goal_UIOverlay.UserInputs.replay:

            interactionPhase = InteractionPhases.ingame_default;
            GameManager.Instance.TriggerLevelSimulation(LinkJava.SimulationFeedback.none);

            break;

        default:
            GameManager.Instance.TriggerLoadLevel();
            EndSimulation();
            break;
        }
    }
Beispiel #2
0
    IEnumerator PlaySimulation(int maxStep)
    {
        int maxGoalsCompleted = 0;
        currentStep = 0;
        paused = false;
        playerInteraction.playerInteraction_UI.playbackSlider.value = 0;
        bool nextLevelButtonVisibility = false;
        while (currentStep <= maxStep)
        {
            if (paused != true)
            {
                if (stepDictionary.ContainsKey(currentStep))
                {
                    float waitTime = 0f;
                    int count = 0;
                    foreach (StepData step in stepDictionary[currentStep])
                    {
                        count++;
                        if (step.componentID == 0)
                        {
                            yield return new WaitForSeconds(0.5f);
                            if (step.componentStatus == null) continue;
                            if (step.componentStatus.goals_completed != null && step.componentStatus.final_condition != -1)
                            {
                                if (maxGoalsCompleted < step.componentStatus.goals_completed) { maxGoalsCompleted = step.componentStatus.goals_completed; }
                            }
                            if (step.componentStatus.final_condition != null && step.componentStatus.final_condition != -1)
                            {
                                playerInteraction.score.stepCount = maxStep;
                                string titleFormatString = "<size=18><b>{0}</b></size>\n";
                                string titleString = "";
                                string goalString = "";
                                string levelFileName = "";
                                if (GameManager.Instance.currentLevelReferenceObject != null) levelFileName = GameManager.Instance.currentLevelReferenceObject.file;

                                switch (step.componentStatus.final_condition)
                                {
                                    case 2:
                                    case 8:
                                    case 10:
                                        success = true;
                                        //if "test" versus "submit" change this text
                                        if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.ME)
                                        {
                                            titleString = "SUCCESSFUL SOLUTION";
                                            goalString += "\n• Congratulations! This solution will always work. Please proceed to the next level.";
                                            playerInteraction.score.completed = true;

                                            //get current score
                                            int currentScore = GameManager.Instance.GetScoreManager().GetCalculatedScore(playerInteraction.score);
                                            //update saved score
                                            GameManager.Instance.GetScoreManager().ScoreLevel(playerInteraction.score);
                                            int lvlScore = GameManager.Instance.GetScoreManager().GetCalculatedScore(playerInteraction.score.index);

                                            GameManager.Instance.currentLevelReferenceObject.completionRank = lvlScore;
                                            GameManager.Instance.GetDataManager().UpdateLevelRank(levelFileName, lvlScore);

                                            //use 'current' not 'best' score for the feedback
                                            playerInteraction.playerInteraction_UI.goalOverlay.SetFeedbackScore(currentScore);

                                            nextLevelButtonVisibility = true;
                                        }
                                        else if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.Play)
                                        {
                                            titleString = "TEST COMPLETE";
                                            goalString += "\n• This solution was successful this time. Submit to check if it's always successful.";
                                            playerInteraction.playerInteraction_UI.goalOverlay.SetFeedbackScore(-1);
                                        }
                                        break;
                                    default:
                                        success = false;
                                        //if "test" versus "submit" change this text
                                        if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.ME)
                                        {
                                            titleString = "UNSUCCESSFUL SOLUTION";
                                        }
                                        else if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.Play)
                                        {
                                            titleString = "TEST COMPLETE";
                                            playerInteraction.playerInteraction_UI.goalOverlay.SetFeedbackScore(-1);
                                        }

                                        goalString = "";
                                        if ((step.componentStatus.final_condition & 1) != 0)
                                        {
                                            goalString += "\n• Make sure arrows aren't blocked.";
                                        }
                                        if ((step.componentStatus.final_condition & 4) != 0)
                                        {
                                            goalString += "\n• This solution was unsuccessful.";
                                        }
                                        if ((step.componentStatus.final_condition & 16) != 0)
                                        {
                                            goalString += "\n• Make sure arrows can't deliver at the same time.";
                                        }
                                        if ((step.componentStatus.final_condition & 32) != 0)
                                        {
                                            goalString += "\n• Make sure all arrows can move.";
                                        }
                                        if ((step.componentStatus.final_condition & 64) != 0)
                                        {
                                            goalString += "\n• Make sure arrows don't get caught in an infinite loop.";
                                        }
                                        if ((step.componentStatus.final_condition & 512) != 0)
                                        {
                                            goalString += "\n• Wrong turn! Check the Flow Arrows on the right of the screen.";
                                        }
                                        List<string> errorFeedback = new List<string>();
                                        foreach (string errorKey in step.componentStatus.goal_descriptions)
                                        {
                                            string key = errorKey.Substring(0, 3);
                                            if (Constants.GoalFeedbackValues.GoalErrorFeedback.ContainsKey(key))
                                            {
                                                if (!errorFeedback.Contains(Constants.GoalFeedbackValues.GoalErrorFeedback[key]))
                                                    errorFeedback.Add(Constants.GoalFeedbackValues.GoalErrorFeedback[key]);
                                            }
                                        }
                                        foreach (string s in errorFeedback) goalString += ("• " + s + "\n");

                                        break;
                                }

                                if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.ME)
                                {
                                    playerInteraction.playerInteraction_UI.goalOverlay.levels.gameObject.SetActive(true);
                                }
                                else if (GameManager.Instance.GetCurrentSimulationType() == LinkJava.SimulationTypes.Play)
                                {
                                    playerInteraction.playerInteraction_UI.goalOverlay.levels.gameObject.SetActive(false);
                                }

                                yield return StartCoroutine(playerInteraction.playerInteraction_UI.TriggerGoalPopUp(titleString, goalString));
                            }
                        }
                        else
                        {
                            GridObjectBehavior g = GameManager.Instance.GetGridManager().GetGridObjectByID(step.componentID);
                            if (g != null)
                            {
                                if (g.timeStep == null)
                                    g.SetTimestep(timeSteps[currentStep]);
                                float time = g.DoStep(step, stepDictionary);
                                if (time > waitTime)
                                    waitTime = time;
                            }
                            else { Debug.Log("Could not find " + step.componentID); }
                        }

                        if (PlayerInteraction_GamePhaseBehavior.onSimulationStep != null)
                        {
                            PlayerInteraction_GamePhaseBehavior.onSimulationStep(step);
                            //PauseSimulation();
                            //UnpauseAfterDelay(5);
                        }
                    }
                    while (paused) yield return new WaitForSeconds(0.1f);
                    yield return new WaitForSeconds(waitTime);
                }
                currentStep++;
                playerInteraction.playerInteraction_UI.playbackSlider.value = currentStep;
                //Debug.Log(currentStep);
            }
            else
            {
                yield return new WaitForEndOfFrame();
            }
        }

        yield return StartCoroutine(FinishSimulation());
    }