Add crude serializer to map generator
authorNeil <neil@dip.sun.ac.za>
Tue, 1 Mar 2016 09:33:50 +0000 (11:33 +0200)
committerNeil <neil@dip.sun.ac.za>
Tue, 1 Mar 2016 09:33:50 +0000 (11:33 +0200)
koperkapel/generators/maps.py

index 6f62b0b5fd05e9b99993b93d02667e9fdab6370c..94269294e914d6f7b6b9430119d279e54b758b36 100644 (file)
@@ -2,10 +2,22 @@
 
 import random
 import math
+import json
+import os
 
 i = random.randint(0,100)
 
 
+ATTRIBUTE_MAP = {
+    '#': {'base': 'cwall', 
+          'behaviour': [],
+         },
+    ' ': {'base': 'floor',
+          'behaviour': ['walk', 'fly'],
+         },
+}
+
+
 class LevelGenerator:
     width = 0
     height = 0
@@ -124,9 +136,25 @@ class LevelGenerator:
             file.write(''.join(l))
             file.write('\n')
         file.close()
+        self._to_json()
         for l in self.regions:
             print(l)
 
+    def _to_json(self):
+        level = {}
+        level['tiles'] = []
+        for l in self.map:
+            row = []
+            for t in l:
+                row.append(ATTRIBUTE_MAP[t])
+            level['tiles'].append(row)
+        name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json')
+        # FIXME: Do a lot better here 
+        # Crude hack so the level is written into the levels folder
+        f = open(name, 'w')
+        json.dump(level, f)
+        f.close()
+
 
 if __name__ == '__main__':
     while True: