-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkCreatureController.cs
346 lines (285 loc) · 11.1 KB
/
NetworkCreatureController.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using BEngine;
namespace FNetwork.Server
{
[RequireComponent(typeof(Seeker))]
public class NetworkCreatureController : MonoBehaviour
{
public GenericCreature Creature { private set; get; }
/** Determines how often it will search for new paths.
* If you have fast moving targets or AIs, you might want to set it to a lower value.
* The value is in seconds between path requests.
*/
public float repathRate = 0.5F;
/** Target to move towards.
* The AI will try to follow/move towards this target.
* It can be a point on the ground where the player has clicked in an RTS for example, or it can be the player object in a zombie game.
*/
public Transform target;
/** Enables or disables searching for paths.
* Setting this to false does not stop any active path requests from being calculated or stop it from continuing to follow the current path.
* \see #canMove
*/
public bool canSearch = true;
/** Enables or disables movement.
* \see #canSearch */
public bool canMove = true;
/** Maximum velocity.
* This is the maximum speed in world units per second.
*/
public float speed = 5;
/** Rotation speed.
* Rotation is calculated using Quaternion.SLerp. This variable represents the damping, the higher, the faster it will be able to rotate.
*/
public float turningSpeed = 10;
/** Determines within what range it will switch to target the next waypoint in the path */
public float pickNextWaypointDist = 4;
/** Distance to the end point to consider the end of path to be reached.
* When this has been reached, the AI will not move anymore until the target changes and OnTargetReached will be called.
*/
public float endReachedDistance = 2F;
/** Cached Seeker component */
protected Seeker seeker;
/** Cached Transform component */
protected Transform tr;
/** Time when the last path request was sent */
protected float lastRepath = -9999;
/** Current path which is followed */
protected Path path;
/** Cached CharacterController component */
protected CharacterController controller;
/** Current index in the path which is current target */
protected int currentWaypointIndex = 0;
/** Holds if the end-of-path is reached
* \see TargetReached */
protected bool targetReached = false;
/** Only when the previous path has been returned should be search for a new path */
protected bool canSearchAgain = true;
protected Vector3 lastFoundWaypointPosition;
protected float lastFoundWaypointTime = -9999;
/** Point to where the AI is heading.
* Filled in by #CalculateVelocity */
protected Vector3 targetPoint;
/** Relative direction to where the AI is heading.
* Filled in by #CalculateVelocity */
protected Vector3 targetDirection;
public float gravity = 9.82F;
/** Returns if the end-of-path has been reached
* \see targetReached */
public bool TargetReached
{
get
{
return targetReached;
}
}
/** Holds if the Start function has been run.
* Used to test if coroutines should be started in OnEnable to prevent calculating paths
* in the awake stage (or rather before start on frame 0).
*/
private bool startHasRun = false;
/** Initializes reference variables.
* If you override this function you should in most cases call base.Awake () at the start of it.
* */
protected virtual void Awake()
{
seeker = GetComponent<Seeker>();
tr = transform;
controller = GetComponent<CharacterController>();
}
// Use this for initialization
void Start()
{
startHasRun = true;
OnEnable();
}
public void setCreature(GenericCreature creature)
{
Creature = creature;
}
public virtual void OnTargetReached()
{
//End of path has been reached
//If you want custom logic for when the AI has reached it's destination
//add it here
//You can also create a new script which inherits from this one
//and override the function in that script
}
/** Run at start and when reenabled.
* Starts RepeatTrySearchPath.
*
* \see Start
*/
protected virtual void OnEnable()
{
lastRepath = -9999;
canSearchAgain = true;
lastFoundWaypointPosition = GetFeetPosition();
if (startHasRun)
{
//Make sure we receive callbacks when paths complete
seeker.pathCallback += OnPathComplete;
StartCoroutine(RepeatTrySearchPath());
}
}
public void OnDisable()
{
// Abort calculation of path
if (seeker != null && !seeker.IsDone()) seeker.GetCurrentPath().Error();
// Release current path
if (path != null) path.Release(this);
path = null;
//Make sure we receive callbacks when paths complete
seeker.pathCallback -= OnPathComplete;
}
/** Tries to search for a path every #repathRate seconds.
* \see TrySearchPath
*/
protected IEnumerator RepeatTrySearchPath()
{
while (true)
{
float v = TrySearchPath();
yield return new WaitForSeconds(v);
}
}
/** Tries to search for a path.
* Will search for a new path if there was a sufficient time since the last repath and both
* #canSearchAgain and #canSearch are true and there is a target.
*
* \returns The time to wait until calling this function again (based on #repathRate)
*/
public float TrySearchPath()
{
if (Time.time - lastRepath >= repathRate && canSearchAgain && canSearch && target != null)
{
SearchPath();
return repathRate;
}
else {
//StartCoroutine (WaitForRepath ());
float v = repathRate - (Time.time - lastRepath);
return v < 0 ? 0 : v;
}
}
/** Requests a path to the target */
public virtual void SearchPath()
{
if (target == null) throw new System.InvalidOperationException("Target is null");
lastRepath = Time.time;
//This is where we should search to
Vector3 targetPosition = target.position;
canSearchAgain = false;
//Alternative way of requesting the path
//ABPath p = ABPath.Construct (GetFeetPosition(),targetPoint,null);
//seeker.StartPath (p);
//We should search from the current position
seeker.StartPath(GetFeetPosition(), targetPosition);
}
/** Called when a requested path has finished calculation.
* A path is first requested by #SearchPath, it is then calculated, probably in the same or the next frame.
* Finally it is returned to the seeker which forwards it to this function.\n
*/
public virtual void OnPathComplete(Path _p)
{
ABPath p = _p as ABPath;
if (p == null) throw new System.Exception("This function only handles ABPaths, do not use special path types");
canSearchAgain = true;
//Claim the new path
p.Claim(this);
// Path couldn't be calculated of some reason.
// More info in p.errorLog (debug string)
if (p.error)
{
Debug.LogError(p.errorLog);
p.Release(this);
return;
}
//Release the previous path
if (path != null) path.Release(this);
//Replace the old path
path = p;
//Reset some variables
currentWaypointIndex = 1;
targetReached = false;
}
/** Rotates in the specified direction.
* Rotates around the Y-axis.
* \see turningSpeed
*/
protected virtual void RotateTowards(Vector3 dir)
{
if (dir == Vector3.zero) return;
Quaternion rot = tr.rotation;
Quaternion toTarget = Quaternion.LookRotation(dir);
rot = Quaternion.Slerp(rot, toTarget, turningSpeed * Time.deltaTime);
Vector3 euler = rot.eulerAngles;
euler.z = 0;
euler.x = 0;
rot = Quaternion.Euler(euler);
tr.rotation = rot;
}
public virtual Vector3 GetFeetPosition()
{
return tr.position - Vector3.up * controller.height * 0.5F;
}
// Update is called once per frame
void Update()
{
if (!canMove) { return; }
if (path == null || path.vectorPath == null || path.vectorPath.Count == 0)
{
return;
}
List<Vector3> vPath = path.vectorPath;
if(currentWaypointIndex < vPath.Count)
{
targetPoint = vPath[currentWaypointIndex];
}
targetDirection = targetPoint - tr.position;
targetDirection.y = 0;
RotateTowards(targetDirection);
float relativeDistance = speed * Time.deltaTime;
if (currentWaypointIndex == (vPath.Count -1))
{
float targetDistance = Vector3.Distance(tr.position, targetPoint);
if (targetDistance > endReachedDistance)
{
if( (targetDistance - endReachedDistance) < relativeDistance)
{
relativeDistance = targetDistance - endReachedDistance;
}
}
else
{
relativeDistance = 0;
}
}
if(relativeDistance == 0)
{
targetReached = true;
}
Vector3 Movement = targetDirection.normalized * relativeDistance;
Movement.y -= gravity * Time.deltaTime;
controller.Move(Movement);
if (Creature != null)
{
Creature.setTargetPosition(targetPoint);
Creature.setPosition(tr.position);
}
if (currentWaypointIndex < (vPath.Count -1) && Vector3.Distance(tr.position, targetPoint) < pickNextWaypointDist)
{
currentWaypointIndex++;
return;
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.magenta;
Gizmos.DrawSphere(targetPoint, 1f);
}
}
}