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

public class PlayerDeath : MonoBehaviour
{
	public bool dead;

	private bool isFrozen;

	public float muscleFunction = 1f;

	public bool terminalState;

	private DamageEffects damageEffects;

	private RagdollHandler ragdoll;

	private Transform hip;

	public float health = 100f;

	public ParticleSystem[] damageParticles;

	private HasControl hasControll;

	private Holding holding;

	private void Start()
	{
		damageEffects = GetComponent<DamageEffects>();
		ragdoll = GetComponent<RagdollHandler>();
		hip = GetComponentInChildren<Hip>().transform;
		hasControll = GetComponent<HasControl>();
		holding = GetComponent<Holding>();
	}

	private void Update()
	{
		if (health < 100f)
		{
			health += Time.deltaTime * 10f;
			health = Mathf.Clamp(health, -10f, 100f);
		}
		if (hip.transform.position.y < -10f)
		{
			Die();
		}
		if (terminalState && muscleFunction > 0f)
		{
			muscleFunction -= Time.deltaTime * 1f;
		}
		if (muscleFunction < 0f && !isFrozen)
		{
			FreezeBody();
		}
	}

	public void TakeDamage(Vector3 damage, Vector3 hitPoint, Rigidbody hitRig = null)
	{
		if (hasControll.hasControl)
		{
			damageEffects.TakeDamage(damage, hitPoint);
		}
		if (hitPoint != Vector3.zero)
		{
			for (int i = 0; i < damageParticles.Length; i++)
			{
				damageParticles[i].transform.rotation = Quaternion.LookRotation(damage);
				damageParticles[i].transform.position = hitPoint;
				damageParticles[i].Play();
			}
		}
		health -= damage.magnitude;
		if (!(health <= 0f))
		{
			return;
		}
		if ((bool)hitRig)
		{
			ConfigurableJoint component = hitRig.GetComponent<ConfigurableJoint>();
			if ((bool)component)
			{
				Object.Destroy(component);
			}
		}
		Kill();
	}

	public void FreezeBody()
	{
		isFrozen = true;
		Joint[] componentsInChildren = GetComponentsInChildren<Joint>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			ConfigurableJoint configurableJoint = (ConfigurableJoint)componentsInChildren[i];
			Rigidbody component = configurableJoint.GetComponent<Rigidbody>();
			JointDrive angularXDrive = configurableJoint.angularXDrive;
			angularXDrive.positionSpring = 5f * component.mass;
			angularXDrive.positionDamper = 1f * component.mass;
			configurableJoint.angularXDrive = angularXDrive;
			configurableJoint.angularYZDrive = angularXDrive;
			configurableJoint.SetTargetRotationLocal(configurableJoint.transform.localRotation, configurableJoint.gameObject.GetComponent<StartRotation>().startRotationLocal);
		}
	}

	public void Die()
	{
		if (!dead)
		{
			holding.Drop();
			dead = true;
			ragdoll.ragdollValue = 0f;
			Collider[] componentsInChildren = GetComponentsInChildren<Collider>();
			foreach (Collider collider in componentsInChildren)
			{
				collider.material = null;
			}
		}
	}

	public void Kill()
	{
		terminalState = true;
		Die();
	}
}