changeset 173:71911af9d42d

Refactor drawing code. Correct tile variants when painting
author Neil Muller <drnlmuller@gmail.com>
date Wed, 14 Sep 2011 01:13:23 +0200
parents dabf13abb3fd
children 893fb9f8f468
files mamba/level.py mamba/widgets/level.py
diffstat 2 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/level.py	Wed Sep 14 00:51:41 2011 +0200
+++ b/mamba/level.py	Wed Sep 14 01:13:23 2011 +0200
@@ -170,3 +170,24 @@
     def draw(self, surface):
         surface.blit(self.background, (0, 0))
         self.sprites.draw(surface)
+
+    def get_tile(self, tile_pos):
+        x, y = tile_pos
+        if 0 <= x < self.tile_size[0] and 0 <= y < self.tile_size[1]:
+            return self.tiles[y][x]
+        return None
+
+    def replace_tile(self, tile_pos, new_tile_char):
+        x, y = tile_pos
+        old_tile = self.tiles[y][x]
+        if old_tile is not None:
+            old_tile.remove(self.sprites)
+        new_tile = self.tileset.get_tile(new_tile_char, tile_pos, self.sprites)
+        self.tiles[y][x] = new_tile
+        # Fix orientations
+        tiles = [self.get_tile((x + dx, y + dy)) for dx in (-1, 0, 1)
+                for dy in (-1, 0, 1)]
+        for tile in tiles:
+            if tile:
+                orientation = self.get_tile_orientation(tile)
+                tile.use_variant(*orientation)
--- a/mamba/widgets/level.py	Wed Sep 14 00:51:41 2011 +0200
+++ b/mamba/widgets/level.py	Wed Sep 14 01:13:23 2011 +0200
@@ -35,13 +35,9 @@
         # and replace the tile with the current tool
         tile_pos = (pixel_pos[0] / TILE_SIZE[0],
                 pixel_pos[1] / TILE_SIZE[1])
-        old_tile = self.level.tiles[tile_pos[1]][tile_pos[0]]
+        old_tile = self.level.get_tile(tile_pos)
         if self.tool == '.' and old_tile is None:
             return
         elif old_tile is not None and old_tile.tile_char == self.tool:
             return
-        if old_tile is not None:
-            old_tile.remove(self.level.sprites)
-        new_tile = self.level.tileset.get_tile(self.tool, tile_pos,
-                self.level.sprites)
-        self.level.tiles[tile_pos[1]][tile_pos[0]] = new_tile
+        self.level.replace_tile(tile_pos, self.tool)