-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTapAndHold.cs
More file actions
210 lines (186 loc) · 7.57 KB
/
Copy pathTapAndHold.cs
File metadata and controls
210 lines (186 loc) · 7.57 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Linq;
using System;
public class TapAndHold : MonoBehaviour
{
bool isHeld; // is mouse button being held
bool gameLost;
bool scaleIsBeingChanged; // bool for waves animation
List<GameObject> mashPull;
List<GameObject> perfectMoveMashes;
Queue<object[]> parameters; // Mash and how to change its size parameters for ChangeSize() method
new GameObject camera;
Settings settings;
GameObject CurrentMash
{
get
{
return mashPull.LastOrDefault();
}
}
GameObject PreviousMash
{
get
{
if (mashPull.Count > 1)
return mashPull[mashPull.Count - 2];
else
return null;
}
}
void Start()
{
mashPull = new List<GameObject>() { GameObject.FindGameObjectWithTag("FirstCylinder") };
perfectMoveMashes = new List<GameObject>();
parameters = new Queue<object[]>();
camera = GameObject.FindGameObjectWithTag("MainCamera");
settings = SettingsReader.Instance.Settings;
}
void Update()
{
if (Input.GetKey(key: KeyCode.Escape))
Application.Quit();
if (scaleIsBeingChanged) // no actions allowed during animations
return;
if (!Input.GetMouseButton(0) && !Input.GetMouseButton(1))
isHeld = false;
if (gameLost)
{
// after losing the game the player should stop
// holding his mouse button for a moment in order to click and restart the game
if ((Input.GetMouseButton(0) || Input.GetMouseButton(1)) && !isHeld)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // restart the game
}
else return;
}
else
{
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
if (isHeld)
{
CurrentMash.transform.localScale +=
new Vector3(
settings.MashGrowSpeed,
0,
settings.MashGrowSpeed
);
if (CurrentMash.transform.localScale.x >= PreviousMash.transform.localScale.x) // x = z, no need to check z
{
OnLosingTheGame();
}
}
else
{
isHeld = true;
Vector3 latestPosition = CurrentMash.transform.position;
GameObject newMash = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
newMash.transform.position = new Vector3(
latestPosition.x,
latestPosition.y + settings.MashScaleY * 2,
latestPosition.z
);
newMash.transform.localScale = new Vector3(0f, settings.MashScaleY, 0f);
mashPull.Add(Instantiate(newMash));
Destroy(newMash);
camera.transform.position += new Vector3(0f, settings.MashScaleY * 2, 0f);
}
}// stopped holding mouse button
else if (!perfectMoveMashes.Contains(CurrentMash) && !scaleIsBeingChanged && mashPull.Count > 1)
{
if (CurrentMash.transform.localScale.x < settings.MinMashScale)
{
GameObject.Destroy(CurrentMash);
mashPull.RemoveAt(mashPull.Count - 1);
camera.transform.position -= new Vector3(0f, settings.MashScaleY * 2, 0f);
return;
}
// Game over: current mash > previous mash || current mash size < min mash size
if ((CurrentMash.transform.localScale - PreviousMash.transform.localScale).x > 0)
{
OnLosingTheGame();
return;
} // Perfect move: previous mash size >= current mash size for max deviation
else if ((CurrentMash.transform.localScale - PreviousMash.transform.localScale).x
>= -settings.MaxDeviation)
{
OnPerfectMove();
}
}
}
}
void OnLosingTheGame()
{
gameLost = true;
CurrentMash.GetComponent<Renderer>().material.color = Color.red;
camera.transform.position -= new Vector3(0, 0, mashPull.Count * settings.MashScaleY * 2);
Invoke("DestroyCurrentMash", 0.5f);
}
void DestroyCurrentMash()
{
Destroy(CurrentMash);
}
void OnPerfectMove()
{
perfectMoveMashes.Add(CurrentMash);
scaleIsBeingChanged = true;
bool changeOnlyCurrent = false; //perfectMoveMashes.Contains(mashPull[mashPull.Count - 2]);
Vector3 currentMashFirstScale =
CurrentMash.transform.localScale + new Vector3(settings.CurrentMashScaleToAdd, 0, settings.CurrentMashScaleToAdd);
Vector3 currentMashSecondScale = currentMashFirstScale.x > settings.MaxMashScale
? new Vector3(settings.MaxMashScale, settings.MashScaleY, settings.MaxMashScale)
: currentMashFirstScale - new Vector3(settings.CurrentMashScaleToSubstract, 0, settings.CurrentMashScaleToSubstract);
ChangeSize(CurrentMash, currentMashFirstScale, currentMashSecondScale, 0f, changeOnlyCurrent);
if (!changeOnlyCurrent)
{
int startingPosition = mashPull.Count - 2;
bool underSecondPerfectMash = false;
for (int i = startingPosition; i >= 0; i--)
{
Vector3 otherMashFirstScale =
mashPull[i].transform.localScale + new Vector3(
settings.OtherMashesScaleToAdd,
0,
settings.OtherMashesScaleToAdd
);
Vector3 otherMashSecondScale = new Vector3(
mashPull[i].transform.localScale.x * settings.OtherMashesScaleToMultiplyBy,
mashPull[i].transform.localScale.y,
mashPull[i].transform.localScale.z * settings.OtherMashesScaleToMultiplyBy
);
ChangeSize(
mashPull[i],
otherMashFirstScale,
underSecondPerfectMash ? mashPull[i].transform.localScale : otherMashSecondScale,
(startingPosition - i) / 10f + 0.3f,
i == 0 || perfectMoveMashes.Contains(mashPull[i - 1])
);
underSecondPerfectMash = underSecondPerfectMash || perfectMoveMashes.Contains(mashPull[i]);
}
}
}
void ChangeSize(GameObject toChange, Vector3 first, Vector3 second, float delayTime = 0f, bool final = false)
{
parameters.Enqueue(new object[] { toChange, first });
Invoke("ChangeSize", 0.0f + delayTime);
parameters.Enqueue(new object[] { toChange, second });
Invoke("ChangeSize", 0.1f + delayTime);
if (final)
Invoke("SetScaleIsBeingChangedToFalse", 0.1f + delayTime + 0.1f);
}
void ChangeSize()
{
object[] parameter = parameters.Dequeue();
GameObject toChange = (GameObject)parameter[0];
Vector3 newScale = (Vector3)parameter[1];
toChange.transform.localScale = newScale;
}
void SetScaleIsBeingChangedToFalse()
{
scaleIsBeingChanged = false;
}
}