home · contact · privacy
De-hardcode map size test.
[plomrogue2-experiments] / new2 / plomrogue / mapping.py
1 import collections
2
3
4
5 class YX(collections.namedtuple('YX', ('y', 'x'))):
6
7     def __add__(self, other):
8         return YX(self.y + other.y, self.x + other.x)
9
10     def __sub__(self, other):
11         return YX(self.y - other.y, self.x - other.x)
12
13     def __str__(self):
14         return 'Y:%s,X:%s' % (self.y, self.x)
15
16
17
18 class MapGeometry():
19
20     def __init__(self, size):
21         self.size = size
22
23     def get_directions(self):
24         directions = []
25         for name in dir(self):
26             if name[:5] == 'move_':
27                 directions += [name[5:]]
28         return directions
29
30     def move(self, start_pos, direction):
31         mover = getattr(self, 'move_' + direction)
32         target = mover(start_pos)
33         if target.y < 0 or target.x < 0 or \
34                 target.y >= self.size.y or target.x >= self.size.x:
35             return None
36         return target
37
38
39
40 class MapGeometryWithLeftRightMoves(MapGeometry):
41
42     def move_LEFT(self, start_pos):
43         return YX(start_pos.y, start_pos.x - 1)
44
45     def move_RIGHT(self, start_pos):
46         return YX(start_pos.y, start_pos.x + 1)
47
48
49
50 class MapGeometrySquare(MapGeometryWithLeftRightMoves):
51
52     def move_UP(self, start_pos):
53         return YX(start_pos.y - 1, start_pos.x)
54
55     def move_DOWN(self, start_pos):
56         return YX(start_pos.y + 1, start_pos.x)