Skip to content

Commit

Permalink
Pathfinding for the hero is quite perfect now!
Browse files Browse the repository at this point in the history
  • Loading branch information
keul committed Apr 24, 2009
1 parent c0b4d26 commit 44dad51
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cheeseboys.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def game():
# BBB: can I check this in the enemy update method?
if cblocals.global_controlsEnabled:
for enemy in enemies.sprites():
if enemy.physical_rect.collidepoint(pygame.mouse.get_pos()):
if enemy.physical_rect.collidepoint(pygame.mouse.get_pos()) and hero.can_see_list.get(enemy.UID(),False):
hero.seeking = enemy
utils.changeMouseCursor(cblocals.IMAGE_CURSOR_ATTACK_TYPE)
break
Expand Down
2 changes: 1 addition & 1 deletion cheeseboys/cblocals.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@
PATHFINDING_GRID_SIZE = (24,32) # as TILE_IMAGE_SIZE
SHOW_FPS = True

DEBUG = False
DEBUG = True

2 changes: 1 addition & 1 deletion cheeseboys/character/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def hasNoFreeMovementTo(self, target, source=None):
else:
# I need a collide_rect traslated to the new source
rx, ry = position_int[0]-int(source[0]), position_int[1]-int(source[1])
collide_rect = collide_rect.move(rx, ry)
collide_rect.move_ip(rx, ry)
v = Vector2.from_points(source, target)
magnitude = v.get_magnitude()
heading = v.normalize()
Expand Down
18 changes: 11 additions & 7 deletions cheeseboys/character/navpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,25 @@ def compute_path(self, target=None):
target = self.get()
if target:
target_tuple = target.as_tuple()
target_is_free_point = level.checkPointIsFree(target_tuple)
target_is_free_slot = level.isPointOnFreeSlot(target_tuple)
# Checking for a free slot to on the grid, to be the target of the pathfinding
free_near_slot = level.getFreeNearSlot(level.toGridCoord(target_tuple))
if free_near_slot:
target_tuple = level.fromGridCoord(free_near_slot)
if not level.checkPointIsFree(target_tuple) or not level.isPointOnFreeSlot(target_tuple):
# The character wanna move on a non free point, or onto a free point but in a non free gridmap slot: no path computed!
free_near_slot = None
if target_is_free_point and not target_is_free_slot:
free_near_slot = level.getFreeNearSlot(level.toGridCoord(target_tuple))
if not target_is_free_point or not target_is_free_slot and not free_near_slot:
# The character wanna move on a non-free point, or onto a free point but in a non free gridmap slot: no path computed!
self.computed_path = [target_tuple,]
return self.computed_path
fromGridCoord = level.fromGridCoord
goal = level.toGridCoord(target_tuple)
if free_near_slot:
goal = free_near_slot
else:
goal = level.toGridCoord(target_tuple)
temp_computed_path = [fromGridCoord(x) for x in character.pathfinder.compute_path(character.position_grid, goal)]
# For a better animation I like to cut the last pathfinding step before the real navPoint;
# this can lead to collision sometimes.
if len(temp_computed_path)>2 and character.hasNoFreeMovementTo(target_tuple, source=temp_computed_path[-2]):
# BBB: fixme!
self.computed_path = temp_computed_path[1:] + [target_tuple,]
else:
self.computed_path = temp_computed_path[1:-1] + [target_tuple,]
Expand Down
13 changes: 8 additions & 5 deletions cheeseboys/level/grid_map_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fromGridCoord(self, coord):
"""Transforms a grid coordinate to a level absolute ones"""
tile_size_x, tile_size_y = cblocals.PATHFINDING_GRID_SIZE
x, y = coord
return x*tile_size_x+tile_size_x/2, y*tile_size_y+tile_size_y-1
return int(x*tile_size_x+tile_size_x/2), int(y*tile_size_y+tile_size_y/2)

def drawGridMapSquares(self, surface):
"""Draw on a surface the gridmap areas, for debug purposes"""
Expand All @@ -59,16 +59,19 @@ def getFreeNearSlot(self, point):
@return: The free slot coord, or None if no free slot is found.
"""
grid_map = self.grid_map
if not grid_map.isBlocked(point):
return point
try:
if not grid_map.isBlocked(point):
return point
except IndexError:
blocked = True
px, py = point
for y,x in ( (-1,0), (0,1), (1,0), (0,-1),):
try:
blocked = grid_map.isBlocked( (px+x,py+y) )
except IndexError:
blocked = True
if not blocked:
return (px+x,py+y)
return (px+x, py+y)
return None

# ******* methods needed to init a PathFinder object *******
Expand Down Expand Up @@ -98,7 +101,7 @@ def grid_map_successors(self, point):
blocked = True
if not blocked:
# must check near non-diag points also
if (0,x) not in successors and (y,0) not in successors:
if (0,x) in successors and (y,0) in successors:
successors.append( (px+x,py+y) )
return successors

Expand Down
4 changes: 2 additions & 2 deletions cheeseboys/level/levels_database/the_south_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def load(name, hero):
level.addPhysicalBackground( (118,1338), (235, 1130) )
level.addPhysicalBackground( (642,1338), (310, 1130) )

crate1 = Crate( (433, 788), 1, 0, physical, visual_obstacles)
crate1 = Crate( (433, 785), 1, 0, physical, visual_obstacles)
level.addSprite(crate1)
crate2 = Crate( (370, 768), 1, 1, physical, visual_obstacles)
crate2 = Crate( (370, 765), 1, 1, physical, visual_obstacles)
level.addSprite(crate2)

level.addAnimations(((110,980),(623, 1284),(704, 1149),(30, 1299),(610, 869),(157, 527),(642, 357),(5, 363)), 'water-wave')
Expand Down

0 comments on commit 44dad51

Please sign in to comment.