Tanoda
ManipulatorArm.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using NaughtyAttributes;
6
7public class ManipulatorArm : MonoBehaviour
8{
9 public static ManipulatorArm instance;
10 public Vector2 limitsV, limitsH;
11 public GameObject verticalMover, horizontalBar;
12 public GameObject followObject, helper, verticalHelper;
13 public bool toolEnabled = false;
14 public bool rotate = false;
15
16 private Vector3 origPos;
17 private float offsetDistance;
18 private float autoReturn = 6f;
19 public UnityEvent onPickUp, onDelayPickUp, onDetach;
20 public float delayEventSeconds = 1.0f;
21 private bool blueIsHidden = false;
22 bool isInDelayedAction = false;
23
24 void Start()
25 {
26 if (!instance)
27 instance = this;
28
30 {
31 Debug.LogError("Nincs a jeleneten ManipulatorHandle!");
32 return;
33 }
34 origPos = ManipulatorHandle.instance.transform.position;
35 offsetDistance = Vector3.Distance(ManipulatorHandle.instance.transform.position, ManipulatorHandle.instance.GetComponent<Collider>().bounds.center);
36 Debug.Log(offsetDistance);
37 HideMe();
38 }
39
40 void Update()
41 {
42 if (!toolEnabled)
43 return;
44
45 if (followObject)
46 {
47 autoReturn = 6f;
48 var followTransform = followObject.transform;
49 var offsetDirection = ManipulatorHandle.instance.transform.position - ManipulatorHandle.instance.GetComponent<Collider>().bounds.center;
50
51 var movepos = followTransform.position + offsetDirection.normalized * offsetDistance;
52 ToolMover(movepos);
53 if (!blueIsHidden)
54 {
55 blueIsHidden = true;
56 onPickUp.Invoke();
57 StartCoroutine(DelayedPickupEvent());
58 }
59 }
60 else
61 {
62 blueIsHidden = false;
63 autoReturn -= Time.deltaTime;
64 if (autoReturn <= 0)
65 {
66 autoReturn = 6f;
67 StartCoroutine(ResetPos());
68 }
69 }
70
71 EnforceTheLimits();
72 }
73
74 public void HideMe()
75 {
76 if (!this) return;
77
78 var cc = transform.childCount;
79 for (int i = 0; i < cc; i++)
80 {
81 transform.GetChild(i).gameObject.SetActive(false);
82 }
83
84 }
85
86 public void ShowMe()
87 {
88 if (!this) return;
89 var cc = transform.childCount;
90 for (int i = 0; i < cc; i++)
91 {
92 transform.GetChild(i).gameObject.SetActive(true);
93 }
94 }
95
96 private void ToolMover(Vector3 pos)
97 {
98 helper.transform.localPosition = Vector3.zero;
99 helper.transform.position = Vector3.MoveTowards(helper.transform.position, pos, 0.5f);
100
101 var lp = helper.transform.localPosition;
102 helper.transform.localPosition = new Vector3(lp.x, 0, lp.z);
103
104 verticalMover.transform.LookAt(helper.transform, verticalMover.transform.up);
105 verticalMover.transform.localEulerAngles += Vector3.down * 90;
106
107 horizontalBar.transform.localPosition = Vector3.zero;
108 horizontalBar.transform.position =
109 Vector3.MoveTowards(horizontalBar.transform.position, helper.transform.position, 10);
110 var to = horizontalBar.transform.localPosition;
111 var dir = (-to).normalized;
112 horizontalBar.transform.localPosition += 9.5f * dir;
113
114 verticalHelper.transform.position = Vector3.MoveTowards(helper.transform.position, pos, 0.5f);
115 lp = verticalHelper.transform.localPosition;
116 var vlp = verticalMover.transform.localPosition;
117
118 verticalMover.transform.localPosition = new Vector3(vlp.x, lp.y, vlp.z);
119 }
120
121 public void ResetCleco(bool disable = false)
122 {
123 followObject = null;
124 StartCoroutine(ResetPos());
125 onDetach.Invoke();
126 blueIsHidden = false;
127 if (disable)
128 toolEnabled = false;
129 }
130
131 private IEnumerator DelayedPickupEvent()
132 {
133 isInDelayedAction = true;
134 yield return new WaitForSeconds(delayEventSeconds);
135 onDelayPickUp?.Invoke();
136 isInDelayedAction = false;
137 }
138
139 private IEnumerator ResetPos()
140 {
141 for (float i = 0; i < 1; i += Time.deltaTime)
142 {
143 followObject = null;
144 ToolMover(Vector3.Lerp(helper.transform.position, origPos, i));
145 yield return new WaitForEndOfFrame();
146 }
147 ToolMover(origPos);
148 }
149
150 private void EnforceTheLimits()
151 {
152 var vlp = verticalMover.transform.localPosition;
153 if (verticalMover.transform.localPosition.y > limitsV.y)
154 {
155 verticalMover.transform.localPosition = new Vector3(vlp.x, limitsV.y, vlp.z);
156 followObject = null;
157 }
158 else if (verticalMover.transform.localPosition.y < limitsV.x)
159 {
160 verticalMover.transform.localPosition = new Vector3(vlp.x, limitsV.x, vlp.z);
161 followObject = null;
162 }
163 var hlp = horizontalBar.transform.localPosition;
164 if (horizontalBar.transform.localPosition.x > limitsH.y)
165 {
166 horizontalBar.transform.localPosition = new Vector3(limitsH.y, hlp.y, hlp.z);
167 followObject = null;
168 }
169 else if (horizontalBar.transform.localPosition.x < limitsH.x)
170 {
171 horizontalBar.transform.localPosition = new Vector3(limitsH.x, hlp.y, hlp.z);
172 followObject = null;
173 }
174 }
175
176 [Button]
177 private void SetMaximumV()
178 {
179 limitsV.y = verticalMover.transform.localPosition.y;
180 }
181 [Button]
182 private void SetMinimumV()
183 {
184 limitsV.x = verticalMover.transform.localPosition.y;
185 }
186
187 [Button]
188 private void SetMaximumH()
189 {
190 limitsH.y = horizontalBar.transform.localPosition.x;
191 }
192 [Button]
193 private void SetMinimumH()
194 {
195 limitsH.x = horizontalBar.transform.localPosition.x;
196 }
197}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void ResetCleco(bool disable=false)
UnityEvent onDelayPickUp
GameObject verticalMover
UnityEvent onPickUp
GameObject followObject
GameObject horizontalBar
UnityEvent onDetach
static ManipulatorArm instance
GameObject verticalHelper
GameObject helper
float delayEventSeconds
static ManipulatorHandle instance