Commits

Cory Li committed 9ecf802

cleaned up export routine a bit

Comments (0)

Files changed (1)

 import random
 import json
 import math
+import numpy
 
 from terrain_algo import DiamondSquare
 from constants import Key, Collision, Tile
     # Resonable Defaults
     name = "Untitled"
     author = "Unknown"
+    tileset = "blank.png"
     game_mode = 0
     size = (0,0)
 
         self.map_sky = self.initialize_grid(size)
 
     def initialize_grid(self,(sizeX,sizeY), val=0):
-        return [[val for y in range(sizeY)] for x in range(sizeX)]
+        return numpy.array([[val for y in range(sizeY)] for x in range(sizeX)])
 
     def add_spawn(self,team, position, radius):
         (x,y) = position
         ds_algo = DiamondSquare(lod,roughness)
         size = ds_algo.size
 
-        self.map_collision = ds_algo.generate().threshold([0.40,0.75,1.0]).scale_by(2).spriteize()
-        self.map_ground = self.initialize_grid((size,size))
+        newmap = ds_algo.generate().threshold([0.40,0.75,1.0]).scale_by(2)
+
+        self.map_collision = newmap.world
+        self.map_ground = newmap.spriteize()
         self.map_sky = self.initialize_grid((size,size))
 
 
     def export(self):
-        return {
+        out =  {
             'metadata':{
                 'size': self.size,
                 'name': self.name,
                 'author': self.author,
-                'game_mode': self.game_mode },
+                'game_mode': self.game_mode,
+                'tileset': self.tileset },
             'mapdata':{
-                'collision': self.map_collision,
-                'ground': self.map_ground,
-                'sky': self.map_sky },
+                'collision': self.map_collision.tolist(),
+                'ground': self.map_ground.tolist(),
+                'sky': self.map_sky.tolist()},
             'spawndata': self.spawn_data,
             }
+        return json.dumps(out)
 
 class MapViewer:
     tile_size = 16 #pixels
         self.screen.blit(scaled_tile, (pos[0]*self.tile_size, pos[1]*self.tile_size))
 
     def update(self):
-        self.draw_layer(self.model.map_collision)
+        self.draw_layer(self.model.map_ground)
         pygame.display.update()
 
     def get_tile(self, pos):
             elif event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_ESCAPE:
                     mainloop = False
-                if event.key == pygame.K_SPACE:
+                elif event.key == pygame.K_SPACE:
                     model.randomize_ds(5,0.6)
+                elif event.key == pygame.K_e:
+                    with open('export.map','w') as f:
+                        f.write(model.export())
 
         view.update()