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

public class EnemyAI : MonoBehaviour
{
	private InputHandler input;

	public Transform target;

	public Transform rotationTarget;

	private Transform hip;

	public Vector3 randomOffset;

	private PlayerDeath death;

	private PlayerDeath ownDeath;

	private float counter;

	private float counter2 = 1f;

	private float deadCounter;

	private bool hasDespawned;

	private void Start()
	{
		input = GetComponent<InputHandler>();
		hip = GetComponentInChildren<Hip>().transform;
		death = target.root.GetComponent<PlayerDeath>();
		ownDeath = GetComponent<PlayerDeath>();
	}

	private void Update()
	{
		if (ownDeath.dead)
		{
			deadCounter += Time.deltaTime;
			if (deadCounter > 5f && !hasDespawned)
			{
				Despawn();
			}
			if (deadCounter > 8f)
			{
				Object.Destroy(base.gameObject);
			}
		}
		else
		{
			Logic();
		}
	}

	public bool IsDead()
	{
		return ownDeath.dead;
	}

	private void Despawn()
	{
		hasDespawned = true;
		Rigidbody[] componentsInChildren = GetComponentsInChildren<Rigidbody>();
		Collider[] componentsInChildren2 = GetComponentsInChildren<Collider>();
		MonoBehaviour[] componentsInChildren3 = GetComponentsInChildren<MonoBehaviour>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].drag = 5f;
			componentsInChildren[i].angularDrag = 5f;
		}
		for (int j = 0; j < componentsInChildren2.Length; j++)
		{
			componentsInChildren2[j].enabled = false;
		}
		for (int k = 0; k < componentsInChildren3.Length; k++)
		{
			if (componentsInChildren3[k].GetType() != GetType())
			{
				componentsInChildren3[k].enabled = false;
			}
		}
	}

	private void Logic()
	{
		counter2 -= Time.deltaTime;
		if (counter2 < 0f)
		{
			counter2 = Random.Range(1f, 4f);
			randomOffset = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f));
		}
		float num = Vector3.Distance(target.position, hip.position);
		input.inputMovementDirection = (target.position + randomOffset * num * 0.7f - hip.position).normalized;
		rotationTarget.rotation = Quaternion.LookRotation(input.inputMovementDirection);
		input.isSpringting = true;
		counter += Time.deltaTime;
		if (num < 0.7f && counter > 0.3f)
		{
			death.TakeDamage(15f * (target.position - hip.position).normalized, Vector3.zero);
			counter = 0f;
		}
	}
}