Tanoda
HandCopySkelet.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.SceneManagement;
5using UnityEngine.UI;
6#if DANA
7using Valve.VR.InteractionSystem;
8using static Valve.VR.InteractionSystem.Hand;
9#endif
10
11public class HandCopySkelet : MonoBehaviour
12{
13#if !UNITY_WEBGL
14 internal bool isCollision = false;
15 internal List<bool> collisionList = new List<bool>();
16 internal GameObject falseHighlightObject;
17 public Material highlightMaterial;
18 public GameObject hand;
19 float time = 0.0f;
20 float penalty = 0.0f;
21 bool isRunning = false;
22 internal bool isPOI = false;
23 public GameObject finish, result;
24 private Vector3 lastHandPos = Vector3.zero;
25 System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
26 bool scroll = false;
27 bool slide = false;
28 bool crRuning = false;
29 internal Coroutine clear;
30 // Start is called before the first frame update
31 private void OnTriggerStay(Collider other)
32 {
33 if (SceneManager.GetActiveScene().buildIndex == 0) return;
34 if (!other.name.Contains("Bone") && !other.name.Contains("Glove") && !isCollision)
35 {
36 var hancCopyAvg = GetComponentInChildren<HandCopyAvg>(true);
37 var hackedHand = hancCopyAvg.GetComponentInParent<HackedHand>();
38 time = 0.0f;
39 var tcd = other.GetComponentInParent<ThrowableCanDisable>();
40 //var flag = tcd.attachme
41 #if DANA
42 if (other.gameObject.GetComponentInParent<Canvas>() && hand.activeInHierarchy)
43 {
44 StopCoroutine(clear);
45 //Debug.Log(TurnThePage(other.gameObject) *100);
46 if ((TurnThePage(other.gameObject).x *100) > 10 && !crRuning)
47 {
48 if (finish.activeInHierarchy || result.activeInHierarchy)
49 {
50 if (finish.activeInHierarchy)
51 {
52 StartCoroutine(ChangeTheAlpha(finish, 1, 0));
53 finish.SetActive(false);
54 result.SetActive(true);
55 StartCoroutine(ChangeTheAlpha(result, 0, 1));
56 }
57 else
58 {
59 StartCoroutine(ChangeTheAlpha(result, 1, 0));
60 result.SetActive(false);
61 finish.SetActive(true);
62 StartCoroutine(ChangeTheAlpha(finish, 0, 1));
63 }
64 }
65 }
66 //Debug.Log(TurnThePage(other.gameObject) *100);
67
68 //if (result.activeInHierarchy && !crRuning)
69 //{
70 // if ((TurnThePage(other.gameObject).y * 100) > 5)
71 // {
72 // StartCoroutine(Scroll(other.gameObject, 0.2f));
73 // }
74 // if ((TurnThePage(other.gameObject).y * 100) < -5)
75 // {
76 // StartCoroutine(Scroll(other.gameObject, -0.2f));
77 // }
78 //}
79
80
81 }
82 if (ToolPositioner.instance.IsTool(other.gameObject) || tcd && tcd.attachmentFlags.HasFlag(Hand.AttachmentFlags.SnapOnAttach))
83 {
84 stopwatch.Start();
85 var colliders = hancCopyAvg.GetComponents<Collider>();
86 foreach (var collider in colliders)
87 {
88 collider.enabled = false;
89 }
90 stopwatch.Stop();
91 Debug.Log($"7 th foreach: {stopwatch.ElapsedMilliseconds}ms");
92 }
93
94 if (other.GetComponentInParent<InteractableHoverEvents>())
95 {
96 isPOI = true;
97 }
98
99 if (other.gameObject.name.Contains("einhell"))
100 {
101 hancCopyAvg.GetComponentInChildren<SkinnedMeshRenderer>(true).enabled = false;
102 hackedHand.realToolInHand = true;
103 }
104
105 if (!isRunning && !isCollision && ConveyorBeltManager.instance.started && GetComponentInChildren<HandCopySkelet>() && tcd && !tcd.enabled && tcd.isActiveAndEnabled == false && other.gameObject.activeSelf && !GetComponentInParent<HackedHand>().attachedToHand/*Macro.CollidesWithActiveTCD(GetComponentInChildren<HandCopySkelet>().gameObject, false)*/)
106 {
107 //Debug.Log(other.name);
108 if (!GetComponentInChildren<TwoHandGrab>())
109 {
110 penalty = 0.0f;
111 //if (other.GetComponent<ThrowableCanDisable>())
112 //{
113 if (!tcd.wrong && MiscLogicManager.instance.mode == MiscLogicManager.CurrentMode.Training)
114 {
115 StartCoroutine(ShowToolHighLight(tcd.gameObject));
116 }
117
118 //}
119 }
120
121
122 }
123 #endif
124 isCollision = true;
125 collisionList.Add(true);
126
127 //Debug.Log(isCollision);
128 }
129 if (other.name.Contains("Bone") && other.name.Contains("Glove"))
130 {
131 isCollision = false;
132 }
133 if (other.name.Contains("handprint"))
134 {
135 //var colliders = GetComponentInChildren<HandCopyAvg>(true).GetComponents<Collider>();
136 //foreach (var collider in colliders)
137 //{
138 // //collider.isTrigger = true;
139 //}
140
141 }
142
143
144 }
145 private void Update()
146 {
147 time += Time.deltaTime;
148 penalty += Time.deltaTime;
149 }
150
151 private Vector3 TurnThePage(GameObject other)
152 {
153 if (lastHandPos == Vector3.zero)
154 {
155 lastHandPos = gameObject.transform.position;
156 return Vector3.zero;
157 }
158 var localHandPos = other.transform.InverseTransformPoint(gameObject.transform.position);
159 var localLastHandPos = other.transform.InverseTransformPoint(lastHandPos);
160
161 var handDelta = lastHandPos - gameObject.transform.position;
162 var localDelta = other.transform.InverseTransformDirection(handDelta);
163 //lastHandPos = gameObject.transform.position;
164 Debug.Log(localDelta*100);
165 return localDelta;
166 }
167 private IEnumerator setCrRunning()
168 {
169 yield return new WaitForSeconds(1);
170 crRuning = false;
171 }
172 private IEnumerator ChangeTheAlpha(GameObject go, float start, float finish)
173 {
174 float time = 0;
175 float duration = 0.5f;
176 crRuning = true;
177 var cggo = go.GetComponent<CanvasGroup>();
178 while (time <= duration)
179 {
180 time += Time.deltaTime;
181 cggo.alpha = Mathf.Lerp(0, 1, time / duration);
182
183 //alpha.a = Mathf.Lerp(startAlpha, endAlpha, time / duration);
184 yield return null;
185 }
186 yield return null;
187 crRuning = false;
188 }
189 private IEnumerator Scroll(GameObject go, float value2)
190 {
191 float time = 0;
192 float duration = 0.5f;
193 crRuning = true;
194 var sb = go.GetComponentInChildren<ScrollRect>();
195 var scrollValue = sb.verticalScrollbar.value;
196 while (time <= duration)
197 {
198 time += Time.deltaTime;
199 sb.verticalScrollbar.value = Mathf.Lerp(sb.verticalScrollbar.value, scrollValue + value2, time / duration);
200
201 //alpha.a = Mathf.Lerp(startAlpha, endAlpha, time / duration);
202 yield return null;
203 }
204 yield return null;
205 crRuning = false;
206 }
207 private void OnTriggerExit(Collider other)
208 {
209 var hackedHand = GetComponentInParent<HackedHand>();
210 isPOI = false;
211 if (!isCollision) clear = StartCoroutine(clearVector());
212 if (crRuning) StartCoroutine(setCrRunning());
213#if DANA
214 if (time > 0.5f)
215 {
216 if (falseHighlightObject) Destroy(falseHighlightObject);
217 if (isRunning)
218 {
219 PenaltyCounter.Instance.penaltyTime += penalty;
220 }
221 StopAllCoroutines();
222 isRunning = false;
223 time = 0f;
224 }
225
226 //if (!hackedHand.attachedToHand)
227 //{
228 var colliders = GetComponentInChildren<HandCopyAvg>(true).GetComponentsInChildren<Collider>();
229 foreach (var collider in colliders)
230 {
231 collider.enabled = true;
232 }
233 //}
234
235
236 GetComponentInChildren<HandCopyAvg>().GetComponentInChildren<SkinnedMeshRenderer>(true).enabled = true;
237 if (other.gameObject.name.Contains("einhell")) hackedHand.realToolInHand = false;
238 collisionList.Remove(true);
239 if (other.name.Contains("handprint"))
240 {
241 colliders = GetComponentInChildren<HandCopyAvg>(true).GetComponents<Collider>();
242 foreach (var collider in colliders)
243 {
244 // collider.isTrigger = false;
245 }
246 }
247#endif
248 isCollision=false;
249 //Debug.Log(isCollision);
250 }
251 private IEnumerator clearVector()
252 {
253 yield return new WaitForSeconds(0.5f);
254 lastHandPos = Vector3.zero;
255 }
256 private void OnDisable()
257 {
258 if (falseHighlightObject) Destroy(falseHighlightObject);
259 if (isRunning)
260 {
261 PenaltyCounter.Instance.penaltyTime += penalty;
262 }
263 StopAllCoroutines();
264 isRunning = false;
265 time = 0f;
266 isCollision=false;
267 }
268
269 private IEnumerator ShowToolHighLight(GameObject other) //BLUE
270 {
271 isRunning = true;
272 if (falseHighlightObject) Destroy(falseHighlightObject);
273
274 if (MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Training && MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Optimization) yield break;
275
276 yield return new WaitForEndOfFrame();
277 var inflate = true;
278
279
280 falseHighlightObject = Instantiate(other, other.transform.position, other.transform.rotation, null);
281#if !UNITY_WEBGL
282 Destroy(falseHighlightObject.GetComponent<TorqueWrench>());
283 Destroy(falseHighlightObject.GetComponent<SeegerHelper>());
284 Destroy(falseHighlightObject.GetComponent<ThrowableCanDisable>());
285#if DANA
286 Destroy(falseHighlightObject.GetComponent<Interactable>());
287#endif
288#endif
289 var allCollider = falseHighlightObject.GetComponentsInChildren<Collider>();
290 var allRb = falseHighlightObject.GetComponentsInChildren<Rigidbody>();
291
292 foreach (var c in allCollider)
293 Destroy(c);
294
295 foreach (var c in allRb)
296 Destroy(c);
297
298 Destroy(falseHighlightObject.GetComponent<HashHolder>());
299
300 yield return new WaitForEndOfFrame();
301
302 //falseHighlightObject.transform.SetPositionAndRotation(other.transform.position, other.transform.rotation);
303
304 if (inflate)
305 falseHighlightObject.transform.localScale *= 1.001f;
306
307 falseHighlightObject.SetActive(true);
308
309 MatChange(falseHighlightObject, true);
310 }
311
312 private void MatChange(GameObject go, bool tool = false)
313 {
314 var mrs = go.GetComponentsInChildren<MeshRenderer>();
315 foreach (var meshRenderer in mrs)
316 {
317 var list = new List<Material>();
318 for (var i = 0; i < meshRenderer.materials.Length; i++)
319 list.Add(highlightMaterial);
320
321 meshRenderer.materials = list.ToArray();
322 }
323 }
324#endif
325 }
326
327
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
GameObject hand
Material highlightMaterial
GameObject finish
GameObject result
static PenaltyCounter Instance
bool IsTool(GameObject tool)
static ToolPositioner instance