-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisBlock Step 8 FINAL (Line Deletion).cs
More file actions
139 lines (119 loc) · 3.8 KB
/
TetrisBlock Step 8 FINAL (Line Deletion).cs
File metadata and controls
139 lines (119 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TetrisBlock : MonoBehaviour //"TetrisBlock" MUST be renamed to whatever your script is called for the code to work!
{
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 10;
private static Transform[,] grid = new Transform[width, height];
//Start is called before the first frame update
void start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1,0,0);
if(!ValidMove())
transform.position -= new Vector3(-1,0,0);
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1,0,0);
if (!ValidMove())
transform.position -= new Vector3(1,0,0);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
//Rotation
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), -90); //You may need to make this -90
if (!ValidMove())
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); //And this (+)90
}
if(Time.time - previousTime > (Input.GetKey(KeyCode.DownArrow) ? fallTime / 10 : fallTime))
{
transform.position += new Vector3(0, -1, 0);
if (!ValidMove())
{
transform.position -= new Vector3(0, -1, 0);
AddToGrid();
CheckForLines();
this.enabled = false;
FindObjectOfType<BlockSpawner>().NewTetromino();
}
previousTime = Time.time;
}
}
void CheckForLines()
{
for (int i = height-1; i >= 0; i--)
{
if(HasLine(i))
{
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i)
{
for(int j=0; j< width; j++)
{
if (grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i)
{
for(int j=0; j<width; j++)
{
Destroy(grid[j,i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i)
{
for(int y=i; y<height; y++)
{
for(int j=0; j<width; j++)
{
if(grid[j,y] != null)
{
grid[j, y-1] = grid[j,y];
grid[j, y] = null;
grid[j, y-1].transform.position -= new Vector3(0,1,0);
}
}
}
}
void AddToGrid()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
grid[roundedX, roundedY] = children;
}
}
bool ValidMove()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
if(roundedX < 0 || roundedX >= width || roundedY < 0 ||roundedY >= height)
{
return false;
}
if (grid[roundedX,roundedY] != null)
return false;
}
return true;
}
}