forked from mdotstrange/CustomBehaviorDesignerTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateWaypoints.cs
268 lines (222 loc) · 7.9 KB
/
generateWaypoints.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
using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine.AI;
using BehaviorDesigner.Runtime.Tasks.Movement;
using System.Collections.Generic;
using System.Collections;
//Task by M dot Strange
public class generateWaypoints : Action
{
public string waypointTargetTag;
public SharedInt desiredPointCount;
public SharedFloat minDistance;
public SharedFloat maxDistance;
public SharedBool useAgentYPosition;
public SharedGameObjectList wayPoints;
public SharedBool useNavMeshEdges;
GameObject objToSpawn;
GameObject wpParent;
int retries;
int remainderWaypointCount;
List<Collider> colliderList = new List<Collider>();
int mainIndex;
NavMeshHit hitto;
NavMeshHit hitto1;
bool goodPointFound;
public override TaskStatus OnUpdate()
{
GetInterestingObjects();
if (AllPointsDone() == true)
{
ParentWayPoints();
return TaskStatus.Success;
} else
{
remainderWaypointCount = GetRemainderPointCount();
for (mainIndex = 0; mainIndex < remainderWaypointCount; mainIndex++)
{
var posTotest = MakeRandoSpherePos();
while (SamplePosition(posTotest) != true)
{
posTotest = MakeRandoSpherePos();
}
}
if (useAgentYPosition.Value == true)
{
LevelWayPointYPosition();
}
ParentWayPoints();
return TaskStatus.Success;
}
}
public void GetInterestingObjects()
{
if (waypointTargetTag != null && waypointTargetTag != "")
{
var colliderArray = Physics.OverlapSphere(transform.position, maxDistance.Value);
if (colliderArray.Length != 0)
{
colliderList = new List<Collider>(colliderArray);
for (int index = 0; index < colliderList.Count; index++)
{
var i = colliderList[index];
if (i.gameObject.CompareTag(waypointTargetTag) && wayPoints.Value.Contains(i.gameObject) != true)
{
var waypoint = MakeWayPoint(i.transform.position);
AddWayPointToList(waypoint);
}
}
}
}
}
public bool AllPointsDone()
{
if (wayPoints.Value.Count == desiredPointCount.Value)
{
return true;
} else
{
return false;
}
}
public int GetRemainderPointCount()
{
return desiredPointCount.Value - wayPoints.Value.Count;
}
public void LevelWayPointYPosition()
{
for (int index1 = 0; index1 < wayPoints.Value.Count; index1++)
{
var i = wayPoints.Value[index1];
if (i.transform.position.y != transform.position.y)
{
i.transform.position = new Vector3(i.transform.position.x, transform.position.y, i.transform.position.z);
}
}
}
public void AddWayPointToList(GameObject obj)
{
wayPoints.Value.Add(obj);
}
public bool IsPositionTooClose(Vector3 pos)
{
if (wayPoints.Value.Count == 0)
{
return false;
} else
{
for (int index = 0; index < wayPoints.Value.Count; index++)
{
var i = wayPoints.Value[index].transform.position;
Vector3 offset = i - pos;
float sqrLen = offset.sqrMagnitude;
if (sqrLen < (maxDistance.Value * 0.5f) * (maxDistance.Value * 0.5f))
{
return true;
}
}
return false;
}
}
public bool SamplePosition(Vector3 pos)
{
retries = 0;
var posTotest = pos;
if (useNavMeshEdges.Value == false)
{
while (NavMesh.SamplePosition(posTotest, out hitto, maxDistance.Value, NavMesh.AllAreas) != true || IsPositionTooClose(hitto.position) == true || retries < 50)
{
retries++;
posTotest = MakeRandoSpherePos();
}
var waypoint = MakeWayPoint(hitto.position);
AddWayPointToList(waypoint);
if (retries < 50)
{
return false;
} else
{
return true;
}
} else
{
goodPointFound = false;
while (retries < 100 && goodPointFound == false)
{
retries++;
posTotest = MakeRandoSpherePos();
var sampleHit = NavMesh.SamplePosition(posTotest, out hitto, maxDistance.Value, NavMesh.AllAreas);
var edgeHit = NavMesh.FindClosestEdge(posTotest, out hitto1, NavMesh.AllAreas);
if (sampleHit == true)
{
if (IsPositionTooClose(hitto.position) == false)
{
var waypoint = MakeWayPoint(hitto.position);
AddWayPointToList(waypoint);
goodPointFound = true;
return true;
}
} else if (edgeHit == true)
{
if (IsPositionTooClose(hitto1.position) == false)
{
var waypoint = MakeWayPoint(hitto1.position);
AddWayPointToList(waypoint);
goodPointFound = true;
return true;
}
}
}
Debug.Log("FINISHED");
if (retries < 100)
{
return false;
} else
{
return true;
}
}
}
public Vector3 MakeRandoSpherePos()
{
if (useAgentYPosition.Value == false)
{
var direction = transform.forward;
var destination = transform.position;
var randoSphere = Random.insideUnitSphere * maxDistance.Value;
randoSphere = new Vector3(randoSphere.x, randoSphere.y, randoSphere.z);
direction = direction + randoSphere * maxDistance.Value;
destination = transform.position + direction.normalized * Random.Range(minDistance.Value, maxDistance.Value);
return new Vector3(destination.x, randoSphere.y, destination.z);
} else
{
var agentYPosition = transform.position.y;
var direction = transform.forward;
var destination = transform.position;
var randoSphere = Random.insideUnitSphere * maxDistance.Value;
randoSphere = new Vector3(randoSphere.x, agentYPosition, randoSphere.z);
direction = direction + randoSphere * maxDistance.Value;
destination = transform.position + direction.normalized * Random.Range(minDistance.Value, maxDistance.Value);
return new Vector3(destination.x, agentYPosition, destination.z);
}
}
public GameObject MakeWayPoint(Vector3 pos)
{
objToSpawn = new GameObject();
objToSpawn.name = "WayPoint " + mainIndex.ToString();
objToSpawn.transform.position = pos;
return objToSpawn;
}
public void ParentWayPoints()
{
wpParent = new GameObject();
wpParent.name = gameObject.name + "_Waypoints";
wpParent.transform.position = transform.position;
for (int index = 0; index < wayPoints.Value.Count; index++)
{
var i = wayPoints.Value[index].transform;
i.SetParent(wpParent.transform);
}
}
}