-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotMedic.cs
200 lines (177 loc) · 5.91 KB
/
BotMedic.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.AI;
using System.Linq;
public class BotMedic : NetworkBehaviour {
private BotMaster _botMaster;
private NavMeshAgent _navMeshAgent;
private BotNavPause _enemyNavPause;
private Animator _animator;
private float _nextAttack;
private float _attackRate = 0.2f;
private float _attackRange = 20; //Same as detection radius
public bool _isFiring = false;
private bool _shouldSwitchEnemy = false;
private List<KeyValuePair<Transform, float>> _seenTargets = new List<KeyValuePair<Transform, float>>();
public int _currentAllyIndex = 0;
private Gun _gun;
private Gun Gun
{
get
{
if (_gun == null)
{
_gun = GetComponentInChildren<Gun>();
}
return _gun;
}
}
private void Start()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
_navMeshAgent.updateRotation = false;
}
void OnEnable()
{
SetInitialReferences();
_botMaster.EventInjuredAllyFoundTarget += FindAttackTarget;
_botMaster.EventInjuredAllyRecovered += LoseAttackTarget;
_botMaster.EventEnemyDie += ResetData;
}
void OnDisable()
{
_botMaster.EventInjuredAllyFoundTarget -= FindAttackTarget;
_botMaster.EventInjuredAllyRecovered -= LoseAttackTarget;
_botMaster.EventEnemyDie -= ResetData;
}
void ResetData()
{
_isFiring = false;
}
void SetInitialReferences()
{
_animator = GetComponent<Animator>();
_botMaster = GetComponent<BotMaster>();
_enemyNavPause = GetComponent<BotNavPause>();
}
[ServerCallback]
private void Update()
{
TryToHeal();
}
void FindAttackTarget(Transform targetTransform)
{
if (_seenTargets.Count == 0)
{
_shouldSwitchEnemy = true;
}
if (!_seenTargets.Any(x => x.Key == targetTransform.root))
{
_seenTargets.Add(new KeyValuePair<Transform, float>(targetTransform.root, Time.time));
}
}
void LoseAttackTarget(Transform targetTransform)
{
if (Gun != null)
{
Gun.transform.localRotation = Quaternion.identity;
}
if (_currentAllyIndex < _seenTargets.Count && _seenTargets[_currentAllyIndex].Key == targetTransform.root)
{
_shouldSwitchEnemy = true;
}
_seenTargets.RemoveAll(x => x.Key == targetTransform.root);
if (_seenTargets.Count == 0)
{
_isFiring = false;
_botMaster.CallEventEnemyAttack(false);
}
}
void TryToHeal()
{
Transform targetTransform = null;
if (_seenTargets.Count > 0)
{
if (_shouldSwitchEnemy)
{
_shouldSwitchEnemy = false;
_currentAllyIndex = Random.Range(0, _seenTargets.Count);
}
if (_currentAllyIndex < _seenTargets.Count)
{
_nextAttack = Time.time + _attackRate;
if (Vector3.Distance(transform.position, _seenTargets.ElementAt(_currentAllyIndex).Key.position) <= _attackRange)
{
_isFiring = true;
Vector3 lookAtVector = new Vector3(_seenTargets.ElementAt(_currentAllyIndex).Key.position.x, transform.position.y, _seenTargets.ElementAt(_currentAllyIndex).Key.position.z);
targetTransform = _seenTargets.ElementAt(_currentAllyIndex).Key;
_botMaster.CallEventEnemyAttack(true);
}
else if (_isFiring && _seenTargets.Count > 0)
{
_botMaster.CallEventEnemyLostTarget(_seenTargets.ElementAt(_currentAllyIndex).Key);
}
}
}
if (targetTransform == null)
{
LookAtTarget(transform, null);
AimOnTarget(null);
}
else
{
LookAtTarget(transform, targetTransform.position);
AimOnTarget(targetTransform);
}
}
private void LookAtTarget(Transform theTransform, Vector3? theTarget)
{
Vector3 targetPoint = Vector3.zero;
if (theTarget == null) // no target
{
var targetPosition = _navMeshAgent.pathEndPosition;
targetPoint = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
}
else
{
targetPoint = new Vector3(theTarget.Value.x, transform.position.y, theTarget.Value.z);
}
var _direction = (targetPoint - transform.position).normalized;
if (_direction != Vector3.zero)
{
var _lookRotation = Quaternion.LookRotation(_direction);
theTransform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, GameConstants.BotRotationFactor * Time.deltaTime);
}
}
[ServerCallback]
private void LateUpdate()
{
_seenTargets.RemoveAll(x => Time.time - x.Value > 20);
_seenTargets.RemoveAll(x => x.Key.GetComponent<Health>()._currentHealth == x.Key.GetComponent<Health>()._maxHealth);
if (_seenTargets.Count == 0 && _isFiring)
{
_isFiring = false;
_botMaster.CallEventEnemyAttack(false);
}
}
private void AimOnTarget(Transform target)
{
if (Gun)
{
if (target == null)
{
LookAtTarget(Gun.transform, null);
}
else if (target.gameObject.layer == GameConstants.ObjectiveLayer)
{
LookAtTarget(Gun.transform, target.position);
}
else
{
LookAtTarget(Gun.transform, target.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Chest).position);
}
}
}
}