Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. extends Navigation
  2.  
  3. class Request:
  4. var start = Vector3()
  5. var end = Vector3()
  6. var agent = null
  7.  
  8. func _init(start,end,agent):
  9. self.start = start
  10. self.end = end
  11. self.agent = agent
  12.  
  13. class Result:
  14. var path = []
  15. var agent = null
  16.  
  17. func _init(path, agent):
  18. self.path = path
  19. self.agent = agent
  20.  
  21.  
  22. var threads = []
  23. var mutex = Mutex.new()
  24.  
  25. func requestThread():
  26. if threads.size() == 0:
  27. threads.append(Thread.new())
  28. print("Threads : ",threads.size())
  29. for thread in threads:
  30. if !thread.is_active():
  31. return thread
  32.  
  33. var newThread = Thread.new()
  34. threads.append(newThread)
  35. return newThread
  36.  
  37.  
  38. func onRequestPath(agent,start,end):
  39. var request = Request.new(start,end,agent)
  40.  
  41. var thread = requestThread()
  42. #!!
  43. mutex.lock()
  44.  
  45. thread.start(self,"loadPath",request)
  46.  
  47. func loadPath(request):
  48.  
  49. var path = get_simple_path(request.start,request.end,true)
  50.  
  51. call_deferred("loadDone")
  52.  
  53. var r = Result.new(path,request.agent)
  54.  
  55. return r
  56.  
  57. func loadDone():
  58. #!!
  59. mutex.unlock()
  60.  
  61. #Does this makes any sense ?
  62. for thread in threads:
  63. if thread.is_active():
  64. var result = thread.wait_to_finish()
  65. result.agent.setPath(result.path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement