Guest User

Untitled

a guest
Jul 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. from random import randint
  2. # get the nodes directly visible from a node
  3. def getVisibleNodes(player):
  4. vNodes = []
  5. nList = GameLogic.globalDict['nList']
  6. for n in nList:
  7. # ray cast the node we are checking
  8. next = player.rayCastTo(n,0,'node')
  9. # if it is directly visible, add it to the list
  10. if next not in vNodes and next == n:
  11. vNodes.append(n)
  12. return vNodes
  13.  
  14. def getPlayerNodes():
  15. uDict = {}
  16. scene = GameLogic.getCurrentScene()
  17. nList = GameLogic.globalDict['nList']
  18. # get all the nodes in the current scene
  19. for ob in scene.objects:
  20. # add objects to the list if they have the node property
  21. if ob.has_key('player'):
  22. player = ob
  23.  
  24. # create the dictionary
  25. vNodes = getVisibleNodes(player)
  26. # the node is the key and the list of visible nodes is the value
  27. uDict[player] = vNodes
  28. GameLogic.globalDict['uDict'] = uDict
  29.  
  30. cont = GameLogic.getCurrentController()
  31.  
  32. # actuators
  33. move = cont.actuators['move']
  34. track = cont.actuators['track']
  35.  
  36. # variables
  37. path = GameLogic.globalDict.get('path')
  38. speed = 0
  39. point = cont.owner['point']
  40. dir = cont.owner['dir']
  41. loop = cont.owner['loop']
  42.  
  43.  
  44. # check if we need to generate a ppath
  45. if cont.owner['get_path']:
  46. point = 1
  47. cont.owner['start'] = cont.owner['end']
  48. #find player location and go there
  49. getPlayerNodes()
  50. uNodeDic = GameLogic.globalDict.get('uDict')
  51. uNode = uNodeDic[uNodeDic.keys()[0]]
  52. print 'uNode: %s' % uNode
  53. if len(uNode) > 1:
  54. print ">1"
  55. while 1:
  56. cont.owner['end'] = str(uNode[randint(0, len(uNode))])
  57. #move on if start and end are diffrent
  58. if cont.owner['start'] != cont.owner['end']:
  59. break
  60.  
  61. elif len(uNode) == 1:
  62. print "1"
  63. cont.owner['end'] = str(uNode[0])
  64. if cont.owner['start'] == cont.owner['end']:
  65. #pick a random node from the map
  66. tempList = GameLogic.globalDict['nList']
  67. while 1:
  68. cont.owner['end'] = str(tempList[randint(0, len(tempList))])
  69. if cont.owner['start'] != cont.owner['end']:
  70. break
  71.  
  72. else:
  73. print "0"
  74. #pick a random node from the map
  75. tempList = GameLogic.globalDict['nList']
  76. while 1:
  77. cont.owner['end'] = str(tempList[randint(0, len(tempList))])
  78. if cont.owner['start'] != cont.owner['end']:
  79. break
  80.  
  81. print 'start: %s' % cont.owner['start']
  82. print 'end: %s' % cont.owner['end']
  83. print 'nList: %s' % GameLogic.globalDict['nList']
  84. import pathfinder
  85. #cont.owner['end'] = "OBNode.007"
  86. scene = GameLogic.getCurrentScene()
  87. # starting node
  88. start = scene.objects[cont.owner['start']]
  89. # ending node
  90. end = scene.objects[cont.owner['end']]
  91. # use the getPath function in pathfinder.py
  92. path = pathfinder.getPath(start, end)
  93. # save the path
  94. GameLogic.globalDict['path'] = path
  95.  
  96. cont.owner['get_path'] = 0
  97.  
  98.  
  99. # loop through the list
  100. if loop:
  101. # change dir at start and end of list
  102. if point+1 == len(path):
  103. dir = -1
  104. elif not point:
  105. dir = 1
  106.  
  107. # set next point if close to node, otherwise keep moving
  108. if cont.owner.getDistanceTo(track.object) <= 1.25:
  109. point += dir
  110. else:
  111. speed = .1
  112.  
  113. # track to the node
  114. track.object = path[point]
  115. cont.activate(track)
  116.  
  117. # update properites
  118. cont.owner['point'] = point
  119. cont.owner['dir'] = dir
  120.  
  121.  
  122. # stop at end of list
  123. else:
  124. # make sure we aren't at the end of the list
  125. if not point >= len(path):
  126. # track to the node
  127. track.object = path[point]
  128. cont.activate(track)
  129.  
  130. # set next point if close to node, otherwise keep moving
  131. if cont.owner.getDistanceTo(path[point]) <= 1.25:
  132. point += 1
  133. else:
  134. speed = .1
  135.  
  136. else:
  137. cont.owner['get_path'] = 1
  138.  
  139.  
  140. # update properites
  141. cont.owner['point'] = point
  142.  
  143.  
  144.  
  145.  
  146. # update movement
  147. move.setDLoc(0.0, speed, 0.0, 1)
  148. GameLogic.addActiveActuator(move,1)
Add Comment
Please, Sign In to add comment