summaryrefslogtreecommitdiff
path: root/_ActiveRagdoll/Actions/Holding.cs
blob: eb2709521d345318246b73cfe8a19a21774d1646 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Player Holding ¶¯×÷-ÄÃÎäÆ÷
public class Holding : MonoBehaviour
{
	[HideInInspector]
	public HoldableObject heldObject;

	[HideInInspector]
	public HoldableObject heldObjectOffHand;

	[HideInInspector]
	public Transform baseTransform;

	private Rigidbody rightHand;

	private Rigidbody leftHand;

	[HideInInspector]
	public ConfigurableJoint leftHandJoint;

	[HideInInspector]
	public ConfigurableJoint rightHandJoint;

	public float grabForce;

	[HideInInspector]
	public bool isGrabbing;

	private Transform rotaionTarget;

	private bool hasMainHandWeapon;

	private PlayerDeath death;

	public LayerMask mask = default(LayerMask);

	private Strength str;

	private float strength = 1f;

	private PID leftHandPID;

	private PID rightHandPID;

	private StandingDataHandler standingData;

	private void Awake()
	{
		death = GetComponent<PlayerDeath>();
		standingData = GetComponent<StandingDataHandler>();
		str = GetComponent<Strength>();
		if (!baseTransform)
		{
			baseTransform = GetComponentInChildren<HoldingBaseTransform>().transform;
		}
		rightHand = GetComponentInChildren<HandRight>().GetComponent<Rigidbody>();
		leftHand = GetComponentInChildren<HandLeft>().GetComponent<Rigidbody>();
		rotaionTarget = base.transform.GetComponentInChildren<RotationTarget>().transform;
		mask = LayerMask.GetMask("Map");
		leftHandPID = leftHand.GetComponent<PID>();
		rightHandPID = rightHand.GetComponent<PID>();
	}

	private void Update()
	{
		if (!death.dead && (bool)heldObject)
		{
			strength = str.strength;
			if (((bool)rightHandJoint || !heldObject.rightHandPos) && ((bool)leftHandJoint || !heldObject.leftHandPos) && (!heldObjectOffHand || (((bool)rightHandJoint || !heldObjectOffHand.rightHandPos) && ((bool)leftHandJoint || !heldObjectOffHand.leftHandPos))))
			{
				isGrabbing = false;
			}
		}
	}

	private void FixedUpdate()
	{
		if (death.dead || isGrabbing)
		{
			return;
		}
		if ((bool)heldObject)
		{
			HoldObjectInPlace(heldObject.rig, heldObject, offHand: false, rightHand);
			if (!heldObjectOffHand && (bool)leftHandJoint)
			{
				HoldObjectInPlace(heldObject.rig, heldObject, offHand: false, leftHand, justHand: true);
			}
		}
		if ((bool)heldObjectOffHand)
		{
			HoldObjectInPlace(heldObjectOffHand.rig, heldObjectOffHand, offHand: true, leftHand);
		}
	}

	private void HoldObjectInPlace(Rigidbody rigi, HoldableObject held, bool offHand, Rigidbody hand, bool justHand = false)
	{
		Vector3 holdingPosition = held.holdingPosition;
		if (offHand)
		{
			holdingPosition.x *= -1f;
		}
		holdingPosition = baseTransform.TransformPoint(holdingPosition);
		Vector3 holdingRotation = held.holdingRotation;
		Vector3 targetRotation = baseTransform.TransformDirection(holdingRotation);
		Vector3 targetRotationUp = baseTransform.TransformDirection(held.holdingUpRotation);
		if (!justHand)
		{
			held.pid.DoPID(holdingPosition, targetRotation, targetRotationUp, strength);
		}
		PID pID = rightHandPID;
		Vector3 localPosition = held.rightHandPos.localPosition;
		if (hand == leftHand)
		{
			localPosition = held.leftHandPos.localPosition;
			pID = leftHandPID;
		}
		Vector3 position = held.holdingPosition + localPosition;
		if (offHand)
		{
			position.x *= -1f;
		}
		position = baseTransform.TransformPoint(position);
		Debug.DrawLine(position, position + Vector3.up);
		Debug.DrawLine(holdingPosition, holdingPosition + Vector3.up);
		pID.DoPID(position, Vector3.zero, Vector3.zero, strength);
	}

	public void StartHolding(HoldableObject holdableObject, bool hasOffHand)
	{
		holdableObject.isHeld = true;
		isGrabbing = true;
		bool offHand = false;
		if (!hasMainHandWeapon)
		{
			hasMainHandWeapon = true;
			heldObject = holdableObject;
			if ((bool)heldObject.rightHandPos)
			{
				StartCoroutine(Grab(mainHand: true, rightHand, heldObject, offHand: false));
			}
			if ((bool)heldObject.leftHandPos && !hasOffHand)
			{
				StartCoroutine(Grab(mainHand: false, leftHand, heldObject, offHand: false));
			}
		}
		else
		{
			offHand = true;
			heldObjectOffHand = holdableObject;
			heldObjectOffHand.leftHandPos = heldObjectOffHand.rightHandPos;
			if ((bool)heldObjectOffHand.rightHandPos)
			{
				StartCoroutine(Grab(mainHand: false, leftHand, heldObjectOffHand, offHand: true));
			}
		}
		StartCoroutine(HoldweaponStill(holdableObject.rig, offHand, holdableObject));
	}

	private IEnumerator Grab(bool mainHand, Rigidbody hand, HoldableObject held, bool offHand)
	{
		while (isGrabbing)
		{
			if (mainHand)
			{
				if (!rightHandJoint)
				{
					ReachForPoint(rightHand, rightHand.transform.GetChild(0).position, held.rightHandPos, isLeft: false, held.rig, held, offHand);
					rightHand.GetComponentInChildren<Collider>().enabled = false;
				}
			}
			else if (!leftHandJoint)
			{
				ReachForPoint(leftHand, leftHand.transform.GetChild(0).position, held.leftHandPos, isLeft: true, held.rig, held, offHand);
				leftHand.GetComponentInChildren<Collider>().enabled = false;
			}
			yield return null;
		}
		leftHand.GetComponentInChildren<Collider>().enabled = true;
		rightHand.GetComponentInChildren<Collider>().enabled = true;
	}

	private IEnumerator HoldweaponStill(Rigidbody rigToGrab, bool offHand, HoldableObject held)
	{
		while (isGrabbing)
		{
			Vector3 pos = held.holdingPosition;
			if (offHand)
			{
				pos.x *= -1f;
			}
			rigToGrab.transform.position = baseTransform.TransformPoint(pos);
			rigToGrab.transform.rotation = Quaternion.LookRotation(baseTransform.TransformDirection(heldObject.holdingRotation));
			yield return null;
		}
	}

	public void Drop()
	{
		List<HoldableObject> list = new List<HoldableObject>();
		if ((bool)heldObject)
		{
			list.Add(heldObject);
		}
		if ((bool)heldObjectOffHand)
		{
			list.Add(heldObjectOffHand);
		}
		for (int i = 0; i < list.Count; i++)
		{
			list[i].holder = null;
			list[i].isHeld = false;
			list[i].rig.useGravity = true;
			list[i].rig.drag = 0f;
			list[i].rig.angularDrag = 0f;
			Collider[] componentsInChildren = list[i].GetComponentsInChildren<Collider>();
			foreach (Collider collider in componentsInChildren)
			{
				collider.material = null;
			}
		}
		heldObject = null;
		heldObjectOffHand = null;
		if ((bool)rightHandJoint)
		{
			Object.Destroy(rightHandJoint);
		}
		if ((bool)leftHandJoint)
		{
			Object.Destroy(leftHandJoint);
		}
		hasMainHandWeapon = false;
	}

	private void ReachForPoint(Rigidbody rigToReach, Vector3 forcePosition, Transform targetPositionTransform, bool isLeft, Rigidbody rigToGrab, HoldableObject held, bool offHand)
	{
		Vector3 normalized = (targetPositionTransform.position - forcePosition).normalized;
		rigToReach.AddForceAtPosition(normalized * grabForce * Mathf.Clamp(Time.deltaTime, 0f, 0.1f), forcePosition, ForceMode.Acceleration);
		if (Vector3.Distance(targetPositionTransform.position, forcePosition) < 0.5f)
		{
			ConnectRig(rigToReach, rigToGrab, targetPositionTransform, isLeft, held, offHand);
		}
	}

	private void ConnectRig(Rigidbody startRig, Rigidbody targetRig, Transform targetPositionTransform, bool isLeft, HoldableObject held, bool offHand)
	{
		ConfigurableJoint configurableJoint = startRig.gameObject.AddComponent<ConfigurableJoint>();
		if (offHand)
		{
			targetPositionTransform.localRotation = Quaternion.Euler(targetPositionTransform.localEulerAngles.x, 0f - targetPositionTransform.localEulerAngles.y, targetPositionTransform.localEulerAngles.z);
		}
		startRig.transform.rotation = targetPositionTransform.rotation;
		startRig.transform.position += targetPositionTransform.position - startRig.transform.GetChild(0).position;
		configurableJoint.connectedBody = targetRig;
		configurableJoint.projectionMode = JointProjectionMode.PositionAndRotation;
		configurableJoint.xMotion = ConfigurableJointMotion.Locked;
		configurableJoint.yMotion = ConfigurableJointMotion.Locked;
		configurableJoint.zMotion = ConfigurableJointMotion.Locked;
		configurableJoint.angularXMotion = ConfigurableJointMotion.Limited;
		configurableJoint.angularYMotion = ConfigurableJointMotion.Limited;
		configurableJoint.angularZMotion = ConfigurableJointMotion.Limited;
		SoftJointLimit highAngularXLimit = configurableJoint.highAngularXLimit;
		highAngularXLimit.limit = held.swingAngles.y;
		configurableJoint.highAngularXLimit = highAngularXLimit;
		highAngularXLimit.limit = held.swingAngles.x;
		configurableJoint.lowAngularXLimit = highAngularXLimit;
		highAngularXLimit.limit = held.twistAngles.x;
		configurableJoint.angularYLimit = highAngularXLimit;
		highAngularXLimit.limit = held.twistAngles.y;
		configurableJoint.angularZLimit = highAngularXLimit;
		SoftJointLimitSpring angularXLimitSpring = configurableJoint.angularXLimitSpring;
		angularXLimitSpring.spring = held.swingSpring;
		configurableJoint.angularXLimitSpring = angularXLimitSpring;
		angularXLimitSpring.spring = held.twistSpring;
		configurableJoint.angularYZLimitSpring = angularXLimitSpring;
		configurableJoint.anchor = startRig.transform.InverseTransformPoint(startRig.transform.GetChild(0).position);
		if (isLeft)
		{
			leftHandJoint = configurableJoint;
		}
		else
		{
			rightHandJoint = configurableJoint;
		}
	}
}