This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHandCollisionHandler.cs
95 lines (84 loc) · 3.18 KB
/
HandCollisionHandler.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
using System.Collections.Generic;
using Assets.Gamelogic.Grabbing;
using Improbable.Global;
using Improbable.Unity;
using Improbable.Unity.Visualizer;
using UnityEngine;
using Improbable.Unity.Core;
using Assets.Gamelogic.Utils;
namespace Assets.Gamelogic.Player
{
[WorkerType(WorkerPlatform.UnityClient)]
public class HandCollisionHandler : MonoBehaviour
{
[Require] private ClientAuthorityCheck.Writer ClientAuthorityCheckWriter;
private HashSet<GameObject> reachableObjects;
private GameObject previousClosestObject;
[SerializeField] private HandGrabbingVisualiser handGrabbingVisualiser;
void OnEnable()
{
reachableObjects = new HashSet<GameObject>();
previousClosestObject = null;
}
private static GameObject GetRootGameObject(GameObject gameObject)
{
var entity = gameObject.GetSpatialOsEntity();
if (entity == null)
{
return null;
}
return entity.UnderlyingGameObject;
}
private void OnTriggerEnter(Collider other)
{
if (ClientAuthorityCheckWriter != null)
{
reachableObjects.Add(GetRootGameObject(other.gameObject));
}
}
private void OnTriggerExit(Collider other)
{
if (ClientAuthorityCheckWriter != null)
{
reachableObjects.Remove(GetRootGameObject(other.gameObject));
}
}
public GameObject GetClosestReachableObject()
{
return previousClosestObject;
}
private void Update()
{
var closestObject = GetClosestReachableObject(reachableObjects);
UpdateHighlights(previousClosestObject, closestObject);
previousClosestObject = closestObject;
}
private GameObject GetClosestReachableObject(HashSet<GameObject> currentReachableObjects)
{
GameObject closestGameObject = null;
float closestDistance = 0;
foreach (GameObject reachableObject in currentReachableObjects)
{
float reachableObjectDistance = Vector3.Distance(transform.position, reachableObject.transform.position);
if (closestGameObject == null || reachableObjectDistance < closestDistance)
{
reachableObjectDistance = closestDistance;
closestGameObject = reachableObject;
}
}
return closestGameObject;
}
private void UpdateHighlights(GameObject previousClosestObject, GameObject closestObject)
{
handGrabbingVisualiser.SetGrabbableInRange(closestObject != null);
if (previousClosestObject != null && previousClosestObject != closestObject)
{
previousClosestObject.GetComponent<GrabbableVisualizer>().UpdateGrabbersInRange(-1);
}
if (closestObject != null && previousClosestObject != closestObject)
{
closestObject.GetComponent<GrabbableVisualizer>().UpdateGrabbersInRange(+1);
}
}
}
}