home · contact · privacy
Fix faulty assumption about position of player in world.things.
[plomrogue2-experiments] / game_common.py
1 from parser import ArgError
2
3
4 class World:
5
6     def __init__(self):
7         self.turn = 0
8         self.map_size = (0, 0)
9         self.terrain_map = ''
10         self.things = []
11         self.Thing = Thing  # child classes may use an extended Thing class here
12
13     def set_map_size(self, yx):
14         y, x = yx
15         self.map_size = (y, x)
16         self.terrain_map = ''
17         for y in range(self.map_size[0]):
18             self.terrain_map += '?' * self.map_size[1]
19
20     def set_map_line(self, y, line):
21         width_map = self.map_size[1]
22         if y >= self.map_size[0]:
23             raise ArgError('too large row number %s' % y)
24         width_line = len(line)
25         if width_line > width_map:
26             raise ArgError('too large map line width %s' % width_line)
27         self.terrain_map = self.terrain_map[:y * width_map] + line + \
28                            self.terrain_map[(y + 1) * width_map:]
29
30     def get_thing(self, id_):
31         for thing in self.things:
32             if id_ == thing.id_:
33                 return thing
34         t = self.Thing(self, id_)
35         self.things += [t]
36         return t
37
38
39 class Thing:
40
41     def __init__(self, world, id_):
42         self.world = world
43         self.id_ = id_
44         self.type_ = '?'
45         self.position = [0,0]