Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System;
- public class PathRequestManager : MonoBehaviour {
- Queue<PathRequest> pathRequestQueue = new Queue<PathRequest>();
- PathRequest currentPathRequest;
- static PathRequestManager pathRequestInstance;
- Pathfinding pathfinding;
- bool isProcessingPath;
- void Awake() {
- pathRequestInstance = this;
- pathfinding = GetComponent<Pathfinding>();
- }
- public static void RequestPath(Vector3 pathStart, Vector3 pathEnd, Action<Vector3[], bool> callback) {
- PathRequest newRequest = new PathRequest(pathStart,pathEnd,callback);
- pathRequestInstance.pathRequestQueue.Enqueue(newRequest);
- pathRequestInstance.TryProcessNext();
- }
- void TryProcessNext() {
- if (!isProcessingPath && pathRequestQueue.Count > 0) {
- currentPathRequest = pathRequestQueue.Dequeue();
- isProcessingPath = true;
- pathfinding.StartFindPath(currentPathRequest.pathStart, currentPathRequest.pathEnd);
- }
- }
- public void FinishedProcessingPath(Vector3[] path, bool success) {
- currentPathRequest.callback (path, success);
- isProcessingPath = false;
- TryProcessNext ();
- }
- 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;
- }
- }
- void UpdatePaths() {
- if (Grid.updatePath == true) {
- Unit[] unitArray = FindObjectsOfType (typeof(Unit)) as Unit[];
- foreach (Unit unit in unitArray) {
- unit.UpdatePath ();
- }
- Grid.updatePath = false;
- }
- }
- void Update() {
- UpdatePaths ();
- }
- }
Add Comment
Please, Sign In to add comment