-6

I am developing a game with a concept of Tetris, But in my game what I want to clear is rectangle formed by the player instead of row or line. Like, for example, I instantiate that the blocks will be cleared if there are 4 rows and 5 columns filled with Tetris blocks on the grid.

So far this is my code that deletes a row:

using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {

    // The Grid itself
    public static int w = 10;
    public static int h = 20;
    public static Transform[,] grid = new Transform[w, h];



    void Start () {

    }


     public static Vector2 roundVec2(Vector2 v) {
        return new Vector2(Mathf.Round(v.x),
                           Mathf.Round(v.y));
    }



    // helps to find out if a certain coordinate is in between the borders or if it's outside of the borders:

    public static bool insideBorder(Vector2 pos) {
        return ((int)pos.x >= 0 &&
                (int)pos.x < w &&
                (int)pos.y >= 0);
    }



    //The next function deletes all Blocks in a certain row. 

    public static void deleteRow(int y) {
        for (int x = 0; x < w; ++x)
        {
            Destroy(grid[x, y].gameObject);
            grid[x, y] = null;

        }
        Score.currentScore += 1;

    }




    // Whenever a row was deleted, the above rows should fall towards the bottom by one unit

    public static void decreaseRow(int y) {
        for (int x = 0; x < w; ++x)
        {
            if (grid[x, y] != null)
            {
                // Move one towards bottom
                grid[x, y - 1] = grid[x, y];
                grid[x, y] = null;

                // Update Block position
                grid[x, y - 1].position += new Vector3(0, -1, 0);
            }
        }
    }


    public static void decreaseRowsAbove(int y)
    {
        for (int i = y; i < h; ++i)
            decreaseRow(i);
    }



    // function that finds out if a row is full of blocks

    public static bool isRowFull(int y)
    {
        for (int x = 0; x < w; ++x)
            if (grid[x, y] == null)
                return false;
        return true;
    }


    //a function that deletes all full rows and then always decreases the above row's y coordinate by one.

    public static void deleteFullRows()
    {
        for (int y = 0; y < h; ++y)
        {
            if (isRowFull(y))
            {
                deleteRow(y);
                decreaseRowsAbove(y + 1);
                --y;

            }
        }
    }       
}

Like this. Instead of line, a rectangle formed will be the one to be clear

So, how do I clear the rectangle formed in my Tetris?

The usual Tetris deletes row or line. But what I want is to clear a rectangular prism.

Thank you for your response

Umair M
  • 10,298
  • 6
  • 42
  • 74
JaneTho
  • 321
  • 1
  • 2
  • 13

1 Answers1

0

Something like this:

public static void deleteGrid(int startX, int startY, int width = 5, int height = 4) 
{

    // to avoid Errors, Check if indexes are in bound for grid.

    // if condition code goes here

    // then...

    for (int i = startX; i < startX + width; i++)
    {
        for (int j = startY; j < startY + height; j++) 
        {
            Destroy(grid[i, j].gameObject);
            grid[i, j] = null;
        }
    }
    Score.currentScore += 1;
}

Call it like this:

Grid.deleteGrid(2,3); // default width = 5, height = 4 will be used here.

Or

Grid.deleteGrid(2,3,3,3); // this will delete 3X3 grid at index (2,3)
Umair M
  • 10,298
  • 6
  • 42
  • 74
  • Thank you for your reply and for editing my question. it has an error that says "no overload for method 'deleteRow' takes '1' argument. Sorry but I am new in game development. I will edit my question and put my whole script. thankyou – JaneTho Sep 05 '16 at 10:46
  • I have updated my answer. Where ever you were calling your `deleteRow()` method. change it to `deleteGrid` – Umair M Sep 05 '16 at 10:52
  • still doesnt work for me. I think theres no problem with your code. I just dont really know how to apply it, really. could you consider my full source code above? Thanks for doing me a favor. – JaneTho Sep 05 '16 at 11:07
  • Well its your part to figure out where to use this code. please mark the answer correct – Umair M Sep 05 '16 at 13:08
  • That's all connected to deleting blocks. When blocks are cleared the above tetris piece will fall down. What I did is replaced the deleteRow() to your suggested one. But it shows an overload error. – JaneTho Sep 05 '16 at 13:11
  • I'll explore it. Thankyou for your time. :) – JaneTho Sep 05 '16 at 13:12