Пример #1
0
    public virtual void ReturnToStep(TimeStepData timeStep)
    {
        if (gameObject != null)
        {
            switch (component.type)
            {
            case "semaphore":
                component.configuration.value          = timeStep.GetSemaphore(component.id).open;
                GetComponent <SpriteRenderer>().sprite = GameManager.Instance.GetGridManager().GetSprite(component);
                break;

            case "pickup":
                component.configuration.value = timeStep.GetPickup(component.id).available;
                if (component.configuration.value >= 0)
                {
                    iTween.ColorTo(gameObject, Color.white, 0.05f); iTween.ScaleTo(gameObject, Vector3.one, 0.05f);
                }
                else if (component.configuration.value < 0)
                {
                    iTween.ColorTo(gameObject, new Color(0.5f, 0.5f, 0.5f), 0.05f); iTween.ScaleTo(gameObject, Vector3.one * 0.8f, 0.05f);
                }
                break;

            case "conditional":
                component.configuration.current = timeStep.GetConditional(component.id).current;
                for (int i = 0; i < conditionalDirections.Count; i++)
                {
                    SpriteRenderer instanceSpriteRenderer = conditionalDirections[i].GetComponent <SpriteRenderer>();
                    if (i == component.configuration.current)
                    {
                        instanceSpriteRenderer.color = new Color(1f, 1f, 1f, 1f);
                    }
                    else
                    {
                        instanceSpriteRenderer.color = new Color(1f, 1f, 1f, 0.5f);
                    }
                }
                break;

            case "signal":
                component.configuration.passed = timeStep.GetSignal(component.id).passed;
                break;
            }
        }
    }
Пример #2
0
    IEnumerator ParseSteps()
    {
        playerInteraction.playerInteraction_UI.loadingText.text = "Parsing...";
        currentStep = 0;
        paused = false;
        Level lvl = GameManager.Instance.GetDataManager().currentLevelData;
        if(lvl.execution.Count > 0)
        {
            int maxStep = 0;
            stepDictionary = new Dictionary<int, List<StepData>>();
            Dictionary<int, List<int>> componentStepsDictionary = new Dictionary<int, List<int>>();
            timeSteps = new List<TimeStepData>();
            List<GridComponent> threads = new List<GridComponent>();

            foreach (GridComponent g in lvl.components)
            {
                if (g.type == "thread")
                {
                    threads.Add(g);
                }
            }

            yield return new WaitForSeconds(1f);
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            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);
                }
                if(stopwatch.ElapsedMilliseconds > 1000 / 60)
                {
                    stopwatch.Stop();
                    stopwatch.Reset();
                    yield return new WaitForEndOfFrame();
                    stopwatch.Start();
                }
            }

            //create in between steps for thread movements
            for (int i = 0; i <= maxStep; i++)
            {
                if (stepDictionary.ContainsKey(i) == false)
                    stepDictionary.Add(i, new List<StepData>());
                if (stepDictionary[i] == null)
                    stepDictionary[i] = new List<StepData>();
                if (stepDictionary[i].Count == 0)
                {
                    for (int j = 0; j < threads.Count; j++)
                    {
                        Vector2 prevPos = new Vector2();
                        if (i > 0)
                        {
                            for (int k = 0; k < stepDictionary[i - 1].Count; k++)
                            {
                                if (stepDictionary[i - 1][k].eventType == "M" && stepDictionary[i - 1][k].componentID == threads[j].id)
                                {
                                    prevPos = stepDictionary[i - 1][k].componentPos;
                                }
                            }
                        }
                        Vector2 nextPos = prevPos;
                        bool end = false;
                        for (int k = i + 1; k < maxStep+1; k++)
                        {
                            if (stepDictionary.ContainsKey(k))
                            {
                                for (int l = 0; l < stepDictionary[k].Count; l++)
                                {
                                    if (stepDictionary[k][l].eventType == "M" && stepDictionary[k][l].componentID == threads[j].id)
                                    {
                                        if (stepDictionary[k][l].componentPos != prevPos)
                                        {
                                            if (Vector2.Distance(prevPos, stepDictionary[k][l].componentPos) > 1 || Vector2.Distance(prevPos, stepDictionary[k][l].componentPos) < -1)
                                            {
                                                if (k - Mathf.Abs(Vector2.Distance(prevPos, stepDictionary[k][l].componentPos)) == i - 1)
                                                {
                                                    if (prevPos.x != stepDictionary[k][l].componentPos.x)
                                                    {
                                                        if (prevPos.x > stepDictionary[k][l].componentPos.x)
                                                        {
                                                            nextPos = new Vector2(nextPos.x - 1, nextPos.y);
                                                        }
                                                        else
                                                        {
                                                            nextPos = new Vector2(nextPos.x + 1, nextPos.y);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (prevPos.y > stepDictionary[k][l].componentPos.y)
                                                        {
                                                            nextPos = new Vector2(nextPos.x, nextPos.y - 1);
                                                        }
                                                        else
                                                        {
                                                            nextPos = new Vector2(nextPos.x, nextPos.y + 1);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        end = true;
                                        break;
                                    }
                                }
                                if (end == true)
                                {
                                    break;
                                }
                            }
                        }

                        StepData newStep = new StepData();
                        newStep.componentID = threads[j].id;
                        newStep.componentPos = nextPos;
                        newStep.eventType = "M";
                        newStep.timeStep = i;

                        stepDictionary[i].Add(newStep);
                    }

                }
                else
                {
                    List<bool> hasTimeStepData = new List<bool>();
                    for (int j = 0; j < threads.Count; j++)
                    {
                        hasTimeStepData.Add(false);
                    }

                    for (int j = 0; j < stepDictionary[i].Count; j++)
                    {
                        if (stepDictionary[i][j].eventType == "M")
                        {
                            for (int k = 0; k < threads.Count; k++)
                            {
                                if (threads[k].id == stepDictionary[i][j].componentID)
                                {
                                    hasTimeStepData[k] = true;
                                }
                            }
                        }
                    }

                    for (int j = 0; j < hasTimeStepData.Count; j++)
                    {
                        if (hasTimeStepData[j] == false)
                        {
                            Vector2 prevPos = new Vector2();
                            for (int k = 0; k < stepDictionary[i - 1].Count; k++)
                            {
                                if (stepDictionary[i - 1][k].eventType == "M" && stepDictionary[i - 1][k].componentID == threads[j].id)
                                {
                                    prevPos = stepDictionary[i - 1][k].componentPos;
                                }
                            }
                            Vector2 nextPos = prevPos;
                            bool end = false;
                            for (int k = i + 1; k < maxStep+1; k++)
                            {
                                if (stepDictionary.ContainsKey(k))
                                {
                                    for (int l = 0; l < stepDictionary[k].Count; l++)
                                    {
                                        if (stepDictionary[k][l].eventType == "M" && stepDictionary[k][l].componentID == threads[j].id)
                                        {
                                            if (stepDictionary[k][l].componentPos != prevPos)
                                            {
                                                if (Vector2.Distance(prevPos, stepDictionary[k][l].componentPos) > 1 || Vector2.Distance(prevPos, stepDictionary[k][l].componentPos) < -1)
                                                {
                                                    if (k - Mathf.Abs(Vector2.Distance(prevPos, stepDictionary[k][l].componentPos)) == i - 1)
                                                    {
                                                        if (prevPos.x != stepDictionary[k][l].componentPos.x)
                                                        {
                                                            if (prevPos.x > stepDictionary[k][l].componentPos.x)
                                                            {
                                                                nextPos = new Vector2(nextPos.x - 1, nextPos.y);
                                                            }
                                                            else
                                                            {
                                                                nextPos = new Vector2(nextPos.x + 1, nextPos.y);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (prevPos.y > stepDictionary[k][l].componentPos.y)
                                                            {
                                                                nextPos = new Vector2(nextPos.x, nextPos.y - 1);
                                                            }
                                                            else
                                                            {
                                                                nextPos = new Vector2(nextPos.x, nextPos.y + 1);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            end = true;
                                            break;
                                        }
                                    }
                                    if (end == true)
                                    {
                                        break;
                                    }
                                }
                            }

                            StepData newStep = new StepData();
                            newStep.componentID = threads[j].id;
                            newStep.componentPos = nextPos;
                            newStep.eventType = "M";
                            newStep.timeStep = i;

                            stepDictionary[i].Add(newStep);
                        }
                    }
                }
                if (stopwatch.ElapsedMilliseconds > 1000 / 60)
                {
                    stopwatch.Stop();
                    stopwatch.Reset();
                    yield return new WaitForEndOfFrame();
                    stopwatch.Start();
                }
            }

            yield return new WaitForSeconds(1f);
            TimeStepData timeStep = new TimeStepData();
            for (int i = 0; i < lvl.components.Count; i++)
            {
                switch (lvl.components[i].type)
                {
                    case "thread":
                        ThreadData thread = new ThreadData();
                        thread.id = lvl.components[i].id;
                        thread.pos = Vector2.zero;
                        switch (lvl.components[i].configuration.initial_direction)
                        {
                            case "North": thread.rotation = new Vector3(0, 0, 90); break;
                            case "South": thread.rotation = new Vector3(0, 0, -90); break;
                            case "East": thread.rotation = new Vector3(0, 0, 0); break;
                            case "West": thread.rotation = new Vector3(0, 0, 180); break;
                        }
                        timeStep.threads.Add(thread);
                        break;

                    case "pickup":
                        PickupData pickup = new PickupData();
                        pickup.id = lvl.components[i].id;
                        pickup.available = lvl.components[i].configuration.value;
                        timeStep.pickups.Add(pickup);
                        break;

                    case "delivery":
                        DeliveryData delivery = new DeliveryData();
                        delivery.id = lvl.components[i].id;
                        delivery.deliveries = 0;
                        timeStep.deliveryPoints.Add(delivery);
                        break;

                    case "semaphore":
                        SemaphoreData semaphore = new SemaphoreData();
                        semaphore.id = lvl.components[i].id;
                        semaphore.open = lvl.components[i].configuration.value;
                        timeStep.sempahores.Add(semaphore);
                        break;

                    case "conditional":
                        ConditionalData conditional = new ConditionalData();
                        conditional.id = lvl.components[i].id;
                        conditional.current = lvl.components[i].configuration.current;
                        conditional.directions = lvl.components[i].configuration.directions;
                        timeStep.conditionals.Add(conditional);
                        break;

                }
                if (stopwatch.ElapsedMilliseconds > 1000 / 60)
                {
                    stopwatch.Stop();
                    stopwatch.Reset();
                    yield return new WaitForEndOfFrame();
                    stopwatch.Start();
                }
            }

            yield return new WaitForSeconds(1f);
            for (int i = 0; i < stepDictionary.Count; i++)
            {
                if (i != 0)
                {
                    timeStep = timeStep.Copy(timeSteps[i - 1]);
                    timeStep.timeStep = i;
                    timeStep.previousStep = timeSteps[i - 1];
                    timeSteps[i - 1].nextStep = timeStep;
                }
                for (int j = 0; j < stepDictionary[i].Count; j++)
                {
                    switch (stepDictionary[i][j].eventType)
                    {
                        case "M":
                            timeStep.GetThread(stepDictionary[i][j].componentID).pos = stepDictionary[i][j].componentPos;
                            if (i != 0)
                            {
                                Vector2 difference = timeSteps[timeSteps.Count - 1].GetThread(stepDictionary[i][j].componentID).pos - timeStep.GetThread(stepDictionary[i][j].componentID).pos;
                                if (difference.x > 0) { timeStep.GetThread(stepDictionary[i][j].componentID).rotation = new Vector3(0, 0, 180); }
                                else if (difference.x < 0) { timeStep.GetThread(stepDictionary[i][j].componentID).rotation = new Vector3(0, 0, 0); }
                                else if (difference.y > 0) { timeStep.GetThread(stepDictionary[i][j].componentID).rotation = new Vector3(0, 0, 90); }
                                else if (difference.y < 0) { timeStep.GetThread(stepDictionary[i][j].componentID).rotation = new Vector3(0, 0, -90); }
                            }
                            break;
                        case "D":
                            if (stepDictionary[i][j].componentStatus.delivered_to != 0)
                            {
                                timeStep.GetDeliveryPoint(stepDictionary[i][j].componentStatus.delivered_to).deliveries++;
                            }
                            break;
                        case "E":
                            if (timeStep.GetSemaphore(stepDictionary[i][j].componentID) != null)
                            {
                                timeStep.GetSemaphore(stepDictionary[i][j].componentID).open = stepDictionary[i][j].componentStatus.value;
                            }
                            else if (timeStep.GetPickup(stepDictionary[i][j].componentID) != null)
                            {
                                timeStep.GetPickup(stepDictionary[i][j].componentID).available = stepDictionary[i][j].componentStatus.available;
                            }
                            else if (timeStep.GetThread(stepDictionary[i][j].componentID) != null)
                            {
                                if (stepDictionary[i][j].componentStatus.payload != null)
                                {
                                    timeStep.GetThread(stepDictionary[i][j].componentID).DisablePackages();
                                    for (int k = 0; k < stepDictionary[i][j].componentStatus.payload.Length; k++)
                                    {
                                        for (int l = 0; l < i; l++)
                                        {
                                            if (timeSteps[l].GetThread(stepDictionary[i][j].componentID).ContainsPackage(stepDictionary[i][j].componentStatus.payload[k]) == false)
                                            {
                                                PackageData package = new PackageData();
                                                package.active = false;
                                                package.id = stepDictionary[i][j].componentStatus.payload[k];
                                                timeSteps[l].GetThread(stepDictionary[i][j].componentID).packages.Add(package);
                                            }
                                        }
                                        if (timeStep.GetThread(stepDictionary[i][j].componentID).ContainsPackage(stepDictionary[i][j].componentStatus.payload[k]))
                                        {
                                            timeStep.GetThread(stepDictionary[i][j].componentID).GetPackage(stepDictionary[i][j].componentStatus.payload[k]).active = true;
                                        }
                                        else
                                        {
                                            PackageData package = new PackageData();
                                            package.active = true;
                                            package.id = stepDictionary[i][j].componentStatus.payload[k];
                                            timeStep.GetThread(stepDictionary[i][j].componentID).packages.Add(package);
                                        }
                                    }
                                }
                            }
                            else if (timeStep.GetConditional(stepDictionary[i][j].componentID) != null)
                            {
                                if (stepDictionary[i][j].componentStatus.current != -1)
                                {
                                    timeStep.GetConditional(stepDictionary[i][j].componentID).current = stepDictionary[i][j].componentStatus.current;
                                }
                            }
                            break;
                    }
                }
                timeSteps.Add(timeStep);
                if (stopwatch.ElapsedMilliseconds > 1000 / 60)
                {
                    stopwatch.Stop();
                    stopwatch.Reset();
                    yield return new WaitForEndOfFrame();
                    stopwatch.Start();
                }
            }

            playerInteraction.playerInteraction_UI.playbackSlider.maxValue = maxStep;
            playerInteraction.playerInteraction_UI.loadingOverlay.ClosePanel();
            playerInteraction.playerInteraction_UI.playbackControls.gameObject.SetActive(true);
            playerInteraction.playerInteraction_UI.stopSimulationButton.interactable = true;
            playerInteraction.playerInteraction_UI.stopSimulationButton.gameObject.SetActive(true);
            playerInteraction.playerInteraction_UI.pauseSimulationButton.interactable = true;
            playerInteraction.playerInteraction_UI.pauseSimulationButton.gameObject.SetActive(true);
            playerInteraction.playerInteraction_UI.playbackSlider.interactable = true;
            playerInteraction.playerInteraction_UI.playbackSlider.gameObject.SetActive(true);
            yield return PlaySimulation(maxStep);
        }
        else
        {
            yield return null;
        }
    }