コード例 #1
0
    void Update()
    {
        if (Input.GetButtonUp("Instruction"))
        {
            showGeneralInstructions();
            Debug.Log("Pressing Button");
        }

        if (Input.GetButtonUp("Restart"))
        {
            reloadTable(boxesToSort);
        }

        // TODO Handle with events instead of checking every frame
        if (triggerLeft && triggerRight && boxLeft && boxRight)
        {
            if (boxLeft && boxRight && boxLeft.IsGrabbed() && boxRight.IsGrabbed() && !triggerLeft.HasObject() && !triggerRight.HasObject())
            {
                bothPickedUp = true;
            }

            if (bothPickedUp && (triggerLeft.isCorrect || triggerRight.isFinal) && triggerLeft.HasObject() && triggerRight.HasObject())
            {
                bothPickedUp = false;
                triggerLeft.DoneInIteration = true;
                if (triggerRight.isFinal)
                {
                    triggerRight.DoneInIteration = true;
                }
                boxLeft.SetGrabbable(false);
                boxRight.SetGrabbable(false);
                ++leftIndex;  // TODO Create method in algorithm to get next index (this only works with bubble sort)
                ++rightIndex; // TODO Create method in algorithm to get next index (this only works with bubble sort)
                if (triggerRight.isFinal || rightIndex >= sortBoxTriggers.Length)
                {
                    leftIndex  = 0; // TODO Create method in algorithm to get next index (this only works with bubble sort)
                    rightIndex = 1; // TODO Create method in algorithm to get next index (this only works with bubble sort)
                }

                triggerLeft  = sortBoxTriggers[leftIndex].GetComponent <SortBoxTriggerScript>();
                triggerRight = sortBoxTriggers[rightIndex].GetComponent <SortBoxTriggerScript>();
                if (triggerLeft && triggerRight)
                {
                    boxLeft  = triggerLeft.GetLastObjectInTrigger().GetComponent <SortBoxScript>();
                    boxRight = triggerRight.GetLastObjectInTrigger().GetComponent <SortBoxScript>();
                }
                else
                {
                    boxLeft  = null;
                    boxRight = null;
                    Debug.LogError("A BOX WAS NOT FOUND!");
                }
                if (boxLeft && boxRight)
                {
                    boxLeft.SetGrabbable(true);
                    boxRight.SetGrabbable(true);
                }
            }
        }
    }
コード例 #2
0
    private void updateTriggers()
    {
        int j = 0;

        foreach (GameObject trigger in sortBoxTriggers)
        {
            // Debug.Log("Update trigger " + j);
            SortBoxTriggerScript sbts = trigger.GetComponent <SortBoxTriggerScript>();
            sbts.correctValue = sortingAlgorithm.ArrayToSort[j];

            if (solved)
            {
                sortBoxTriggers[j].GetComponent <SortBoxTriggerScript>().isFinal = true;
                Debug.Log("Solved " + j);
            }
            else if (j == sortBoxTriggers.Length - sortingAlgorithm.CurrentStep())
            {
                sortBoxTriggers[j].GetComponent <SortBoxTriggerScript>().isFinal = true;
            }
            j++;

            if (sbts.isFinal)
            {
                sbts.DoneInIteration = true;
            }
            else
            {
                sbts.DoneInIteration = false;
            }
        }
        if (!isShowingGeneralInstructions)
        {
            instructionWall.GetComponentInChildren <TextMeshPro>().text = sortingAlgorithm.GetInstruction();
        }
    }
コード例 #3
0
    IEnumerator createTriggersAndBoxes()
    {
        solved = false;
        GetComponent <ResizeTable>().updateTableSize(boxesToSort);
        sortBoxes                    = new GameObject[boxesToSort];
        sortBoxTriggers              = new GameObject[boxesToSort];
        sortingAlgorithm             = new BubbleSort();
        sortingAlgorithm.ArrayToSort = createRandomArray();
        int j = 0;

        foreach (int i in sortingAlgorithm.ArrayToSort)
        {
            GameObject    box           = Instantiate(sortBox, boxSpawnPoint.transform);
            SortBoxScript sortBoxScript = box.GetComponent <SortBoxScript>();
            if (sortBoxScript)
            {
                sortBoxScript.SetNumbersVisible(isNumbersVisible);
                sortBoxScript.SetGrabbable(j == leftIndex || j == rightIndex);
            }

            GameObject boxTrigger = Instantiate(sortBoxTrigger, boxSpawnPoint.transform);
            boxTrigger.transform.localPosition += new Vector3(0, 0, j * boxTrigger.transform.localScale.z * 1.1f);
            //boxTrigger.GetComponent<SortBoxTriggerScript>().correctValue = i;
            //Debug.Log("Does this run on restart?");
            box.transform.localPosition += new Vector3(0, 0, j * boxTrigger.transform.localScale.z * 1.1f);

            boxTrigger.GetComponent <SortBoxHandler>().SetActive(true);

            //TODO: This stuff should probably be placed on SortBoxScript instead.
            box.GetComponent <SortBoxScript>().value = i;
            foreach (TextMeshPro tmPro in box.GetComponentsInChildren <TextMeshPro>())
            {
                tmPro.text = i.ToString();
            }
            sortBoxes[j]       = box;
            sortBoxTriggers[j] = boxTrigger;
            j++;
            yield return(null);
        }

        triggerLeft  = sortBoxTriggers[leftIndex].GetComponent <SortBoxTriggerScript>();
        triggerRight = sortBoxTriggers[rightIndex].GetComponent <SortBoxTriggerScript>();
        if (triggerLeft && triggerRight)
        {
            boxLeft  = triggerLeft.GetObjectInTrigger().GetComponent <SortBoxScript>();
            boxRight = triggerRight.GetObjectInTrigger().GetComponent <SortBoxScript>();
        }

        //sortBoxTriggers[j - sortingAlgorithm.CurrentStep() - 1].GetComponent<SortBoxTriggerScript>().isFinal = true;
        updateTriggers();
        if (!isShowingGeneralInstructions)
        {
            instructionWall.GetComponentInChildren <TextMeshPro>().text = sortingAlgorithm.GetInstruction();
        }
        tableExist = true;
    }