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
/
GrabbableTransformHandler.cs
149 lines (129 loc) · 5.01 KB
/
GrabbableTransformHandler.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
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
using Assets.Gamelogic.Player;
using Assets.Gamelogic.Utils;
using Improbable;
using Improbable.General;
using Improbable.Global;
using Improbable.Unity.Core;
using Improbable.Unity.Entity;
using Improbable.Unity.Visualizer;
using Improbable.Worker;
using UnityEngine;
namespace Assets.Gamelogic.Grabbing
{
public class GrabbableTransformHandler : MonoBehaviour
{
[Require] private Grabbable.Reader GrabbableReader;
[Require] private Position.Reader PositionReader;
[Require] private Rotation.Reader RotationReader;
private GameObject grabberControllerGameObject;
private Vector3 smoothedVelocity;
private Vector3 previousPosition;
[SerializeField] private Rigidbody modelRigidbody;
private void OnEnable()
{
transform.position = PositionReader.Data.coords.ToUnityVector();
transform.rotation = RotationReader.Data.rotation.ToUnityQuaternion();
previousPosition = transform.position;
smoothedVelocity = Vector3.zero;
UpdateGrabberObject();
GrabbableReader.ComponentUpdated.Add(OnGrabbableUpdate);
PositionReader.ComponentUpdated.Add(OnPositionUpdate);
RotationReader.ComponentUpdated.Add(OnRotationUpdate);
}
private void OnDisable()
{
GrabbableReader.ComponentUpdated.Remove(OnGrabbableUpdate);
PositionReader.ComponentUpdated.Remove(OnPositionUpdate);
RotationReader.ComponentUpdated.Remove(OnRotationUpdate);
}
private void OnGrabbableUpdate(Grabbable.Update update)
{
UpdateGrabberObject();
}
private void OnPositionUpdate(Position.Update update)
{
if (!CurrentlyBeingHeld() && PositionReader.Authority == Authority.NotAuthoritative)
{
if (update.coords.HasValue)
{
transform.position = update.coords.Value.ToUnityVector();
}
}
}
private void OnRotationUpdate(Rotation.Update update)
{
if (!CurrentlyBeingHeld() && RotationReader.Authority == Authority.NotAuthoritative)
{
if (update.rotation.HasValue)
{
transform.rotation = update.rotation.Value.ToUnityQuaternion();
}
}
}
private void UpdateGrabberObject()
{
if (CurrentlyBeingHeld())
{
// May have been passed from one grabber to another
SetGrabberObject();
}
else
{
RemoveGrabberObject();
}
}
private bool CurrentlyBeingHeld()
{
return GrabbableReader.Data.currentGrabberInfo.HasValue;
}
private void SetGrabberObject()
{
var grabberEntityId = GrabbableReader.Data.currentGrabberInfo.Value.grabberEntity;
var controllerSide = GrabbableReader.Data.currentGrabberInfo.Value.controllerSide;
var grabberObject = LocalEntities.Instance.Get(grabberEntityId).UnderlyingGameObject;
if (grabberObject != null)
{
SetGrabberController(grabberObject.GetComponent<PlayerControllers>().GetController(controllerSide));
}
}
public void RemoveGrabberObject()
{
if (grabberControllerGameObject != null)
{
grabberControllerGameObject = null;
if (PositionReader.Authority == Authority.Authoritative)
{
modelRigidbody.isKinematic = false;
modelRigidbody.useGravity = true;
modelRigidbody.velocity = smoothedVelocity;
}
}
}
private void SetGrabberController(GameObject controllerObject)
{
modelRigidbody.isKinematic = true;
modelRigidbody.useGravity = false;
grabberControllerGameObject = controllerObject;
}
private void Update()
{
MatchGrabberControllerTransform();
}
private void MatchGrabberControllerTransform()
{
if (grabberControllerGameObject != null)
{
var grabberInfo = GrabbableReader.Data.currentGrabberInfo.Value;
var grabberTransform = grabberControllerGameObject.transform;
transform.position = grabberTransform.position + grabberTransform.rotation * grabberInfo.relativePosition.ToUnityVector();
transform.rotation = grabberTransform.rotation * grabberInfo.relativeOrientation.ToUnityQuaternion();
CacheCurrentVelocity(transform.position);
}
}
private void CacheCurrentVelocity(Vector3 currentPosition)
{
smoothedVelocity = smoothedVelocity * 0.5f + (currentPosition - previousPosition) * 0.5f / Time.deltaTime;
previousPosition = currentPosition;
}
}
}