Commits

Cory Li  committed e3238a2

Basic model implemented, along with simple hardcoded mouse events

  • Participants
  • Parent commits 52f2279

Comments (0)

Files changed (1)

 import pygame
 import pygame.locals
 import random
+import json
+import math
+
+
+
+# CONSTANTS
+LEFT = 1
+
+
+# DECLARE ENUMS
+class Terrain:
+    GROUND = 0
+    WATER = 1
+    WALL = 2
+
+
+class TiledMap:
+
+    # Resonable Defaults
+    name = "Untitled"
+    author = "Unknown"
+    game_mode = 0
+    size = (0,0)
+
+    # Empty Data
+    map_collision = []
+    map_ground = []
+    map_sky = []
+    spawn_data = []
+
+
+    def __init__(self,size):
+        self.size = size
+        self.map_collision = self.initialize_grid(size, Terrain.GROUND)
+        self.map_ground = self.initialize_grid(size)
+        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)]
+
+    def add_spawn(team, position):
+        (x,y) = position
+        self.spawn_data.append({
+                'team' : team,
+                'x': x,
+                'y': y,
+            })
+
+    def export(self):
+        return {
+            'metadata':{
+                'size': self.size,
+                'name': self.name,
+                'author': self.author,
+                'game_mode': self.game_mode },
+            'mapdata':{
+                'collision': self.map_collision,
+                'ground': self.map_ground,
+                'sky': self.map_sky },
+            'spawndata': self.spawn_data,
+            }
+
+class MapViewer:
+    tile_size = 8 #pixels
+
+    camWidth = 100
+    camHeight = 100
+    camX = 0
+    camY = 0
+
+    def __init__(self,model):
+        pygame.init()
+        self.model = model
+        self.screen = pygame.display.set_mode((
+                        self.camWidth * self.tile_size,
+                        self.camHeight * self.tile_size))
+        pygame.display.set_caption('Spells Map Editor')
+        self.screen.fill((255,255,255))
+
+    def draw_layer(self,mapdata):
+        sizeX,sizeY = self.model.size
+        for x in range(sizeX):
+            for y in range(sizeY):
+                if mapdata[x][y] == 0:
+                    self.draw_tile((x,y),(255,0,0))
+                if mapdata[x][y] == 1:
+                    self.draw_tile((x,y),(0,255,0))
+
+    def draw_tile(self,pos,color):
+        pygame.draw.rect(self.screen, color,
+                (pos[0]*self.tile_size, pos[1]*self.tile_size, self.tile_size, self.tile_size))
+
+
+    def update(self):
+        self.draw_layer(self.model.map_collision)
+        pygame.display.update()
+
+    def get_tile(self, pos):
+        x,y = pos
+        return (x/self.tile_size, y/self.tile_size)
+
 
 
 
 if __name__=='__main__':
-    pygame.init()
-    screen = pygame.display.set_mode((128,98))
-    screen.fill((255,255,255))
-
-    pygame.display.flip()
+    model = TiledMap((100,100))
+    view = MapViewer(model)
 
     mainloop = True
     clock = pygame.time.Clock()
     while mainloop:
-
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 mainloop = False
+            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
+                tile =  view.get_tile(event.pos)
+                model.map_collision[tile[0]][tile[1]] = 1
+
             elif event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_ESCAPE:
                     mainloop = False
-        pygame.display.update()
-
+        view.update()