summaryrefslogtreecommitdiff
path: root/MovementHandler.cs
blob: 22795f77f572c11601b513c408796c00d6b1951c (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
using System.Collections;
using UnityEngine;

public class MovementHandler : MonoBehaviour
{
	private InputHandler inputHandler;

	public float friction = 0.9f;

	public Vector3 movementVector;

	public float[] animationForceAmounts;

	private AnimationHandler animationHandler;

	private RigidbodyHolder allRigs;

	public AnimationCurve jumpCurve;

	public float jumpForce;

	private StandingDataHandler standingData;

	private MovementDataHandler data;

	private PlayerDeath death;

	[HideInInspector]
	public float multiplier = 1f;

	private WobbleShake wobbleShake;

	private void Start()
	{
		wobbleShake = GetComponentInChildren<WobbleShake>();
		death = GetComponent<PlayerDeath>();
		standingData = GetComponent<StandingDataHandler>();
		inputHandler = GetComponent<InputHandler>();
		animationHandler = GetComponent<AnimationHandler>();
		allRigs = GetComponent<RigidbodyHolder>();
		data = GetComponent<MovementDataHandler>();
	}

	private void FixedUpdate()
	{
		if (!death.dead)
		{
			data.sinceJump += Time.fixedDeltaTime;
			movementVector += inputHandler.inputMovementDirection * animationForceAmounts[animationHandler.animationState];
			movementVector *= friction;
			for (int i = 0; i < allRigs.GetAllRigs().Length; i++)
			{
				allRigs.GetAllRigs()[i].AddForce(movementVector * multiplier, ForceMode.Acceleration);
			}
		}
	}

	public void Jump()
	{
		if (!(data.sinceJump < 0.5f) && !(standingData.sinceGrounded > 0.3f))
		{
			data.sinceJump = 0f;
			StartCoroutine(AddJumpForce());
			wobbleShake.AddShake(Vector3.up * 2f, 0.8f);
		}
	}

	private IEnumerator AddJumpForce()
	{
		float counter = 0f;
		for (int i = 0; i < allRigs.GetAllRigs().Length; i++)
		{
			allRigs.GetAllRigs()[i].velocity = new Vector3(allRigs.GetAllRigs()[i].velocity.x, 0f, allRigs.GetAllRigs()[i].velocity.z);
		}
		while (counter < jumpCurve.keys[jumpCurve.length - 1].time && !death.dead)
		{
			counter += Time.deltaTime;
			for (int j = 0; j < allRigs.GetAllRigs().Length; j++)
			{
				allRigs.GetAllRigs()[j].AddForce(Vector3.up * multiplier * jumpForce * jumpCurve.Evaluate(counter) * Time.deltaTime, ForceMode.Acceleration);
			}
			yield return null;
		}
	}
}