-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathPathRequestManager.cs
More file actions
73 lines (57 loc) · 1.5 KB
/
Copy pathPathRequestManager.cs
File metadata and controls
73 lines (57 loc) · 1.5 KB
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
public class PathRequestManager : MonoBehaviour {
Queue<PathResult> results = new Queue<PathResult>();
static PathRequestManager instance;
Pathfinding pathfinding;
void Awake() {
instance = this;
pathfinding = GetComponent<Pathfinding>();
}
void Update() {
if (results.Count > 0) {
int itemsInQueue = results.Count;
lock (results) {
for (int i = 0; i < itemsInQueue; i++) {
PathResult result = results.Dequeue ();
result.callback (result.path, result.success);
}
}
}
}
public static void RequestPath(PathRequest request) {
ThreadStart threadStart = delegate {
instance.pathfinding.FindPath (request, instance.FinishedProcessingPath);
};
new Thread(threadStart).Start();
}
public void FinishedProcessingPath(PathResult result) {
lock (results) {
results.Enqueue (result);
}
}
}
public struct PathResult {
public Vector3[] path;
public bool success;
public Action<Vector3[], bool> callback;
public PathResult (Vector3[] path, bool success, Action<Vector3[], bool> callback)
{
this.path = path;
this.success = success;
this.callback = callback;
}
}
public struct PathRequest {
public Vector3 pathStart;
public Vector3 pathEnd;
public Action<Vector3[], bool> callback;
public PathRequest(Vector3 _start, Vector3 _end, Action<Vector3[], bool> _callback) {
pathStart = _start;
pathEnd = _end;
callback = _callback;
}
}