summaryrefslogtreecommitdiff
path: root/GameCode/IkLeg.cs
blob: d8dd137b9f37d241b8a080998d085c1578132f65 (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
290
using Sonigon;
using UnityEngine;

public class IkLeg : MonoBehaviour
{
	[Header("Sounds")]
	[SerializeField]
	private SoundEvent soundCharacterStep;

	[SerializeField]
	private SoundEvent soundCharacterStepBig;

	private SoundParameterIntensity soundParameterIntensity = new SoundParameterIntensity();

	private CharacterData data;

	private float legLenth;

	public Transform legRoot;

	public Transform footTarget;

	public LayerMask mask;

	public AnimationCurve upCurve;

	public AnimationCurve forwardCurve;

	public Transform moveDeltaTransform;

	[HideInInspector]
	public float stepTime;

	public float stepSpeed = 1f;

	private float footDownTime;

	[HideInInspector]
	public bool footDown = true;

	public IkLeg otherLeg;

	private Vector2 restPosition;

	private Vector2 startOffset;

	private Vector2 legRootOffset;

	private float scale = 1f;

	public float prediction = 1f;

	private Vector2 raycastPosLocal;

	private Vector2 raycastPosWorld;

	private Vector2 previousRaycastPosLocal;

	private Vector2 previousRaycastPosWorld;

	private Transform raycastTransform;

	private Transform previousRaycastTransform;

	private Vector2 footPosition;

	private Vector2 deltaPos;

	private Vector2 lastPos;

	private void Start()
	{
		data = GetComponentInParent<CharacterData>();
		legLenth = Vector3.Distance(base.transform.position, footTarget.position);
		startOffset = footTarget.position - data.transform.position;
		legRootOffset = legRoot.position - data.transform.position;
	}

	private void FixedUpdate()
	{
		if ((bool)data)
		{
			SetValuesFixed();
		}
	}

	private void LateUpdate()
	{
		if ((bool)data)
		{
			DoRayCast();
			UpdateRayCastWorldPos();
			DoStep();
			UpdatePreviousRayCastWorldPos();
			UpdateMiscVariables();
			SetFootPos();
			Apply();
		}
	}

	private void AirFootMovement()
	{
		footPosition = Vector3.Lerp(footPosition, restPosition + data.playerVel.velocity * 0.08f, TimeHandler.deltaTime * 15f);
		raycastTransform = null;
	}

	private bool stepWasLost()
	{
		if (Vector3.Distance(footPosition, legRoot.position) > legLenth * 0.5f)
		{
			return true;
		}
		return false;
	}

	private void DoStep()
	{
		if (footDown)
		{
			footDownTime += TimeHandler.deltaTime * stepSpeed / scale;
			if (!(footDownTime > 1f) || !otherLeg.footDown || (!(Mathf.Abs(data.playerVel.velocity.x) > 1f) && !stepWasLost()))
			{
				return;
			}
			StartStep();
			footDownTime = 0f;
			footDown = false;
			if (!data.dead && data.isPlaying && data.isGrounded && !data.isWallGrab)
			{
				soundParameterIntensity.intensity = Mathf.Abs(data.playerVel.velocity.x);
				if (data.stats.SoundTransformScaleThresholdReached())
				{
					SoundManager.Instance.Play(soundCharacterStepBig, data.transform, soundParameterIntensity);
				}
				else
				{
					SoundManager.Instance.Play(soundCharacterStep, data.transform, soundParameterIntensity);
				}
			}
		}
		else
		{
			stepTime += TimeHandler.deltaTime * stepSpeed / scale;
			if (stepTime > 1f)
			{
				EndStep();
				stepTime = 0f;
				footDown = true;
			}
		}
	}

	private void EndStep()
	{
		if ((bool)raycastTransform)
		{
			previousRaycastTransform = raycastTransform;
			previousRaycastPosLocal = raycastPosLocal;
		}
	}

	private void StartStep()
	{
	}

	private void SetFootPos()
	{
		if (data.isGrounded)
		{
			if ((bool)raycastTransform)
			{
				if (footDown)
				{
					footPosition = previousRaycastPosWorld;
				}
				else
				{
					footPosition = Vector3.Lerp(previousRaycastPosWorld, raycastPosWorld, forwardCurve.Evaluate(stepTime)) + Vector3.up * upCurve.Evaluate(stepTime);
				}
			}
		}
		else
		{
			AirFootMovement();
		}
	}

	private void Apply()
	{
		legRoot.position = (Vector2)data.transform.position + legRootOffset * scale;
		footTarget.position = footPosition;
	}

	private void DoRayCast()
	{
		Vector2 vector = deltaPos * prediction;
		Vector2 origin = data.transform.position + base.transform.right * base.transform.localPosition.x;
		Vector2 direction = Vector2.down + vector;
		float distance = legLenth * 1.5f * scale + vector.magnitude;
		RaycastHit2D[] array = Physics2D.RaycastAll(origin, direction, distance, mask);
		RaycastHit2D hit = default(RaycastHit2D);
		for (int i = 0; i < array.Length; i++)
		{
			if (!(array[i].transform.root == data.transform) && (bool)array[i].transform)
			{
				if (!hit.transform)
				{
					hit = array[i];
				}
				else if (array[i].distance < hit.distance)
				{
					hit = array[i];
				}
			}
		}
		if ((bool)hit.transform)
		{
			HitGround(hit);
		}
		else
		{
			HitNothing();
		}
	}

	private void HitNothing()
	{
	}

	private void HitGround(RaycastHit2D hit)
	{
		bool flag = false;
		if (!raycastTransform && (bool)hit.transform)
		{
			flag = true;
		}
		if ((bool)raycastTransform && raycastTransform != hit.transform)
		{
			MigrateGroundHit(raycastTransform, hit.transform, hit);
		}
		raycastTransform = hit.transform;
		raycastPosLocal = raycastTransform.InverseTransformPoint(hit.point);
		if (flag)
		{
			Land(hit);
		}
	}

	private void MigrateGroundHit(Transform from, Transform to, RaycastHit2D hit)
	{
	}

	private void Land(RaycastHit2D hit)
	{
		EndStep();
		UpdatePreviousRayCastWorldPos();
	}

	private void UpdateRayCastWorldPos()
	{
		if ((bool)raycastTransform)
		{
			raycastPosWorld = raycastTransform.TransformPoint(raycastPosLocal);
		}
	}

	private void UpdatePreviousRayCastWorldPos()
	{
		if ((bool)previousRaycastTransform)
		{
			previousRaycastPosWorld = previousRaycastTransform.TransformPoint(previousRaycastPosLocal);
		}
	}

	private void UpdateMiscVariables()
	{
		scale = data.transform.localScale.x;
		restPosition = (Vector2)data.transform.position + startOffset * 0.7f * scale;
	}

	private void SetValuesFixed()
	{
		if ((bool)moveDeltaTransform)
		{
			deltaPos = Vector3.Lerp(deltaPos, (Vector2)moveDeltaTransform.position - lastPos, TimeHandler.deltaTime * 15f);
			lastPos = moveDeltaTransform.position;
			deltaPos.y = 0f;
		}
	}
}