public
anonymous / nodemap.py
Created

Pasted form Komodo IDE

  • Download Gist
nodemap.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
def generate_nodes(self, game, state):
mask = game.map.collision_mask
mask_width = mask.get_size()[0]
mask_height = mask.get_size()[1]
 
onPlane = False
 
 
### SET THE NODES ###
 
for y in range(self.EXTRA_NODE_HEIGHT+35, mask_height):# 35 == approx character height
for x in range(mask_width):
 
if onPlane:
if x == 0:# We changed map row
nav_node = node.Node(game, state, (map_width, y-1-self.EXTRA_NODE_HEIGHT))
onPlane = False
 
if (not mask.get_at((x, y))) and mask.get_at((x, y+1)):# If this is a floor pixel
if not onPlane: # If it's the first pixel of a certain plane
node.Node(game, state, (x, y-self.EXTRA_NODE_HEIGHT))
onPlane = True
 
else:# We're off the ground, but we were a pixel ago. Also, we're not at the beginning of the screen.
if onPlane:
nav_node = node.Node(game, state, (x-1, y-self.EXTRA_NODE_HEIGHT))
onPlane = False
 
for nav_node in self.nodelist:
numof_neighbours = 0
for (x, y) in self.nodepositions:
if x == nav_node.x and y == nav_node.y: continue # We're dealing with the same node.
 
if math.hypot(nav_node.x-x, nav_node.y-y) <= self.MIN_NODE_DISTANCE:
numof_neighbours += 1
 
if numof_neighbours > 1:
index = self.nodelist.index(nav_node)
self.nodelist[index].to_destroy = True
# TODO: Remove nodes in spawn as soon as spawn is implemented.
 
 
# Actually destroy all the nodes who were marked to die
index = 0
# I hate doing this, but we have to compensate for removing items in the list
while index < len(self.nodelist)-1:
nav_node = self.nodelist[index]
if nav_node.to_destroy:
nav_node.destroy(state)
index -= 1
index += 1
 
 
### CONNECT THE NODES ###
 
# For each node, create a character object that moves in a certain direction, and see to what it connects.
walkerplayer = player.Player(game, state, len(state.players))
walkerchar = state.entities[walkerplayer.character_id]
 
for origin_node in self.nodelist:
for command in [1, -1, 2, -2]:# walk right, walk left, walk right and jump, walk left and jump. All 4 possible paths from a given node.
has_left_origin_node = False# Used to detect when we are stuck
time = 0
distance = 0
please_break = False
 
# Place the character on the ground, so that the node lies about at his heart.
walkerchar.x = origin_node.x - walkerchar.collision_mask.get_size()[0]/2
walkerchar.y = origin_node.y - (walkerchar.collision_mask.get_size()[1] - self.EXTRA_NODE_HEIGHT)
 
if not mask.get_at((origin_node.x+6, origin_node.y+self.EXTRA_NODE_HEIGHT)):
while mask.overlap(walkerchar.collision_mask, (int(walkerchar.x), int(walkerchar.y))):
walkerchar.x += 1
elif not mask.get_at((origin_node.x-6, origin_node.y+self.EXTRA_NODE_HEIGHT)):
while mask.overlap(walkerchar.collision_mask, (int(walkerchar.x), int(walkerchar.y))):
walkerchar.x -= 1
 
# Set the input of the character
walkerplayer.left = command>0
walkerplayer.right = command<0
walkerplayer.up = (abs(command)-1)>0
 
while True:
# Check collision with any nodes
for othernode in self.nodelist:
if othernode.collide_with_character(walkerchar):
# Is this the original node?
if othernode != origin_node:
# Yay, new path found. Create a connection
origin_node.add_connection(othernode, distance, command)
please_break = True# Exit both loops
break
 
if please_break:
break
 
# Make the character walk
walkerchar.step(game, state, (1/self.SIMULATION_FPS))
walkerchar.endstep(game, state, (1/self.SIMULATION_FPS))
 
if time > self.MAX_TIME_STUCK:
if origin_node.collide_with_character(walkerchar):
# We're stuck. Abort
break
 
walkerplayer.jump = 0 # In case jump was activated, we don't need it anymore.
 
time += 1/self.SIMULATION_FPS
distance += abs(walkerchar.hspeed)
print "~"+str(int((self.nodelist.index(origin_node)+1)/len(self.nodelist)*100))+"%"
 
walkerplayer.destroy(game, state)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.