Hook up some stuff to the settings options
authorNeil <neil@dip.sun.ac.za>
Fri, 19 Apr 2013 10:33:50 +0000 (12:33 +0200)
committerNeil <neil@dip.sun.ac.za>
Fri, 19 Apr 2013 10:33:50 +0000 (12:33 +0200)
erdslangetjie/__main__.py
erdslangetjie/level.py

index 93b39f531918045ee60bc0e75a2d3ba8386d0ab0..dd96a96be8e6d983319d46de1662234e77aa0460 100644 (file)
@@ -343,8 +343,40 @@ class GameApp(App):
         self.levels = LevelList()
         self.game = None
 
+    def build_config(self, config):
+        config.setdefaults('bane', {
+            'start_level': 'levels/level1.txt',
+            'sound': 'True'
+            })
+
+    def build_settings(self, settings):
+        config_json = """[
+            { "type": "title",
+              "title": "Bane's Befuddlement"
+            },
+
+            { "type": "options",
+              "title": "Start Level",
+              "desc": "Level to start at",
+              "section": "bane",
+              "key": "start_level",
+              "options": ["%s"] },
+
+            { "type": "bool",
+              "title": "Sound",
+              "desc": "Enable sound",
+              "section": "bane",
+              "key": "sound"
+             }
+             ]""" % '", "'.join(self.levels.get_level_names())
+        settings.add_json_panel("Bane's Befuddlement",
+                self.config, data=config_json)
+
     def build(self):
         root = ScrollView(size_hint=(None, None))
+        level_name = self.config.getdefault('bane', 'start_level', None)
+        if level_name:
+            self.levels.set_level_to(level_name)
         return root
 
     def on_start(self):
index f430f5dd53a27e2def6254aaca778917dd32f7aa..2246b1f2acd6910aa2e3bc17ca0b5f10354d41ea 100644 (file)
@@ -288,8 +288,9 @@ class LevelList(object):
     LEVELS = 'level_list'
 
     def __init__(self):
-        self.levels = []
-        self.errors = []
+        self._levels = []
+        self._level_names = []
+        self._errors = []
         level_list = load(self.LEVELS)
         for line in level_list:
             line = line.strip()
@@ -299,24 +300,32 @@ class LevelList(object):
                 level_file.close()
                 try:
                     level.validate()
-                    self.levels.append(level)
+                    self._levels.append(level)
+                    self._level_names.append(line)
                 except RuntimeError as err:
-                    self.errors.append(
+                    self._errors.append(
                             'Invalid level %s in level_list: %s' % (line, err))
             else:
-                self.errors.append(
+                self._errors.append(
                     'Level list includes non-existant level %s' % line)
         level_list.close()
         self._cur_level = 0
 
     def get_current_level(self):
-        if self._cur_level < len(self.levels):
-            return self.levels[self._cur_level]
+        if self._cur_level < len(self._levels):
+            return self._levels[self._cur_level]
         else:
             return None
 
     def get_errors(self):
-        return self.errors
+        return self._errors
+
+    def get_level_names(self):
+        return self._level_names
+
+    def set_level_to(self, level_name):
+        if level_name in self._level_names:
+            self._cur_level = self._level_names.index(level_name)
 
     def advance_to_next_level(self):
         self._cur_level += 1