summaryrefslogtreecommitdiff
path: root/CameraMovement.cs
blob: a94012a55dd0d051416abc799eee8a66c0592b15 (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
using UnityEngine;

public class CameraMovement : MonoBehaviour
{
	public Camera camera;

	public Transform positionTarget;

	public Transform headPosition;

	public Transform rotationTarget;

	public Transform bobberTarget;

	private StandingDataHandler standingData;

	private float fallingValue;

	private float fallingEffectValue;

	private float minBobble = 0.02f;

	public bool ADS;

	public DamageEffects effects;

	private WeaponHandler weaponHandler;

	private Rigidbody hip;

	private HasControl hasControl;

	private HeadCollisionHandler headCollisionHandler;

	private MovementDataHandler movementData;

	private Rigidbody torso;

	private PlayerDeath death;

	private Strength str;

	private Vector3 cameraCurrentRelativeADSPosition = Vector3.zero;

	private Vector3 movementADSVelocity = Vector3.zero;

	private Vector3 ADSMovementPosition = Vector3.zero;

	private void Start()
	{
		torso = base.transform.root.GetComponentInChildren<Torso>().GetComponent<Rigidbody>();
		headCollisionHandler = base.transform.root.GetComponentInChildren<HeadCollisionHandler>();
		standingData = base.transform.GetComponentInParent<StandingDataHandler>();
		movementData = base.transform.GetComponentInParent<MovementDataHandler>();
		weaponHandler = base.transform.GetComponentInParent<WeaponHandler>();
		camera = GetComponentInChildren<Camera>();
		hip = base.transform.root.GetComponentInChildren<Hip>().GetComponent<Rigidbody>();
		hasControl = base.transform.root.GetComponent<HasControl>();
		death = base.transform.root.GetComponent<PlayerDeath>();
		str = base.transform.root.GetComponent<Strength>();
		effects = base.transform.root.GetComponent<DamageEffects>();
	}

	private void LateUpdate()
	{
		if (hasControl.hasControl)
		{
			headCollisionHandler.collisionValue = 0f;
			if (standingData.sinceLanded > 1f)
			{
				fallingValue = Mathf.Clamp(standingData.sinceGrounded - 0.5f, 0f, 10f) * 0.5f;
			}
			if (standingData.sinceGrounded > 0.5f || standingData.sinceLanded < 0.5f)
			{
				fallingEffectValue = Mathf.Lerp(fallingEffectValue, fallingValue, Time.smoothDeltaTime * 15f);
			}
			else
			{
				fallingEffectValue = Mathf.Lerp(fallingEffectValue, minBobble, Time.smoothDeltaTime * 3f);
			}
			float physicsValue = GetPhysicsValue();
			Vector3 vector = Vector3.Lerp(positionTarget.position, bobberTarget.position, Mathf.Clamp(physicsValue + headCollisionHandler.collisionValue, 0f, 1f));
			Vector3 position = vector;
			position.y = base.transform.position.y;
			base.transform.position = position;
			base.transform.position = vector;
			base.transform.rotation = Quaternion.Lerp(rotationTarget.rotation, bobberTarget.rotation, Mathf.Clamp(physicsValue + Mathf.Clamp(headCollisionHandler.collisionValue, 0f, 0.4f), 0f, 1f));
			SetCameraPosition();
		}
	}

	private void SetCameraPosition()
	{
		if (ADS && (bool)weaponHandler.gunADS)
		{
			cameraCurrentRelativeADSPosition = Vector3.Lerp(cameraCurrentRelativeADSPosition, Vector3.zero, Time.deltaTime * 20f);
			camera.transform.position = weaponHandler.gunADS.TransformPoint(cameraCurrentRelativeADSPosition) + ADSMovementPosition;
			camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, weaponHandler.ADSFOV, Time.deltaTime * 20f);
			return;
		}
		if ((bool)weaponHandler && (bool)weaponHandler.gunADS)
		{
			cameraCurrentRelativeADSPosition = weaponHandler.gunADS.InverseTransformPoint(camera.transform.position);
		}
		camera.transform.localPosition = Vector3.Lerp(camera.transform.localPosition, Vector3.zero, Time.deltaTime * 20f);
		camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 90f, Time.deltaTime * 20f);
	}

	private void FixedUpdate()
	{
		if (hasControl.hasControl && (bool)weaponHandler && (bool)weaponHandler.rightGun)
		{
			movementADSVelocity += (weaponHandler.rightGun.rig.velocity - hip.velocity) * 0.01f;
			movementADSVelocity += -ADSMovementPosition * 3f;
			movementADSVelocity *= 0.8f;
		}
	}

	private void Update()
	{
		if (hasControl.hasControl)
		{
			ADSMovementPosition += movementADSVelocity * Time.deltaTime;
		}
	}

	private float GetPhysicsValue()
	{
		float result = fallingEffectValue * 0.3f + (1f - str.strength) + effects.damageValue;
		if (death.dead)
		{
			result = 1f;
		}
		return result;
	}
}