Example #1
0
 public void addBranchesToTree()
 {
     for (int i = tree.Count - 1; i >= 0; i--)
     {
         GameObject oldBranch = (GameObject)tree[i];
         branchScript = oldBranch.GetComponent <BranchScript>();
         if (!branchScript.hasBeenBranched)
         {
             for (int j = 0; j < Random.Range(3, 6); j++)
             {
                 GameObject newBranch = branchScript.createNewBranch(levelCounter, branchCounter);
                 newBranch.transform.SetParent(t);
                 tree.Add(newBranch);
                 branchCounter++;
             }
             branchScript.setHasBeenBranchedToTrue();
         }
         else
         {
             break;
         }
     }
     levelCounter++;
     branchCounter = 0;
 }
    public static void ScaleBug(BranchScript branch, GameObject bugObject)
    {
        Bug bug = bugObject.GetComponent <Bug>();

        bugObject.transform.localScale = new Vector3(1.15f, 1.15f, 1.15f);
        RectTransform rectT = bugObject.AddComponent(typeof(RectTransform)) as RectTransform;
        // = bugObject.GetComponent<RectTransform>();

        GameObject branchObject = GameObject.Find("Branch");

        //branchObject.transform.localScale = new Vector3(1, 1, 1);

        //RectTransform branchRect = branchObject.GetComponent<RectTransform>();
        rectT.sizeDelta = new Vector2(12, 9);


        bug.lengthInMM = RandomBugLength(bug);
        float bugLength = bug.lengthInMM;

        float scaleRatio = (bugLength / branch.branchWidthInMM) * 4;



        Vector3 scaleFactor = new Vector3(scaleRatio / bugObject.transform.localScale.x, scaleRatio / bugObject.transform.localScale.y, bugObject.transform.localScale.z);

        Debug.Log(scaleRatio);

        Transform branchTransform = branch.gameObject.transform;

        bugObject.transform.localScale = scaleFactor;//branchTransform.localScale.x * scaleRatio;
    }
    public static void ScaleRuler(BranchScript branch, Bug bug)
    {
        RectTransform bugRect = bug.GetComponent <RectTransform>();

        //GameObject rulerPrefab = GameManager.instance.rulerPrefab;
        //rulerPrefab.SetActive(true);
        Instantiate(utilInstance.ruler, new Vector3(bugRect.position.x, bugRect.transform.position.y + .6f, bugRect.transform.position.z), Quaternion.identity);
        RectTransform rulerRect = utilInstance.ruler.GetComponent <RectTransform>();
        //utilInstance.ruler.transform.position = new Vector3(bugRect.transform.position.x, bugRect.transform.position.y - 200, bugRect.transform.position.z);
        //rulerPrefab, new Vector3(bugRect.transform.position.x, bugRect.transform.position.y - 200, bugRect.transform.position.z), Quaternion.identity

        float rulerLengthInMM = 56f;
        float bugLengthIG     = (bugRect.rect.width) * bug.lengthAsProportionOfImageWidth;
        float rulerLengthIG   = bugLengthIG * (rulerLengthInMM / bug.lengthInMM);

        rulerRect.localScale = new Vector3(bugRect.localScale.x * rulerLengthIG / bugLengthIG, bugRect.localScale.x * rulerLengthIG / bugLengthIG, 1);
    }
Example #4
0
    void HandleLengthUI()
    {
        bugButtons.SetActive(false);
        bugSelectionText.SetActive(false);

        BranchScript branch = GameObject.Find("Branch").GetComponent <BranchScript>();

        lengthUI.SetActive(true);
        GameObject ruler = GameObject.Find("Ruler");

        Utilities.ScaleRuler(branch, currentBugScript);

        measurementInput = lengthUI.GetComponentInChildren <InputField>();
        measurementInput.onEndEdit.AddListener(delegate { EvaluateMeasurement(measurementInput); });
        if (lengthSubmit != null)
        {
            lengthSubmit.GetComponent <Button>().onClick.AddListener(BugUISubmit);
        }
    }
Example #5
0
    private void createNewBush(GameObject branch, BranchScript givenBranchScript, int randomBushColor)
    {
        Vector3    spawnPos = branchScript.endPoint.transform.position;
        GameObject newBush  = Instantiate(bushPrefab, spawnPos, Quaternion.identity);

        newBush.transform.localScale = newBush.transform.localScale * Random.Range(0.1f, 1f);
        newBush.transform.parent     = branch.transform;
        if (useOnlyTheColorBelow)         // if useOnlyTheColorBelow == true
        {
            randomBushColor = colorIndexInArray;
        }
        else if (!useSingleRandomColor)         // if useSingleRandomColor == false
        {
            /*
             * Here bushColors.Count gives a number greater than the size of the array
             * but since Random.Range never reaches bushColors.Count (but it does get close in terms of decimals)
             * and since type casting into int truncates the decimals, it looks at all elements in the array.
             */
            randomBushColor = (int)Random.Range(0, bushColors.Count);
        }
        newBush.GetComponent <Renderer>().material = bushColors[randomBushColor];
        branchScript.setHasBeenBranchedToTrue();
    }
Example #6
0
    private void addBushesAndFinishTree()
    {
        int randomBushColor = (int)Random.Range(0, bushColors.Count);

        for (int i = tree.Count - 1; i >= 0; i--)
        {
            GameObject endBranch = (GameObject)tree[i];
            branchScript = endBranch.GetComponent <BranchScript>();
            if (!branchScript.hasBeenBranched)
            {
                createNewBush(endBranch, branchScript, randomBushColor);
            }
            else
            {
                break;
            }
        }
        if (spawnBushOnFirstBranch)
        {
            GameObject firstBranch = (GameObject)tree[0];
            branchScript = firstBranch.GetComponent <BranchScript>();
            createNewBush(firstBranch, branchScript, randomBushColor);
        }
    }
    public void SenseSticks()
    {
        GameObject[]        items    = TimeManagerScript.instance.allItemsOnGrid;
        List <ItemScript>   iScripts = new List <ItemScript>();
        List <BranchScript> bScripts = new List <BranchScript>();

        for (int i = 0; i < items.Length; i++)
        {
            if (items[i].gameObject.GetComponent <BranchScript>())
            {
                bScripts.Add(items[i].GetComponent <BranchScript>());
            }
            else
            {
                iScripts.Add(items[i].GetComponent <ItemScript>());
            }
        }

        int          count = 1;
        bool         uBlocked = false, dBlocked = false, lBlocked = false, rBlocked = false;
        BranchScript br = null;

        while (count <= sensingRange)
        {
            //up
            if (!uBlocked && SenseSticksHelper(0, -count, gridSize, gridSize, iScripts, bScripts, ref uBlocked, ref br))
            {
                if (br != null)
                {
                    br.EatBranch();                             //eat branch if on top
                }
                anim.SetTrigger("MoveBack");
                gItem.MoveOneUnitBySeconds(Direction.up, moveAnimSpeed);
                break;
            }
            //right
            if (!rBlocked && SenseSticksHelper(count, 0, gridSize, gridSize, iScripts, bScripts, ref rBlocked, ref br))
            {
                if (br != null)
                {
                    br.EatBranch();                             //eat branch if on top
                }
                anim.SetTrigger("MoveRight");
                gItem.MoveOneUnitBySeconds(Direction.right, moveAnimSpeed);
                break;
            }
            //down
            if (!dBlocked && SenseSticksHelper(0, count, gridSize, gridSize, iScripts, bScripts, ref dBlocked, ref br))
            {
                if (br != null)
                {
                    br.EatBranch();                             //eat branch if on top
                }
                anim.SetTrigger("MoveFront");
                gItem.MoveOneUnitBySeconds(Direction.down, moveAnimSpeed);
                break;
            }
            //left
            if (!lBlocked && SenseSticksHelper(-count, 0, gridSize, gridSize, iScripts, bScripts, ref lBlocked, ref br))
            {
                if (br != null)
                {
                    br.EatBranch();                             //eat branch if on top
                }
                anim.SetTrigger("MoveLeft");
                gItem.MoveOneUnitBySeconds(Direction.left, moveAnimSpeed);
                break;
            }
            count++;
        }

        if (coffee != null && coffee.itemPos.x >= 0 && coffee.itemPos.y >= 0 && coffee.CompareToItemPosition(itemPos))
        {
            GoCoffeeMode();
        }
    }
    private bool SenseSticksHelper(int xDisp, int yDisp, int xMax, int yMax, List <ItemScript> allItems, List <BranchScript> bScripts, ref bool blocked, ref BranchScript eatIt)
    {
        Vector2Int posCheck = new Vector2Int(itemPos.x + xDisp, itemPos.y + yDisp);

        if (posCheck.x > xMax || posCheck.x < 0 ||
            posCheck.y > yMax || posCheck.y < 0)
        {
            blocked = true;
            return(false);            //break if beyond the grid
        }
        for (int i = 0; i < allItems.Count; i++)
        {
            if (allItems[i].CompareToItemPosition(posCheck) &&
                !allItems[i].gameObject.GetComponent <BranchScript>() && !allItems[i].gameObject.GetComponent <CoffeeScript>())
            {
                blocked = true;
                return(false);                //break if blocked off from a branch
            }
        }
        for (int i = 0; i < bScripts.Count; i++)
        {
            if (bScripts[i].CompareToItemPosition(posCheck) && !bScripts[i].isOnFire)
            {
                if (Mathf.Abs(xDisp) == 1 || Mathf.Abs(yDisp) == 1)
                {
                    eatIt = bScripts[i];
                }
                return(true);                //finds if it contains branch script and not on fire
            }
            else if (bScripts[i].CompareToItemPosition(posCheck) && bScripts[i].isOnFire)
            {
                blocked = true;
                return(false);                //break if blocked off with burning branch
            }
        }

        return(false);
    }
    void Start()
    {
        //GameObject holding spawned bugs
        Transform spawnedBugs = GameObject.Find("SpawnedBugs").transform;

        branch = GameObject.Find("Branch").GetComponent <BranchScript>();

        //Sets number of bugs based on difficulty
        string sceneName        = SceneManager.GetActiveScene().name;
        string branchDifficulty = sceneName.Substring(0, 4);

        setNumWithDifficulty(branchDifficulty);

        //catches exceptions with the number of bugs
        if (numOfBugs > transform.childCount)
        {
            numOfBugs = transform.childCount;
        }
        else if (numOfBugs < 0)
        {
            numOfBugs = 0;
        }


        //keeps track of which bugs are already added to scene
        HashSet <Transform> alreadyAdded = new HashSet <Transform>();


        //Loop to add bugs
        for (int i = 0; i < numOfBugs; i++)
        {
            //randomly picks a spawnPoint
            int       point      = Random.Range(0, transform.childCount);
            Transform spawnPoint = transform.GetChild(point);

            //while loop checks if spawn point has been used
            //IF has been used, finds one that hasnt
            while (alreadyAdded.Contains(spawnPoint))
            {
                point      = Random.Range(0, transform.childCount);
                spawnPoint = transform.GetChild(point);
            }

            //adds spawnpoint to hashset
            alreadyAdded.Add(spawnPoint);

            //randomly picks bug that can spawn from that spawn point
            int       bugIndex = Random.Range(0, spawnPoint.childCount);
            Transform bug      = spawnPoint.GetChild(bugIndex);

            //duplicates it and adds it to the spawnBugs
            Transform newBug = Instantiate(bug);

            //sets the bugs position
            Vector3 bugPosition = spawnPoint.position + newBug.position;
            newBug.transform.position = bugPosition;
            newBug.gameObject.SetActive(true);

            //adds the bug to scene and makes it visible
            newBug.parent = spawnedBugs;
            newBug.gameObject.SetActive(true);
            Utilities.ScaleBug(branch, newBug.gameObject);
        }
    }