Exemplo n.º 1
0
 public void DisplayNextShape(TetrisShape shape)
 {
     UnityEngine.Object.Destroy(_nextShape);
     _nextShape = ShapeFactory.CreateShape(shape);
     _nextShape.transform.position = NextShapePosition;
     _nextShape.transform.SetParent(_playersBoard.transform, false);
 }
Exemplo n.º 2
0
    public void UpdateGrid(TetrisShape tetris)
    {
        for (int y = 0; y < gridHeight; ++y)
        {
            for (int x = 0; x < gridWidth; ++x)
            {
                if (grid[x, y] != null)
                {
                    if (grid[x, y].parent == tetris.transform)
                    {
                        grid[x, y] = null;
                    }
                }
            }
        }

        foreach (Transform mino in tetris.transform)
        {
            Vector2 pos = Round(mino.position);

            if (pos.y < gridHeight)
            {
                grid[(int)pos.x, (int)pos.y] = mino;
            }
        }
    }
Exemplo n.º 3
0
 public void GameOver()
 {
     currentShape.isDropping = false;
     currentShape            = null;
     gameActive = false;
     Debug.Log("GAME OVER!");
     Destroy(this.gameObject);
 }
Exemplo n.º 4
0
 public void RotateShape(TetrisShape shape)
 {
     var i = 0;
     foreach (Transform block in _currentShape.transform)
     {
         block.localPosition = new Vector3(
             shape.Blocks[i].X,
             shape.Blocks[i++].Y)
             * ShapeFactory.BLOCK_SIZE;
     }
 }
Exemplo n.º 5
0
        public TetrisModel(int width, int height)
        {
            Score = 0;
            CollectedLinesCount = 0;
            Level = 1;

            _board = new Board(width, height);

            JsonReader.LoadResources();
            _nextShape = GenerateNextShape();
            TryToAddShape();
        }
Exemplo n.º 6
0
    public void SpawnShape()
    {
        if (!gameActive)
        {
            return;
        }
        // Span a random shape.
        //Debug.Log ("Spawning new shape...");
        int        rnd   = Random.Range(0, shapeTypes.Length);
        GameObject shape = Instantiate(shapeTypes [rnd]);

        shape.transform.position = transform.position;
        currentShape             = shape.GetComponent <TetrisShape> ();
        shape.transform.parent   = blockHolder;
    }
Exemplo n.º 7
0
    private void SpawnShape()
    {
        if (IsGameOver())
        {
            return;
        }

        System.Random rand = new System.Random();
        shape             = ScriptableObject.CreateInstance <TetrisShape>();
        shape.TopLeft     = GetSpawnPosition();
        shape.TileTexture = tileTextures[rand.Next(0, tileTextures.Count)];

        if (parentWindow != null)
        {
            parentWindow.Repaint();
        }
    }
Exemplo n.º 8
0
        public List<int> AttachShape(TetrisShape shape)
        {
            var collectedLine = shape.Blocks
                .Select(block => _currentShapeCoord + block)
                .Where(coord => coord.Y < Height)
                .Where(coord =>
                {
                    this[coord] = 1;
                    if (BlockIsAttached != null) BlockIsAttached(this, new CoordEventArgs(coord.X, coord.Y));
                    return (++_linesCount[coord.Y] == Width);
                })
                .Select(coord => coord.Y)
                .ToList();

            collectedLine.Sort();

            if (ShapeIsAttached != null) ShapeIsAttached(this, EventArgs.Empty);
            return collectedLine;
        }
Exemplo n.º 9
0
        public static GameObject CreateShape(TetrisShape modelShape)
        {
            var shape = new GameObject("TetrisShape");

            var hexCode = modelShape.HexColor;
            var color = hexCode.ToColor();

            foreach (var block in modelShape.Blocks)
            {
                float x = block.X;
                float y = block.Y;

                var localVar = Object.Instantiate(Block);
                localVar.transform.parent = shape.transform;
                localVar.transform.position = new Vector2(x * BLOCK_SIZE, y * BLOCK_SIZE);
                localVar.GetComponent<SpriteRenderer>().color = color;
            }
            return shape;
        }
Exemplo n.º 10
0
        public void RandomNextShape()
        {
            // Spawn a random block
            Array values = Enum.GetValues(typeof(TetrisShape));
            _NextShape = (TetrisShape)values.GetValue(Game.Random.Next(values.Length));

            TetrisEntity tetris = Parent as TetrisEntity;
            tetris.InfoPanel.NextShape = GetNextShape();
            tetris.InfoPanel.NextShape.Velocity = Vector2.Zero;
        }
Exemplo n.º 11
0
        private bool TryToAddShape()
        {
            _currentShape = _nextShape;
            _nextShape = GenerateNextShape();

            _board.CurrentShapeCoord = new Point((
                _board.Width - _currentShape.Width) / 2,
                _board.Height - _currentShape.Height - 1);

            return _board.CheckShapeOffset(_currentShape, new Point(0, 0));
        }
Exemplo n.º 12
0
        public void RotateShape(RotateDirection rotateDirection)
        {
            var shape = _currentShape.Rotate(rotateDirection);

            if (_board.CheckShapeOffset(shape, new Point(0, 0)))
            {
                _currentShape = shape;
                if (RotateDone != null) RotateDone(this, EventArgs.Empty);
            }
        }
Exemplo n.º 13
0
 public bool CheckShapeOffset(TetrisShape shape, Point offset)
 {
     return !shape.Blocks
         .Select(block => _currentShapeCoord + block + offset)
         .Any(coord => coord.X < 0 || coord.X > Width - 1 ||
                       coord.Y < 0 || coord.Y < Height &&
                       this[coord] != 0);
 }
Exemplo n.º 14
0
        public void SpawnShape(TetrisShape shape, Point spawnCoord)
        {
            _currentShape = ShapeFactory.CreateShape(shape);

            _currentShape.transform.position = new Vector2(
            (spawnCoord.X - (float)(_model.BoardWidth - 1) / 2),
            spawnCoord.Y - (float)_model.BoardHeight/2 + 1)
            *ShapeFactory.BLOCK_SIZE;

            _currentShape.transform.SetParent(_playersBoard.transform, false);
        }