home · contact · privacy
Create new Map object instead of changing it.
[plomrogue2-experiments] / game_common.py
1 from parser import ArgError
2
3
4 class Map:
5
6     def __init__(self, size=(0, 0), terrain=''):
7         self.size = size
8         self.terrain = terrain
9
10     def set_line(self, y, line):
11         height_map = self.size[0]
12         width_map = self.size[1]
13         if y >= height_map:
14             raise ArgError('too large row number %s' % y)
15         width_line = len(line)
16         if width_line > width_map:
17             raise ArgError('too large map line width %s' % width_line)
18         self.terrain = self.terrain[:y * width_map] + line +\
19                        self.terrain[(y + 1) * width_map:]
20
21     def get_position_index(self, yx):
22         return yx[0] * self.size[1] + yx[1]
23
24
25 class World:
26
27     def __init__(self):
28         self.Map = Map  # child classes may use an extended Map class here
29         self.Thing = Thing  # child classes may use an extended Thing class here
30         self.turn = 0
31         self.map_ = self.Map()
32         self.things = []
33
34     def get_thing(self, id_):
35         for thing in self.things:
36             if id_ == thing.id_:
37                 return thing
38         t = self.Thing(self, id_)
39         self.things += [t]
40         return t
41
42     def new_map(self, yx):
43         self.map_ = self.Map(yx, '?')
44
45
46 class Thing:
47
48     def __init__(self, world, id_):
49         self.world = world
50         self.id_ = id_
51         self.type_ = '?'
52         self.position = [0,0]
53
54
55 class CommonCommandsMixin:
56
57     def cmd_MAP(self, yx):
58         """Create new map of size yx and only '?' cells."""
59         self.world.new_map(yx)
60     cmd_MAP.argtypes = 'yx_tuple:nonneg'
61
62     def cmd_THING_TYPE(self, i, type_):
63         t = self.world.get_thing(i)
64         t.type_ = type_
65     cmd_THING_TYPE.argtypes = 'int:nonneg string'
66
67     def cmd_THING_POS(self, i, yx):
68         t = self.world.get_thing(i)
69         t.position = list(yx)
70     cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'