diff mamba/sprites.py @ 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 b292370c4548
line wrap: on
line diff
--- 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))