changeset 136:00ada2e29798

Somewhat better (but still hideous) image variant support.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 11 Sep 2011 23:56:59 +0200
parents e0b4bfc51336
children 7fbeeb402685
files data/levels/dev.txt mamba/level.py mamba/mutators.py mamba/sprites.py
diffstat 4 files changed, 81 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/data/levels/dev.txt	Sun Sep 11 22:46:46 2011 +0200
+++ b/data/levels/dev.txt	Sun Sep 11 23:56:59 2011 +0200
@@ -1,3 +1,4 @@
+Dev Level
 lab
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X...~....E.............................X
--- a/mamba/level.py	Sun Sep 11 22:46:46 2011 +0200
+++ b/mamba/level.py	Sun Sep 11 23:56:59 2011 +0200
@@ -35,7 +35,7 @@
     }
 
 THING_MAP = {
-    'a': mktile(BaseSprite, image_name='snake/snake-head-r'),
+    'a': mktile(BaseSprite, image_name='red'),
     }
 
 
@@ -83,6 +83,7 @@
         This file format is potentially yucky.
         """
         level_data = load_file('levels/%s.txt' % (self.level_name,))
+        self.name = level_data.readline().strip()
         tileset_name = level_data.readline().strip()
         self.tileset = Tileset(tileset_name)
         tiles_ascii = [line.strip() for line in level_data.readlines()]
--- a/mamba/mutators.py	Sun Sep 11 22:46:46 2011 +0200
+++ b/mamba/mutators.py	Sun Sep 11 23:56:59 2011 +0200
@@ -28,19 +28,16 @@
 # mutator that does nothing
 NULL = Mutator(lambda x: x)
 
-# sprites mutators
-RIGHT = NULL
-DOWN = Mutator(rotate, 90)
-LEFT = Mutator(rotate, 180)
-UP = Mutator(rotate, -90)
+# base mutators
+R90 = Mutator(rotate, 90)
+R180 = Mutator(rotate, 180)
+R270 = Mutator(rotate, -90)
 
-# tile mutators
-TL = NULL
-BL = Mutator(rotate, -90)
-TR = Mutator(rotate, 90)
-BR = Mutator(rotate, 180)
-HORIZ = NULL
-VERT = Mutator(rotate, 90)
+# sprites mutator aliases
+RIGHT = NULL
+DOWN = R90
+LEFT = R180
+UP = R270
 
 
 # overlays
--- a/mamba/sprites.py	Sun Sep 11 22:46:46 2011 +0200
+++ b/mamba/sprites.py	Sun Sep 11 23:56:59 2011 +0200
@@ -11,15 +11,71 @@
     return (ts_x * p_x, ts_y * p_y)
 
 
+class SpriteImageVariants(object):
+    VARIANTS = {
+        '....': ('-0', ()),
+
+        'X...': ('-1', (mutators.R90,)),
+        '.X..': ('-1', (mutators.R270,)),
+        '..X.': ('-1', (mutators.NULL,)),
+        '...X': ('-1', (mutators.R180,)),
+
+        'XX..': ('-2o', (mutators.NULL,)),
+        '..XX': ('-2o', (mutators.R90,)),
+
+        'X.X.': ('-2a', (mutators.R180,)),
+        'X..X': ('-2a', (mutators.R270,)),
+        '.XX.': ('-2a', (mutators.R90,)),
+        '.X.X': ('-2a', (mutators.NULL,)),
+
+        '.XXX': ('-3', (mutators.R90,)),
+        'X.XX': ('-3', (mutators.R270,)),
+        'XX.X': ('-3', (mutators.NULL,)),
+        'XXX.': ('-3', (mutators.R180,)),
+
+        'XXXX': ('', ()),
+        }
+
+    def __init__(self, base_image_name):
+        self.base_image_name = base_image_name
+
+    def __call__(self, top, bottom, left, right):
+        variant_str = ''.join('X' if d else '.'
+                              for d in [top, bottom, left, right])
+        variant_suffix, mutators = self.VARIANTS[variant_str]
+        return (self.base_image_name + variant_suffix, mutators)
+
+
+class SolidSpriteImageVariants(SpriteImageVariants):
+    VARIANTS = {
+        '....': ('-0', ()),
+
+        'X...': ('-1', (mutators.R90,)),
+        '.X..': ('-1', (mutators.R270,)),
+        '..X.': ('-1', (mutators.NULL,)),
+        '...X': ('-1', (mutators.R180,)),
+
+        'XX..': ('', ()),
+        '..XX': ('', ()),
+
+        'X.X.': ('-2a', (mutators.R180,)),
+        'X..X': ('-2a', (mutators.R270,)),
+        '.XX.': ('-2a', (mutators.R90,)),
+        '.X.X': ('-2a', (mutators.NULL,)),
+
+        '.XXX': ('', ()),
+        'X.XX': ('', ()),
+        'XX.X': ('', ()),
+        'XXX.': ('', ()),
+
+        'XXXX': ('', ()),
+        }
+
+
 class BaseSprite(Sprite):
     tileset = 'common'
-
-    variant_suffix_0 = ''
-    variant_suffix_1 = ''
-    variant_suffix_2o = ''
-    variant_suffix_2a = ''
-    variant_suffix_3 = ''
-    variant_suffix_4 = ''
+    variants_class = None
+    variants = None
 
     def __init__(self, tile_char=None, tileset=None, image_name=None,
                  mutators=()):
@@ -38,29 +94,10 @@
         self.tile_pos = tile_pos
         self.rect = self.image.get_rect().move(tile_sizify(tile_pos))
 
-    def get_tile_variant_modifiers(self, image_name, top, bottom, left, right):
-        variant_str = ''.join('X' if d else '.'
-                              for d in [top, bottom, left, right])
-        variant_suffix, mutator = {
-            '....': (self.variant_suffix_0, mutators.NULL),
-            'X...': (self.variant_suffix_1, mutators.UP),
-            '.X..': (self.variant_suffix_1, mutators.DOWN),
-            '..X.': (self.variant_suffix_1, mutators.LEFT),
-            '...X': (self.variant_suffix_1, mutators.RIGHT),
-            'XX..': (self.variant_suffix_2o, mutators.VERT),
-            '..XX': (self.variant_suffix_2o, mutators.HORIZ),
-            'X.X.': (self.variant_suffix_2a, mutators.TL),
-            'X..X': (self.variant_suffix_2a, mutators.TR),
-            '.XX.': (self.variant_suffix_2a, mutators.BL),
-            '.X.X': (self.variant_suffix_2a, mutators.BR),
-            '.XXX': (self.variant_suffix_3, mutators.UP),
-            'X.XX': (self.variant_suffix_3, mutators.DOWN),
-            'XX.X': (self.variant_suffix_3, mutators.LEFT),
-            'XXX.': (self.variant_suffix_3, mutators.RIGHT),
-            'XXXX': (self.variant_suffix_4, mutators.NULL),
-            }[variant_str]
-
-        return (image_name + variant_suffix, (mutator,))
+    def get_variant(self, top, bottom, left, right):
+        if (self.variants_class is not None) and (self.variants is None):
+            self.variants = self.variants_class(self.image_name)
+        return self.variants(top, bottom, left, right)
 
 
 class TileSprite(BaseSprite):
@@ -100,13 +137,8 @@
 
 class PuddleSprite(SingleImageTileSprite):
     image_name = 'puddle'
-
-    variant_suffix_0 = '-0'
-    variant_suffix_1 = '-1'
-    variant_suffix_2a = '-2a'
+    variants_class = SolidSpriteImageVariants
 
     def __init__(self, variant=(0, 0, 0, 0), **kw):
         super(PuddleSprite, self).__init__(**kw)
-        image_name, mutators = self.get_tile_variant_modifiers(self.image_name,
-                                                               *variant)
-        self.image = self.load_image(image_name, mutators)
+        self.image = self.load_image(*self.get_variant(*variant))