Added Tetris functionality

This commit is contained in:
Furentes
2019-04-05 11:21:16 +02:00
parent 2e74c3e35e
commit 41d16a7947
30 changed files with 3815 additions and 207 deletions

87
Assets/Scripts/Grid.cs Normal file
View File

@ -0,0 +1,87 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Grid : MonoBehaviour
{
// The grid
public static int w = 10;
public static int h = 20;
public static Transform[,] grid = new Transform[w, h];
private static int count = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// Round Vector to full numbers
public static Vector2 roundVec2(Vector2 v)
{
return new Vector2(Mathf.Round(v.x), Mathf.Round(v.y));
}
public static bool insideBorder(Vector2 pos)
{
return ((int)pos.x >= 0 && (int)pos.x < w && (int)pos.y >= 0);
}
public static void deleteRow(int y)
{
count++;
for (int x = 0; x < w; ++x)
{
Destroy(grid[x, y].gameObject);
grid[x, y] = null;
}
}
public static void decreaseRowsAbove(int y)
{
for (int i = y; i < h; ++i)
decreaseRow(i);
}
public static void decreaseRow(int y)
{
for(int x = 0; x < w; ++x)
{
if(grid[x,y] != null)
{
grid[x, y - 1] = grid[x, y];
grid[x, y] = null;
grid[x, y - 1].position += new Vector3(0, -1, 0);
}
}
}
public static bool isRowFull(int y)
{
for (int x = 0; x < w; ++x)
if (grid[x, y] == null)
return false;
return true;
}
public static void deleteFullRows()
{
for (int y = 0; y < h; ++y)
{
if(isRowFull(y))
{
deleteRow(y);
decreaseRowsAbove(y + 1);
Group.fallSpeed -= (float)0.025;
--y;
}
}
}
}