home · contact · privacy
Register game commands and tasks outside of game module.
[plomrogue2-experiments] / game_common.py
index 39a12f1612f0cbd15d342a82c88c26feace90dde..7731c61eebe782e8c8e07187e9925a1d20343539 100644 (file)
@@ -1,11 +1,33 @@
 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), 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]
@@ -18,29 +40,30 @@ class Map:
         self.terrain = self.terrain[:y * width_map] + line +\
                        self.terrain[(y + 1) * width_map:]
 
-    def set_size(self, yx):
-        y, x = yx
-        self.size = (y, x)
-        self.terrain = ''
-        for y in range(self.size[0]):
-            self.terrain += '?' * self.size[1]
+    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.map_ = Map()
         self.things = []
-        self.Thing = Thing  # child classes may use an extended Thing class here
 
-    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 = self.game.map_manager.get_map_class(geometry)
+        self.map_ = map_type(yx)
 
 
 class Thing:
@@ -54,10 +77,10 @@ class Thing:
 
 class CommonCommandsMixin:
 
-    def cmd_MAP_SIZE(self, yx):
-        """Set self.map_size to yx, redraw self.terrain_map as '?' cells."""
-        self.world.map_.set_size(yx)
-    cmd_MAP_SIZE.argtypes = 'yx_tuple:nonneg'
+    def cmd_MAP(self, geometry, yx):
+        """Create new map of grid geometry, size yx and only '?' cells."""
+        self.world.new_map(geometry, yx)
+    cmd_MAP.argtypes = 'string:geometry yx_tuple:pos'
 
     def cmd_THING_TYPE(self, i, type_):
         t = self.world.get_thing(i)