-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.cs
115 lines (97 loc) · 3.32 KB
/
InputManager.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager : MonoBehaviour
{
public Block block;
float fall = 0;
float accel = 1;
TetManager tetManager;
void Start()
{
block = FindObjectOfType<Block>();
tetManager = FindObjectOfType<TetManager>();
}
void Update()
{
if(!tetManager.gameOver && tetManager.gameStart)
UserInput();
}
void UserInput()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
block.transform.position += Vector3.right;
if (CheckIsValidPosition()) { tetManager.UpdateMap(block); }
else { block.transform.position += Vector3.left; }
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
block.transform.position += Vector3.left;
if (CheckIsValidPosition()) { tetManager.UpdateMap(block); }
else { block.transform.position += Vector3.right; }
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (block.allowRotation)
{
if (block.limitRotation)
{
if (block.transform.eulerAngles.z >= 90)
block.transform.Rotate(0, 0, -90);
else
block.transform.Rotate(0, 0, 90);
}
else
{
block.transform.Rotate(0, 0, 90);
}
if (CheckIsValidPosition()) { tetManager.UpdateMap(block); }
else
{
if (block.limitRotation)
{
if (block.transform.eulerAngles.z >= 90)
block.transform.Rotate(0, 0, -90);
else
block.transform.Rotate(0, 0, 90);
}
else
block.transform.Rotate(0, 0, -90);
}
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= accel)
{
block.transform.position += Vector3.down;
if (CheckIsValidPosition()) { tetManager.UpdateMap(block); }
else
{
block.transform.position += Vector3.up;
if (tetManager.GameOver(block.transform.position.y))
{
tetManager.gameOver = true;
UIManager.Instance.DisplayGameoverText("Lose", new Color(1, 0, 0, 1));
NetworkManager.Instance.InformGameOver();
return;
}
tetManager.DeleteRow();
tetManager.SpawnNextBlock();
NetworkManager.Instance.OnUserMapDisplay();
}
fall = Time.time;
}
}
bool CheckIsValidPosition()
{
foreach(Transform b in block.transform)
{
Vector2 pos = tetManager.Round(b.transform.position);
if (tetManager.CheckIsInside(pos) == false)
return false;
if (tetManager.GetTransformAtMapPosition(pos) != null && tetManager.GetTransformAtMapPosition(pos).parent != block.transform)
return false;
}
return true;
}
}