Created
January 29, 2012 11:23
-
-
Save anonymous/1698361 to your computer and use it in GitHub Desktop.
Pasted form Komodo IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment