-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIAgent.cs
189 lines (137 loc) · 4.56 KB
/
AIAgent.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
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(SphereCollider))]
public class AIAgent : Agent, IDamageable
{
#region Variables
protected SphereCollider m_trigger;
protected HashSet<Agent> m_agentTrespassers = new HashSet<Agent>();
protected GameObject m_target = null;
[SerializeField] private float m_rotationSpeed = 10f;
private LayerMask m_ignoreLayer;
#endregion
#region MonoBehaviour
protected new void Awake()
{
base.Awake();
m_currentHealth = m_maxHealth;
m_trigger = GetComponent<SphereCollider>();
m_trigger.enabled = false;
m_ignoreLayer = ~LayerMask.GetMask("Entity");
}
protected new void Start()
{
base.Start();
m_trigger.enabled = true;
}
private void OnTriggerEnter(Collider other)
{
if (!other.isTrigger &&other.gameObject.TryGetComponent(out Agent agent) && agent.AgentTeam != m_agentTeam)
{
OnAgentEnter(agent);
}
}
private void OnTriggerExit(Collider other)
{
if (!other.isTrigger && other.gameObject.TryGetComponent(out Agent agent) && agent.AgentTeam != m_agentTeam)
{
OnAgentExit(agent);
}
}
protected new void Update()
{
base.Update();
if (m_target && !m_target.gameObject.activeInHierarchy)
{
if(m_target.TryGetComponent(out Agent agent))
{
OnAgentExit(agent);
}
m_target = null;
}
}
#endregion
#region Functions
public virtual void ShootAtTarget()
{
if (m_target != null)
{
// If the target is an agent and is not in sight skip.
if (m_target.TryGetComponent(out Agent agent) && !CanSeeTarget(m_target)) return;
ShootAt(m_target);
}
else if (m_agressor != null && CanSeeTarget(m_agressor.gameObject) && m_agressor.gameObject.activeInHierarchy)
{
ShootAt(m_agressor.gameObject);
}
}
public void ShootAt(GameObject target)
{
if (!target) return;
// Look at target position
Vector3 desiredForward = target.transform.position - transform.position;
desiredForward = Vector3.Normalize(new Vector3(desiredForward.x, 0f, desiredForward.z));
transform.forward = Vector3.Slerp(transform.forward, desiredForward, Time.deltaTime * m_rotationSpeed);
// Can shoot ?
if (Vector3.SqrMagnitude(transform.forward - desiredForward) < 0.1f && Time.time >= m_nextShootDate)
{
m_nextShootDate = Time.time + m_shootFrequency;
ShootForward();
}
}
protected override void OnHealthChange()
{
base.OnHealthChange();
}
protected override void OnDeath()
{
base.OnDeath();
gameObject.SetActive(false);
}
public void SetTarget(GameObject obj)
{
m_target = obj;
}
public virtual void OnAgentEnter(Agent agent)
{
m_agentTrespassers.Add(agent);
}
public virtual void OnAgentExit(Agent agent)
{
if (m_target == agent.gameObject) m_target = null;
m_agentTrespassers.Remove(agent);
}
protected override Vector3 GetBulletTrajectory()
{
return m_target == null ?
transform.forward :
Vector3.Normalize(new Vector3(m_target.transform.position.x - m_gunTransform.position.x, 0f, m_target.transform.position.z - m_gunTransform.position.z));
}
protected void CheckTarget()
{
float minDistance = float.MaxValue;
Agent newTarget = null;
foreach(Agent agent in m_agentTrespassers)
{
float distance = Vector3.SqrMagnitude(transform.position - agent.transform.position);
if (!CanSeeTarget(agent.gameObject)) continue;
if (distance < minDistance)
{
minDistance = distance;
newTarget = agent;
}
}
SetTarget(newTarget != null ? newTarget.gameObject : null);
}
// Consideration functions
public virtual float HasTarget()
{
return System.Convert.ToSingle(m_target != null || (m_agressor != null && m_agressor.gameObject.activeInHierarchy));
}
public bool CanSeeTarget(GameObject target)
{
Vector3 dir = target.transform.position - transform.position;
return !Physics.Raycast(transform.position, dir.normalized, dir.magnitude, m_ignoreLayer, QueryTriggerInteraction.Ignore);
}
#endregion
}