void InitFollowingTetrominos()
        {
            var numFollowingTetrominos = Mathf.Max(1, levelManager.FollowingTetrominoCount);

            if (followingTetrominos.Count > 0)
            {
                for (var i = 0; i < followingTetrominos.Count; i++)
                {
                    var t = followingTetrominos[i];
                    t.RemoveAllBlocks();
                }
                followingTetrominos.Clear();
            }

            for (var i = 0; i < numFollowingTetrominos; i++)
            {
                TetrominoData data = GetNextTetrominoData();
                var           t    = new Tetromino(blockInstanceManager, data);
                t.SetLiarTetrisMode(stateManager != null && stateManager.CurrentState.Value == GameState.LiarTetrisMode);

                followingTetrominos.Add(t);
            }

            UpdateFollowingTetrominosPositions();
        }
        void InitPreviewTetromino(Tetromino current)
        {
            var blocks = new Block[current.BlockCount];

            while (blocks.Length > blocksForPreviewTetromino.Count)
            {
                Block b = Instantiate(previewBlockPrefab);
                b.transform.parent = instanceManager?.BlockParent;
                blocksForPreviewTetromino.Add(b);
            }

            var id = 0;

            for (var i = 0; i < blocksForPreviewTetromino.Count; i++)
            {
                var b = blocksForPreviewTetromino[i];
                if (blocks.Length <= i)
                {
                    b.gameObject.SetActive(false);
                    continue;
                }

                b.gameObject.SetActive(true);
                blocks[i] = b;
            }

            previewTetromino = new Tetromino(blocks, current.Data);
        }
 public void MoveTetromino(Vector3 center, Tetromino tetromino)
 {
     for (var i = 0; i < tetromino.BlockCount; i++)
     {
         var localPos = tetromino.GetLocalBlockPosition(i);
         var pos      = center + new Vector3(localPos.x, localPos.y, 0);
         tetromino.MoveBlock(i, pos);
     }
 }
        public void UpdateTetrominoPositionInGrids(Tetromino tetromino)
        {
            var center = tetromino.Center;

            for (var i = 0; i < tetromino.BlockCount; i++)
            {
                var gridPos = center + tetromino.GetLocalBlockPosition(i);
                var pos     = GetBlockPosition(gridPos);
                tetromino.MoveBlock(i, pos);
            }
        }
        // Start is called before the first frame update
        void Start()
        {
            instanceManager = GetComponent <BlockInstanceManager>();

            if (toGenerate != null)
            {
                var origin = position == null ? Vector3.zero : position.position;
                for (var i = 0; i < toGenerate.Length; i++)
                {
                    var pos       = origin + offset * i;
                    var tetromino = new Tetromino(instanceManager, toGenerate[i]);
                    tetromino.MoveTetromino(pos);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// add the current blocks to grids
        /// </summary>
        /// <returns> min height and max height of the added blocks </returns>
        public Vector2Int AddToGrid(Tetromino tetromino)
        {
            var center    = tetromino.Center;
            var minHeight = grids.GetLength(1) - 1;
            var maxHeight = 0;

            for (var i = 0; i < tetromino.BlockCount; i++)
            {
                var localPos = tetromino.GetLocalBlockPosition(i);
                var blockPos = center + localPos;
                grids[blockPos.x, blockPos.y] = tetromino.GetBlock(i);
                minHeight = Mathf.Min(blockPos.y, minHeight);
                maxHeight = Mathf.Max(blockPos.y, maxHeight);
            }

            return(new Vector2Int(minHeight, maxHeight));
        }
        public Tetromino Spawn()
        {
            var data = GetNextTetrominoData();
            var t    = new Tetromino(blockInstanceManager, data);

            followingTetrominos.Add(t);
            t.SetLiarTetrisMode(stateManager.CurrentState.Value == GameState.LiarTetrisMode);


            var tetromino = followingTetrominos[0];

            followingTetrominos.RemoveAt(0);
            UpdateFollowingTetrominosPositions();


            return(tetromino);
        }
        void Hold()
        {
            if (waitingForCompeleteMove || clearingLines)
            {
                waitingForCompeleteMove = false;
            }

            if (!holdable)
            {
                return;
            }

            audioManager.PlayHoldSE();

            holdable = false;
            var temp = heldTetromino;

            heldTetromino    = currentTetromino;
            currentTetromino = temp;

            // initialize the pose
            if (heldTetromino != null)
            {
                heldTetromino.InitPose();

                // add the held tetromino data to the candidates to spawn
                //spawner.AddCandidate(heldTetromino.Data);
            }

            if (currentTetromino == null)
            {
                currentTetromino = spawner.Spawn();
            }

            currentTetromino.UpdateCenterPosition(spawnPoint);

            UpdateTetrominoPositionInGrids(currentTetromino);

            heldTetromino.MoveTetromino(heldTetrominoPositionTransform.position);

            InitPreviewTetromino(currentTetromino);
            MovePreviewTetromino();
        }
        void SpawnTetromino(bool holdable = true)
        {
            this.holdable    = holdable;
            currentTetromino = spawner.Spawn();

            if (previewTetromino == null || previewTetromino.Data != currentTetromino.Data)
            {
                InitPreviewTetromino(currentTetromino);
            }

            currentTetromino.UpdateCenterPosition(spawnPoint);
            UpdateTetrominoPositionInGrids(currentTetromino);

            MovePreviewTetromino();

            if (gridManager.InvalidMove(currentTetromino))
            {
                stateManager.GameOver();
            }
        }
Exemple #10
0
        public bool InvalidMove(Tetromino tetromino)
        {
            var center = tetromino.Center;

            for (var i = 0; i < tetromino.BlockCount; i++)
            {
                var localPos = tetromino.GetLocalBlockPosition(i);
                var gridPos  = center + localPos;

                if (gridPos.x < 0 || grids.GetLength(0) <= gridPos.x || gridPos.y < 0 || grids.GetLength(1) <= gridPos.y)
                {
                    return(true);
                }

                if (grids[gridPos.x, gridPos.y] != null)
                {
                    return(true);
                }
            }

            return(false);
        }