iTz_WyTe

PathRequestManager

Mar 18th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System;
  6.  
  7. public class PathRequestManager : MonoBehaviour {
  8.  
  9. Queue<PathRequest> pathRequestQueue = new Queue<PathRequest>();
  10. PathRequest currentPathRequest;
  11.  
  12. static PathRequestManager pathRequestInstance;
  13. Pathfinding pathfinding;
  14.  
  15. bool isProcessingPath;
  16.  
  17. void Awake() {
  18. pathRequestInstance = this;
  19. pathfinding = GetComponent<Pathfinding>();
  20. }
  21.  
  22. public static void RequestPath(Vector3 pathStart, Vector3 pathEnd, Action<Vector3[], bool> callback) {
  23. PathRequest newRequest = new PathRequest(pathStart,pathEnd,callback);
  24. pathRequestInstance.pathRequestQueue.Enqueue(newRequest);
  25. pathRequestInstance.TryProcessNext();
  26. }
  27.  
  28. void TryProcessNext() {
  29. if (!isProcessingPath && pathRequestQueue.Count > 0) {
  30. currentPathRequest = pathRequestQueue.Dequeue();
  31. isProcessingPath = true;
  32. pathfinding.StartFindPath(currentPathRequest.pathStart, currentPathRequest.pathEnd);
  33. }
  34. }
  35.  
  36. public void FinishedProcessingPath(Vector3[] path, bool success) {
  37. currentPathRequest.callback (path, success);
  38. isProcessingPath = false;
  39. TryProcessNext ();
  40. }
  41.  
  42. struct PathRequest {
  43. public Vector3 pathStart;
  44. public Vector3 pathEnd;
  45. public Action<Vector3[], bool> callback;
  46.  
  47. public PathRequest(Vector3 _start, Vector3 _end, Action<Vector3[], bool> _callback) {
  48. pathStart = _start;
  49. pathEnd = _end;
  50. callback = _callback;
  51. }
  52. }
  53.  
  54. void UpdatePaths() {
  55. if (Grid.updatePath == true) {
  56. Unit[] unitArray = FindObjectsOfType (typeof(Unit)) as Unit[];
  57. foreach (Unit unit in unitArray) {
  58. unit.UpdatePath ();
  59. }
  60. Grid.updatePath = false;
  61. }
  62. }
  63.  
  64. void Update() {
  65. UpdatePaths ();
  66. }
  67. }
Add Comment
Please, Sign In to add comment