X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blobdiff_plain;f=game_common.py;h=7731c61eebe782e8c8e07187e9925a1d20343539;hp=b37e4475ffaf3fd420e53cd39eea6551c169bce2;hb=HEAD;hpb=1c48dfdf85d5549202412f01eced7712adf2d468 diff --git a/game_common.py b/game_common.py index b37e447..7731c61 100644 --- a/game_common.py +++ b/game_common.py @@ -1,6 +1,24 @@ from parser import ArgError +class MapManager: + + def __init__(self, map_classes): + """Collects tuple of basic Map[Geometry] classes.""" + self.map_classes = map_classes + + def get_map_geometries(self): + geometries = [] + for map_class in self.map_classes: + geometries += [map_class.__name__[3:]] + return geometries + + def get_map_class(self, geometry): + for map_class in self.map_classes: + if map_class.__name__[3:] == geometry: + return map_class + + class Map: def __init__(self, size=(0, 0)): @@ -33,16 +51,18 @@ class World: self.turn = 0 self.things = [] - def get_thing(self, id_): + def get_thing(self, id_, create_unfound=True): for thing in self.things: if id_ == thing.id_: return thing - t = self.Thing(self, id_) - self.things += [t] - return t + if create_unfound: + t = self.Thing(self, id_) + self.things += [t] + return t + return None def new_map(self, geometry, yx): - map_type = getattr(self, 'Map' + geometry) + map_type = self.game.map_manager.get_map_class(geometry) self.map_ = map_type(yx) @@ -59,12 +79,8 @@ class CommonCommandsMixin: def cmd_MAP(self, geometry, yx): """Create new map of grid geometry, size yx and only '?' cells.""" - legal_grids = {'Hex', 'Square'} - 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:nonneg' + cmd_MAP.argtypes = 'string:geometry yx_tuple:pos' def cmd_THING_TYPE(self, i, type_): t = self.world.get_thing(i)