X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blobdiff_plain;f=game_common.py;h=1119ce1b4f8b694a6f8a7f5da6b8686d07434752;hp=0598dfa77e61f715c224a84f7cedaaedb158664b;hb=8ddccb6e5601f8df54f9cbc4c376ad76aee2fe78;hpb=7a02f223961c532d9423433427c0e1b3e1cdb871 diff --git a/game_common.py b/game_common.py index 0598dfa..1119ce1 100644 --- a/game_common.py +++ b/game_common.py @@ -1,31 +1,58 @@ from parser import ArgError -class World: +class MapManager: - def __init__(self): - self.turn = 0 - self.map_size = (0, 0) - self.terrain_map = '' - self.things = [] - self.Thing = Thing # child classes may use an extended Thing class here + def __init__(self, globs): + """With globs a globals() call, collect caller's Map classes.""" + self.map_classes = [] + for name in globs: + if name[:3] == 'Map': + self.map_classes += [globs[name]] + + def get_map_geometries(self): + geometries = [] + for map_class in self.map_classes: + geometries += [map_class.__name__[3:]] + return geometries - def set_map_size(self, yx): - y, x = yx - self.map_size = (y, x) - self.terrain_map = '' - for y in range(self.map_size[0]): - self.terrain_map += '?' * self.map_size[1] + def get_map_class(self, geometry): + for map_class in self.map_classes: + if map_class.__name__[3:] == geometry: + return map_class - def set_map_line(self, y, line): - width_map = self.map_size[1] - if y >= self.map_size[0]: + +class Map: + + def __init__(self, size=(0, 0)): + self.size = size + self.terrain = '?'*self.size_i + + @property + def size_i(self): + return self.size[0] * self.size[1] + + def set_line(self, y, line): + height_map = self.size[0] + width_map = self.size[1] + if y >= height_map: raise ArgError('too large row number %s' % y) width_line = len(line) if width_line > width_map: raise ArgError('too large map line width %s' % width_line) - self.terrain_map = self.terrain_map[:y * width_map] + line + \ - self.terrain_map[(y + 1) * width_map:] + self.terrain = self.terrain[:y * width_map] + line +\ + self.terrain[(y + 1) * width_map:] + + def get_position_index(self, yx): + return yx[0] * self.size[1] + yx[1] + + +class World: + + def __init__(self): + self.Thing = Thing # child classes may use an extended Thing class here + self.turn = 0 + self.things = [] def get_thing(self, id_): for thing in self.things: @@ -35,6 +62,10 @@ class World: self.things += [t] return t + def new_map(self, geometry, yx): + map_type = self.game.map_manager.get_map_class(geometry) + self.map_ = map_type(yx) + class Thing: @@ -45,16 +76,16 @@ class Thing: self.position = [0,0] -class Commander: - - def cmd_MAP_SIZE(self, yx): - """Set self.map_size to yx, redraw self.terrain_map as '?' cells.""" - self.world.set_map_size(yx) - cmd_MAP_SIZE.argtypes = 'yx_tuple:nonneg' +class CommonCommandsMixin: - def cmd_TERRAIN_LINE(self, y, terrain_line): - self.world.set_map_line(y, terrain_line) - cmd_TERRAIN_LINE.argtypes = 'int:nonneg string' + def cmd_MAP(self, geometry, yx): + """Create new map of grid geometry, size yx and only '?' cells.""" + legal_grids = self.map_manager.get_map_geometries() + if geometry not in legal_grids: + raise ArgError('First map argument must be one of: ' + + ', '.join(legal_grids)) + self.world.new_map(geometry, yx) + cmd_MAP.argtypes = 'string yx_tuple:pos' def cmd_THING_TYPE(self, i, type_): t = self.world.get_thing(i)