Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- # get the nodes directly visible from a node
- def getVisibleNodes(player):
- vNodes = []
- nList = GameLogic.globalDict['nList']
- for n in nList:
- # ray cast the node we are checking
- next = player.rayCastTo(n,0,'node')
- # if it is directly visible, add it to the list
- if next not in vNodes and next == n:
- vNodes.append(n)
- return vNodes
- def getPlayerNodes():
- uDict = {}
- scene = GameLogic.getCurrentScene()
- nList = GameLogic.globalDict['nList']
- # get all the nodes in the current scene
- for ob in scene.objects:
- # add objects to the list if they have the node property
- if ob.has_key('player'):
- player = ob
- # create the dictionary
- vNodes = getVisibleNodes(player)
- # the node is the key and the list of visible nodes is the value
- uDict[player] = vNodes
- GameLogic.globalDict['uDict'] = uDict
- cont = GameLogic.getCurrentController()
- # actuators
- move = cont.actuators['move']
- track = cont.actuators['track']
- # variables
- path = GameLogic.globalDict.get('path')
- speed = 0
- point = cont.owner['point']
- dir = cont.owner['dir']
- loop = cont.owner['loop']
- # check if we need to generate a ppath
- if cont.owner['get_path']:
- point = 1
- cont.owner['start'] = cont.owner['end']
- #find player location and go there
- getPlayerNodes()
- uNodeDic = GameLogic.globalDict.get('uDict')
- uNode = uNodeDic[uNodeDic.keys()[0]]
- print 'uNode: %s' % uNode
- if len(uNode) > 1:
- print ">1"
- while 1:
- cont.owner['end'] = str(uNode[randint(0, len(uNode))])
- #move on if start and end are diffrent
- if cont.owner['start'] != cont.owner['end']:
- break
- elif len(uNode) == 1:
- print "1"
- cont.owner['end'] = str(uNode[0])
- if cont.owner['start'] == cont.owner['end']:
- #pick a random node from the map
- tempList = GameLogic.globalDict['nList']
- while 1:
- cont.owner['end'] = str(tempList[randint(0, len(tempList))])
- if cont.owner['start'] != cont.owner['end']:
- break
- else:
- print "0"
- #pick a random node from the map
- tempList = GameLogic.globalDict['nList']
- while 1:
- cont.owner['end'] = str(tempList[randint(0, len(tempList))])
- if cont.owner['start'] != cont.owner['end']:
- break
- print 'start: %s' % cont.owner['start']
- print 'end: %s' % cont.owner['end']
- print 'nList: %s' % GameLogic.globalDict['nList']
- import pathfinder
- #cont.owner['end'] = "OBNode.007"
- scene = GameLogic.getCurrentScene()
- # starting node
- start = scene.objects[cont.owner['start']]
- # ending node
- end = scene.objects[cont.owner['end']]
- # use the getPath function in pathfinder.py
- path = pathfinder.getPath(start, end)
- # save the path
- GameLogic.globalDict['path'] = path
- cont.owner['get_path'] = 0
- # loop through the list
- if loop:
- # change dir at start and end of list
- if point+1 == len(path):
- dir = -1
- elif not point:
- dir = 1
- # set next point if close to node, otherwise keep moving
- if cont.owner.getDistanceTo(track.object) <= 1.25:
- point += dir
- else:
- speed = .1
- # track to the node
- track.object = path[point]
- cont.activate(track)
- # update properites
- cont.owner['point'] = point
- cont.owner['dir'] = dir
- # stop at end of list
- else:
- # make sure we aren't at the end of the list
- if not point >= len(path):
- # track to the node
- track.object = path[point]
- cont.activate(track)
- # set next point if close to node, otherwise keep moving
- if cont.owner.getDistanceTo(path[point]) <= 1.25:
- point += 1
- else:
- speed = .1
- else:
- cont.owner['get_path'] = 1
- # update properites
- cont.owner['point'] = point
- # update movement
- move.setDLoc(0.0, speed, 0.0, 1)
- GameLogic.addActiveActuator(move,1)
Add Comment
Please, Sign In to add comment