Example #1
0
        private ControllingTetrisPiece CreateControllingPiece(int pieceIndex)
        {
            GameObject             pieceObj = new GameObject();
            ControllingTetrisPiece piece    = pieceObj.AddComponent <ControllingTetrisPiece>();

            piece.grid       = this;
            piece.pieceIndex = pieceIndex;
            piece.piece      = tetrisPieces[pieceIndex].GetComponent <TetrisPiece>();

            return(piece);
        }
Example #2
0
        private void Start()
        {
            //Build color array
            materialPieces = new List <Material>();

            foreach (GameObject go in tetrisPieces)
            {
                materialPieces.Add(go.GetComponent <TetrisPiece>().ColoredMaterial);
            }

            gridBlocks = new List <GameObject>();

            BuildEmptyGrid();

            //Test: fill bottom and then remove a piece
            //FillLevel(0, 1);
            //SetGridPoint(new Vector3Int(0, 0, 0), 0);

            currentPiece = CreateRandomControllingPiece(nextPieceIndex);
            RenderGrid();

            nextPieceIndex = UnityEngine.Random.Range(0, tetrisPieces.Count);

            IEnumerator DropLoop()
            {
                while (true)
                {
                    yield return(new WaitForSeconds(1));

                    Drop();
                    UpdateGrid();
                    RenderGrid();
                }
            }

            StartCoroutine(DropLoop());

            /*
             * IEnumerator RandomLoop()
             * {
             *  while (true)
             *  {
             *      yield return new WaitForSeconds(.1f);
             *
             *      Random(10);
             *      UpdateGrid();
             *      RenderGrid();
             *  }
             * }
             *
             * StartCoroutine(RandomLoop());*/
        }
Example #3
0
        private ControllingTetrisPiece CreateRandomControllingPiece(int pieceIndex)
        {
            TetrisPiece tp = tetrisPieces[pieceIndex].GetComponent <TetrisPiece>();

            int x = UnityEngine.Random.Range(tp.dimensions.x, dimensions.x - tp.dimensions.x);
            int y = dimensions.y - 1;
            int z = UnityEngine.Random.Range(tp.dimensions.z, dimensions.z - tp.dimensions.z);

            ControllingTetrisPiece piece = CreateControllingPiece(pieceIndex);

            piece.transform.position = transform.position + new Vector3(x, y, z) * blockSize;

            piece.Init();

            return(piece);
        }
Example #4
0
        public void Drop()
        {
            //Detect we're going to overlap / hit the floor in this step, if so disable this piece!
            if (currentPiece.Overlaps(currentPiece.GetGridPosition() + new Vector3Int(0, -1, 0), currentPiece.rotation, true, true))
            {
                if (CheckForCeiling())
                {
                    //GAME OVER
                }
                else
                {
                    Destroy(currentPiece);
                    currentPiece = CreateRandomControllingPiece(nextPieceIndex);

                    nextPieceIndex = UnityEngine.Random.Range(0, tetrisPieces.Count);
                }
            }
            else
            {
                currentPiece.Move(new Vector3Int(0, -1, 0));
            }
        }