-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris_Block.cs
141 lines (126 loc) · 3.73 KB
/
Tetris_Block.cs
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
140
141
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tetris_Block : MonoBehaviour
{
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 1.0f;
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))
{
transform.RotateAround(transform.TransformPoint(rotationPoint),new Vector3(0,0,1),90);
if(!ValidMove())
{
transform.RotateAround(transform.TransformPoint(rotationPoint),new Vector3(0,0,1),-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<spawnertetris>().NewTetris();
}
previousTime = Time.time;
}
}
void CheckForLines()
{
for(int i = height -1;i >= 0; i++)
{
if(MasLine(i))
{
DeleteLine(i);
rowDown(i);
}
}
}
bool MasLine(int i) //줄이 블록으로 곽 차 있는지 확인하기
{
for(int j =0;j < width;j++)// 줄을 0~9까지 검색한다.
{
if(grid[j, i] == null)//비어 있다면 false
return false;
}
return true;//줄이 꽉 차 있으면 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 roundx = Mathf.RoundToInt(children.transform.position.x);
int roundy = Mathf.RoundToInt(children.transform.position.y);
grid[roundx, roundy] = children;
}
}
bool ValidMove()
{
foreach(Transform children in transform)
{
int roundx = Mathf.RoundToInt(children.transform.position.x);
int roundy = Mathf.RoundToInt(children.transform.position.y);
if(roundx < 0 || roundx >= width || roundy < 0 ||roundy >= height)
{
return false;
}
if(grid[roundx, roundy] != null)
{
return false;
}
}
return true;
}
}