Exemplo n.º 1
0
    public static Tower BuildTower(GameSettings gameSettings, string towerName = "Tower")
    {
        if (!BlockPrefab)
        {
            Debug.LogError("BlockPrefab is missing from Resources folder");
            return(null);
        }
        float angle                 = 360 / gameSettings.NumberOfBlockPerCircle;
        Tower tower                 = new Tower();
        int   blocksInRow           = (int)(360 / angle);
        float distanceBetweenBlocks = 56.7f / angle;

        angle /= 2;
        GameObject towerGO = new GameObject(towerName);

        towerGO.transform.position = gameSettings.LevelGroundCenterPosition;
        for (int i = 0; i < gameSettings.TowerHeight; i++)
        {
            Circle     circle   = new Circle();
            GameObject circleGO = new GameObject("Circle_" + i);
            Vector3    CircleBlockCollisonPosition = gameSettings.LevelGroundCenterPosition + Vector3.up * (i - 1) * gameSettings.BlockHeight;
            Vector3    circleCenterPosition        = gameSettings.LevelGroundCenterPosition + Vector3.up * i * gameSettings.BlockHeight;
            circleGO.transform.position = circleCenterPosition;
            Vector3 blockPosition = circleCenterPosition + Vector3.right * distanceBetweenBlocks / blocksInRow;
            float   rotateDegrees = 0;
            for (int j = 0; j < blocksInRow; j++)
            {
                Quaternion rotation = Quaternion.AngleAxis(rotateDegrees, Vector3.up);
                GameObject newBlock = Instantiate(BlockPrefab, blockPosition, rotation, circleGO.transform);
                Vector3    addedDistanceToDirection = rotation * newBlock.transform.right * distanceBetweenBlocks;
                newBlock.transform.position += addedDistanceToDirection;
                newBlock.GetComponentInChildren <BlockController>().ParentCircle = circle;
                newBlock.name += "_" + i + "_" + j;
                rotateDegrees += angle;
                circle.Blocks.Add(newBlock);
            }
            if (i % 2 != 0)
            {
                Quaternion rotation = Quaternion.AngleAxis(angle / 2, Vector3.up);
                circleGO.transform.rotation = rotation;
            }
            circleGO.transform.parent    = towerGO.transform;
            circle.CircleGO              = circleGO;
            circle.ParentTower           = tower;
            circle.CircleIndex           = i;
            circle.TotalNumberOfBlocks   = gameSettings.NumberOfBlockPerCircle;
            circle.CurrentNumberOfBlocks = gameSettings.NumberOfBlockPerCircle;
            GameObject CBC = Instantiate(CircleBoxCollision, CircleBlockCollisonPosition, Quaternion.identity);
            CBC.GetComponent <BlockCollision>().ParentCircle = circle;
            circle.CircleCollision = CBC.GetComponent <BlockCollision>();
            tower.AddCircle(circle);
        }
        tower.TowerGO     = towerGO;
        tower.TotalBlocks = gameSettings.TowerHeight * gameSettings.NumberOfBlockPerCircle;
        return(tower);
    }