Mercurial > rinkhals
comparison gamelib/misc.py @ 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 | e89a1afe4e84 |
children | 96dbf2c8506e |
comparison
equal
deleted
inserted
replaced
430:db7bb20d2336 | 431:129de5883524 |
---|---|
2 | 2 |
3 import random | 3 import random |
4 | 4 |
5 from pygame.locals import KEYDOWN, K_ESCAPE | 5 from pygame.locals import KEYDOWN, K_ESCAPE |
6 from pgu import gui | 6 from pgu import gui |
7 from pgu.algo import getline | |
7 | 8 |
8 import serializer | 9 import serializer |
9 | 10 |
10 class Position(serializer.Simplifiable): | 11 class Position(serializer.Simplifiable): |
11 """2D position / vector""" | 12 """2D position / vector""" |
37 def right_of(self, b): | 38 def right_of(self, b): |
38 return self.x > b.x | 39 return self.x > b.x |
39 | 40 |
40 def __eq__(self, b): | 41 def __eq__(self, b): |
41 return self.x == b.x and self.y == b.y and self.z == b.z | 42 return self.x == b.x and self.y == b.y and self.z == b.z |
43 | |
44 def intermediate_positions(self, b): | |
45 """Only operates in two dimensions.""" | |
46 if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: | |
47 # pgu gets this case wrong on occasion. | |
48 return [b] | |
49 start = self.to_tile_tuple() | |
50 end = b.to_tile_tuple() | |
51 points = getline(start, end) | |
52 points.remove(start) # exclude start_pos | |
53 if end not in points: | |
54 # Rounding errors in getline cause this | |
55 points.append(end) | |
56 return [Position(p[0], p[1]) for p in points] | |
42 | 57 |
43 class WeightedSelection(object): | 58 class WeightedSelection(object): |
44 def __init__(self, weightings=None): | 59 def __init__(self, weightings=None): |
45 self.weightings = [] | 60 self.weightings = [] |
46 self.total = 0 | 61 self.total = 0 |