home · contact · privacy
Fix FOV floating point bugs by using fractions.Fraction for fractions.
[plomrogue2-experiments] / game_common.py
index 5aa4b2544729d337fcef2ee939fe1ddeddc3ea63..1119ce1b4f8b694a6f8a7f5da6b8686d07434752 100644 (file)
@@ -1,11 +1,36 @@
 from parser import ArgError
 
 
+class MapManager:
+
+    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 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), terrain=''):
+    def __init__(self, size=(0, 0)):
         self.size = size
-        self.terrain = terrain
+        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]
@@ -25,10 +50,8 @@ class Map:
 class World:
 
     def __init__(self):
-        self.Map = Map  # child classes may use an extended Map class here
         self.Thing = Thing  # child classes may use an extended Thing class here
         self.turn = 0
-        self.map_ = self.Map()
         self.things = []
 
     def get_thing(self, id_):
@@ -39,8 +62,9 @@ class World:
         self.things += [t]
         return t
 
-    def new_map(self, yx):
-        self.map_ = self.Map(yx, '?')
+    def new_map(self, geometry, yx):
+        map_type = self.game.map_manager.get_map_class(geometry)
+        self.map_ = map_type(yx)
 
 
 class Thing:
@@ -54,10 +78,14 @@ class Thing:
 
 class CommonCommandsMixin:
 
-    def cmd_MAP(self, yx):
-        """Create new map of size yx and only '?' cells."""
-        self.world.new_map(yx)
-    cmd_MAP.argtypes = 'yx_tuple:nonneg'
+    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)