Documentation for the Unity C# Library
Loading...
Searching...
No Matches
InteractableClass.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using Autohand;
5using JJKeller;
6using PixoVR.Core;
7using UnityEngine;
8using Random = UnityEngine.Random;
9
10namespace PixoVR.Event
11{
16 [RequireComponent(typeof(RegisterItem))]
17 public abstract class InteractableClass : MonoBehaviour
18 {
20 public bool IsActive;
21 public bool IsBroken;
22 public int BrokenId = 0;
23 public bool isInspected = false;
25 [SerializeField] protected GameObject[] correctModels;
26 [SerializeField] protected GameObject[] brokenModels;
27 [SerializeField] private Outline _outline;
28 [SerializeField] private Outline[] _additionalOutlines;
29 [SerializeField] protected DistanceGrabbable _distanceGrabbable;
30
31 private const int MinValue = 1;
32 private const int MaxValue = 100;
33
34 protected Grabbable[] _grabbables;
35 private bool _waitingForSelection;
37 private Coroutine _timerCoroutine;
38
43 public virtual void Interactive(bool state)
44 {
45 IsActive = state;
47 }
48
49 public virtual void Awake()
50 {
51 _grabbables = GetComponentsInChildren<Grabbable>();
52 }
53
54 public virtual void Active(bool state)
55 {
56 IsActive = state;
57 }
62 protected void ShowModels()
63 {
64 if (correctModels.Length > 0)
65 {
66 correctModels[(int)ModelState.NORMAL].SetActive(true);
67 }
68
69 if ((IsBroken) && (correctModels.Length > 1))
70 {
71 correctModels[(int)ModelState.NORMAL].SetActive(false);
72
73 int randIndex = Random.Range(1, correctModels.Length);
74
75 for (int i = 1; i < correctModels.Length; i++)
76 {
77 correctModels[i].SetActive(i == randIndex);
78 }
79 }
80 }
81
86 public virtual void Highlight(bool state)
87 {
88 if (_outline == null)
89 {
90 Debug.LogWarning("Outline is not exist");
91 }
92 else
93 {
94 _outline.enabled = state;
95 }
96
97
98 if (_additionalOutlines != null)
99 {
100 foreach (var outline in _additionalOutlines)
102 outline.enabled = state;
103 }
104 }
105 }
106
111 public virtual void WaitForSelection()
112 {
114 {
116
117 if (_distanceGrabbable == null && _grabbables == null)
118 {
119 Debug.LogWarning("Grabbable is not exist");
120
121 return;
122 }
123
124 _distanceGrabbable?.StartTargeting.AddListener(OnStartTargeting);
125
126 _distanceGrabbable?.StopTargeting.AddListener(OnStopTargeting);
127
128 for (int i = 0; i < _grabbables?.Length; i++)
129 {
130 _grabbables[i].onHighlight.AddListener(OnStartTargeting);
131 _grabbables[i].onUnhighlight.AddListener(OnStopTargeting);
132 }
133 }
134 }
135
139 public virtual void TurnOffWaitForSelection()
140 {
142 {
143 _waitingForSelection = false;
144
145 if (_distanceGrabbable == null && _grabbables == null)
146 {
147 Debug.LogWarning("Grabbable is not exist");
148
149 return;
150 }
151
152 _distanceGrabbable?.StartTargeting.RemoveListener(OnStartTargeting);
153
154 _distanceGrabbable?.StopTargeting.RemoveListener(OnStopTargeting);
155
156 for (int i = 0; i < _grabbables?.Length; i++)
157 {
158 _grabbables[i].onHighlight.RemoveListener(OnStartTargeting);
159 _grabbables[i].onUnhighlight.RemoveListener(OnStopTargeting);
160 }
161 }
162 }
163
164 private void OnStartTargeting(Hand hand, Grabbable grabbable)
165 {
166 Highlight(true);
167 }
168
169 private void OnStopTargeting(Hand hand, Grabbable grabbable)
170 {
171 Highlight(false);
172 }
173
178 public virtual void OpenInspectionMenu(string quizId)
180 }
181
185 public virtual void ShowModel()
186 {
187 foreach (var model in correctModels)
188 {
189 model.SetActive(!IsBroken);
190 }
191
192 foreach (var model in brokenModels)
194 model.SetActive(IsBroken);
195 }
196 }
197
201 public virtual void HideModel()
202 {
203 foreach (var model in correctModels)
204 {
205 model.SetActive(false);
206 }
207
208 foreach (var model in brokenModels)
209 {
210 model.SetActive(false);
211 }
212 }
213
214 public virtual void PrepareInspectionReport(List<InteractableClass> interactables)
215 {
217
218 public virtual void SetParametersForItem(string value, string[] additionalValues)
219 {
220
221 }
222
226 public virtual void SetDefaultState()
227 {
228 }
234 public virtual void GenerateBreakdown(int percentBroke)
235 {
236 IsBroken = Random.Range(MinValue, MaxValue) <= percentBroke;
237 }
238
244 public virtual void PlayForwardAnimation(bool playForward, Action callback)
245 {
246 callback?.Invoke();
247 }
248
252 public virtual void Execute()
253 {
254 EventBetter.Raise(
255 new TriggeredItemEvent()
256 {
257 item = ItemId
258 }
259 );
260 }
261
266 public virtual void ActivateSetCallback(Action callback)
268 }
269
274 public virtual void TurnOnOffState(bool turnOn)
275 {
276 }
277
281 public virtual void RepairElement()
282 {
283 IsBroken = false;
284 isInspected = true;
285 ShowModels();
286 }
287
288 public virtual void SetInspected()
290 isInspected = true;
291 }
292
293 protected void SetGrabEnabled(bool isEnabled)
294 {
295 if (_distanceGrabbable != null)
297 _distanceGrabbable.enabled = isEnabled;
298 }
299
300 if (_grabbables != null)
301 {
302 for (int i = 0; i < _grabbables.Length; i++)
304 _grabbables[i].enabled = isEnabled;
305 }
306 }
307 }
309 public virtual void CheckInspectionStatus()
310 {
311 if (isInspected)
312 {
313 Execute();
314 }
315 else
316 {
317 EventBetter.Raise(
319 {
320 item = ItemsEnum.NONE
321 }
322 );
323 }
325
330 public void SetTimer(float timer)
331 {
332 if (_timerCoroutine != null)
333 {
334 Debug.LogWarning("Stopped previous timer manually");
335
336 StopTimer();
337 }
338
339 _timerCoroutine = StartCoroutine(StartTimer(timer));
340 }
341
345 public void StopTimer()
346 {
347 if (_timerCoroutine != null)
348 {
349 StopCoroutine(_timerCoroutine);
350
351 _timerCoroutine = null;
352 }
353 }
354
355 private IEnumerator StartTimer(float timer)
356 {
357 while (true)
358 {
359 yield return new WaitForSeconds(timer);
362 }
363 }
364
365 public virtual void OnTimerExpired()
366 {
367
368 }
369 }
371 public enum ModelState
372 {
373 NORMAL = 0,
374 BROKEN
375 }
376}
UnityEngine.Random Random
Intentionally made partial, in case you want to extend it easily.
s Base class for interactable objects with which the step manager interacts. It also stores a set of ...
virtual void TurnOnOffState(bool turnOn)
Outline _outline
[SerializeField]
GameObject[] correctModels
[SerializeField]
GameObject[] brokenModels
[SerializeField]
virtual void PlayForwardAnimation(bool playForward, Action callback)
Used to play an animation and wait for it to finish.
virtual void ActivateSetCallback(Action callback)
Waiting for object activation.
virtual void GenerateBreakdown(int percentBroke)
Generate a breakdown on the object with a certain probability.
virtual void TurnOffWaitForSelection()
Disable waiting for object highlighting with a pointer or hand approach. Mainly used in eval fashion.
void StopTimer()
Stop loops and infinitely runs a method.
virtual void Execute()
Used to indicate to the step manager that some actions were performed on the object.
virtual void RepairElement()
Remove from the broken state object.
virtual void Interactive(bool state)
IEnumerator StartTimer(float timer)
DistanceGrabbable _distanceGrabbable
[SerializeField]
virtual void HideModel()
Disable parts of some object childs models. Useful for configuring object breakdowns.
virtual void OpenInspectionMenu(string quizId)
Enable inpection menu UI.
virtual void SetParametersForItem(string value, string[] additionalValues)
virtual void PrepareInspectionReport(List< InteractableClass > interactables)
virtual void Active(bool state)
void SetGrabEnabled(bool isEnabled)
Outline[] _additionalOutlines
[SerializeField]
virtual void Highlight(bool state)
Highlight an object.
void OnStopTargeting(Hand hand, Grabbable grabbable)
virtual void ShowModel()
Enable parts of some object childs models. Useful for configuring object breakdowns.
void SetTimer(float timer)
Loops and infinitely runs a method after some time in seconds.
void OnStartTargeting(Hand hand, Grabbable grabbable)
virtual void SetDefaultState()
Restoring the original state of an object after any actions with it.
virtual void WaitForSelection()
Highlight the object with a pointer or hand approach. Mainly used in evaluation mode.
ItemsEnum
Represents a list of ID for interactable objects.
Definition ItemsEnum.cs:11