Mercurial > rinkhals
changeset 431:129de5883524
Moved intermediate position generation to a more suitable location.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 16:46:12 +0000 |
parents | db7bb20d2336 |
children | d630465d7a84 |
files | gamelib/animal.py gamelib/misc.py |
diffstat | 2 files changed, 16 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/animal.py Sat Nov 21 16:43:03 2009 +0000 +++ b/gamelib/animal.py Sat Nov 21 16:46:12 2009 +0000 @@ -3,7 +3,6 @@ import random from pgu.vid import Sprite -from pgu.algo import getline import imagecache import tiles @@ -355,18 +354,7 @@ def _gen_path(self, start_pos, final_pos): """Construct a direct path from start_pos to final_pos, excluding start_pos""" - if abs(start_pos.x - final_pos.x) < 2 and \ - abs(start_pos.y - final_pos.y) < 2: - # pgu gets this case wrong on occasion. - return [final_pos] - start = start_pos.to_tile_tuple() - end = final_pos.to_tile_tuple() - points = getline(start, end) - points.remove(start) # exclude start_pos - if end not in points: - # Rounding errors in getline cause this - points.append(end) - return [Position(x[0], x[1]) for x in points] + return start_pos.intermediate_positions(final_pos) def _find_best_path_step(self, final_pos, gameboard): """Find the cheapest path to final_pos, and return the next step
--- a/gamelib/misc.py Sat Nov 21 16:43:03 2009 +0000 +++ b/gamelib/misc.py Sat Nov 21 16:46:12 2009 +0000 @@ -4,6 +4,7 @@ from pygame.locals import KEYDOWN, K_ESCAPE from pgu import gui +from pgu.algo import getline import serializer @@ -40,6 +41,20 @@ def __eq__(self, b): return self.x == b.x and self.y == b.y and self.z == b.z + def intermediate_positions(self, b): + """Only operates in two dimensions.""" + if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: + # pgu gets this case wrong on occasion. + return [b] + start = self.to_tile_tuple() + end = b.to_tile_tuple() + points = getline(start, end) + points.remove(start) # exclude start_pos + if end not in points: + # Rounding errors in getline cause this + points.append(end) + return [Position(p[0], p[1]) for p in points] + class WeightedSelection(object): def __init__(self, weightings=None): self.weightings = []