Esempio n. 1
0
    public int CheckNumbOfChilds()
    {
        //Variable that count childs
        int ChildCounter = 0;

        //For each child in the container that stores buildings block...
        foreach (Transform child in buildingsContainer)
        {
            //...Add one to the counter
            ChildCounter += 1;
            PredioScore predioScore = child.GetComponent <PredioScore>();
            //If the number of the building block is higher than the score showed...
            if (predioScore.buildingNumber > scoreShower)
            {
                //...Show in score the number of the building block
                scoreShower = predioScore.buildingNumber;
            }
        }

        foreach (Transform child in buildingsContainer)
        {
            PredioScore predioScore = child.GetComponent <PredioScore>();
            //If the building block has the number equals to the score...
            if (child.name == "Building Nº" + scoreShower)
            {
                //...Show the score on top of building
                predioScore.canvas.gameObject.SetActive(true);
            }
            //If not...
            else
            {
                //...Hide the score
                predioScore.canvas.gameObject.SetActive(false);
            }
        }
        //reset scoreShower to not stack in the next call of the funcition
        scoreShower = 0;
        //Return the numbers of building blocks that are togheter
        return(ChildCounter);
    }
Esempio n. 2
0
    //If the building block collides...
    void OnCollisionEnter2D(Collision2D other)
    {
        //...Get the script from the other building;
        PredioScore otherPredioScore = other.transform.GetComponent <PredioScore>();

        //If the script exists... (If collided with another building).
        if (otherPredioScore != null)
        {
            //...If not already collided...
            if (!collidedWithOtherBuilding && !collidedWithGround)
            {
                //...If the other building has the same color...
                if (otherPredioScore._spriteRenderer.color == _spriteRenderer.color)
                {
                    //Set this to has the same parent from other building;
                    transform.parent = otherPredioScore.transform.parent;
                    //Fix the scale to not distorce with base in the parent;
                    transform.localScale = new Vector3(1, 1, 1);
                    //Stores the colision position;
                    fixedPos = transform.position;
                    //Stores the number of buildings with the same color that are togheter;
                    int score = transform.parent.parent.GetComponent <FloorScript>().CheckNumbOfChilds();
                    //Set the score canvas text to the score variable number;
                    _scoreText.text = score.ToString();
                    //Count the number of childs of each color in the rope script;
                    if (_goToThisFloor == "B")
                    {
                        _ropeMovement.numbOfChildsBlue += 1;
                    }
                    else if (_goToThisFloor == "R")
                    {
                        _ropeMovement.numbOfChildsRed += 1;
                    }
                    else if (_goToThisFloor == "G")
                    {
                        _ropeMovement.numbOfChildsGreen += 1;
                    }
                }
                //If the color is different...
                else
                {
                    //Destroy the building block in question
                    Destroy(gameObject);
                }
                //Set that collided with other building
                collidedWithOtherBuilding = true;
            }
        }

        //If collided with a floor...
        FloorScript otherFloor = other.transform.GetComponent <FloorScript>();

        if (otherFloor != null)
        {
            //...If wasn't collided before...
            if (!collidedWithGround && !collidedWithOtherBuilding)
            {
                //...If number of blue buildings is more than 0 and this build has to go to the blue floor...
                if (_ropeMovement.numbOfChildsBlue > 0 && _goToThisFloor == "B")
                {
                    //...Destroy the gameobject;
                    Destroy(gameObject);
                    //Cancel the realocation of score panel;
                    otherFloor.CheckNumbOfChilds();
                }
                //Same for red
                if (_ropeMovement.numbOfChildsRed > 0 && _goToThisFloor == "R")
                {
                    Destroy(gameObject);
                    otherFloor.CheckNumbOfChilds();
                }
                //Same for green
                if (_ropeMovement.numbOfChildsGreen > 0 && _goToThisFloor == "G")
                {
                    Destroy(gameObject);
                    otherFloor.CheckNumbOfChilds();
                }
                //If the floor that the build most go is the floor that he collided...
                if (_goToThisFloor == otherFloor.name)
                {
                    //...Check if the floor have any buildings already
                    if (_goToThisFloor == "B" && _ropeMovement.numbOfChildsBlue <= 0)
                    {
                        //...If this is the first building, add to the count of respective color.
                        _ropeMovement.numbOfChildsBlue += 1;
                    }
                    else if (_goToThisFloor == "R" && _ropeMovement.numbOfChildsRed <= 0)
                    {
                        //...If this is the first building, add to the count of respective color.
                        _ropeMovement.numbOfChildsRed += 1;
                    }
                    else if (_goToThisFloor == "G" && _ropeMovement.numbOfChildsGreen <= 0)
                    {
                        //...If this is the first building, add to the count of respective color.
                        _ropeMovement.numbOfChildsGreen += 1;
                    }
                    //Set the parent to the buildings container;
                    transform.parent = otherFloor.buildingsContainer;
                    //Fix the scale of the object;
                    transform.localScale = new Vector3(1, 1, 1);
                    //Set the score number;
                    int score = other.transform.GetComponent <FloorScript>().CheckNumbOfChilds();
                    //Set the score text;
                    _scoreText.text = score.ToString();
                    //Set the scale object to the scale of original object;
                    transform.localScale = buildGO.transform.localScale;
                    //Stores the position of the collision;
                    fixedPos = transform.position;
                }
                //If collided in a floor with the wrong collor...
                else
                {
                    //...Destroy object and recount the childs;
                    Destroy(gameObject);
                    otherFloor.CheckNumbOfChilds();
                }

                //Set the collision bool to true;
                collidedWithGround = true;
            }
        }
    }