Commits

Richard Jones committed 91afdce

simplify the property lookup stuff in tmx

Comments (0)

Files changed (8)

 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             if last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
             if last.left >= cell.right and new.left < cell.right:
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player('player.png', (start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)

15-blocker-sides.py

 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player('player.png', (start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
     direction = 1
     def update(self, dt, game):
         self.rect.x += self.direction * 100 * dt
-        for cell in game.tilemap.layers_by_name['triggers'].collide(self.rect, 'reverse'):
+        for cell in game.tilemap.layers['triggers'].collide(self.rect, 'reverse'):
             if self.direction > 0:
                 self.rect.right = cell.left
             else:
 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player('player.png', (start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
 
         self.enemies = tmx.SpriteLayer()
-        for enemy in self.tilemap.layers_by_name['triggers'].find(enemy=''):
+        for enemy in self.tilemap.layers['triggers'].find('enemy'):
             self.enemies.add(Enemy('enemy.png', (enemy.px, enemy.py)))
         self.tilemap.layers.append(self.enemies)
 

17-player-direction.py

     direction = 1
     def update(self, dt, game):
         self.rect.x += self.direction * 100 * dt
-        for cell in game.tilemap.layers_by_name['triggers'].collide(self.rect, 'reverse'):
+        for cell in game.tilemap.layers['triggers'].collide(self.rect, 'reverse'):
             if self.direction > 0:
                 self.rect.right = cell.left
             else:
 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player((start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
 
         self.enemies = tmx.SpriteLayer()
-        for enemy in self.tilemap.layers_by_name['triggers'].find(enemy=''):
+        for enemy in self.tilemap.layers['triggers'].find('enemy'):
             self.enemies.add(Enemy('enemy.png', (enemy.px, enemy.py)))
         self.tilemap.layers.append(self.enemies)
 
     direction = 1
     def update(self, dt, game):
         self.rect.x += self.direction * 100 * dt
-        for cell in game.tilemap.layers_by_name['triggers'].collide(self.rect, 'reverse'):
+        for cell in game.tilemap.layers['triggers'].collide(self.rect, 'reverse'):
             if self.direction > 0:
                 self.rect.right = cell.left
             else:
 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player((start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
 
         self.enemies = tmx.SpriteLayer()
-        for enemy in self.tilemap.layers_by_name['triggers'].find(enemy=''):
+        for enemy in self.tilemap.layers['triggers'].find('enemy'):
             self.enemies.add(Enemy('enemy.png', (enemy.px, enemy.py)))
         self.tilemap.layers.append(self.enemies)
 
     direction = 1
     def update(self, dt, game):
         self.rect.x += self.direction * 100 * dt
-        for cell in game.tilemap.layers_by_name['triggers'].collide(self.rect, 'reverse'):
+        for cell in game.tilemap.layers['triggers'].collide(self.rect, 'reverse'):
             if self.direction > 0:
                 self.rect.right = cell.left
             else:
 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player((start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
 
         self.enemies = tmx.SpriteLayer()
-        for enemy in self.tilemap.layers_by_name['triggers'].find(enemy=''):
+        for enemy in self.tilemap.layers['triggers'].find('enemy'):
             self.enemies.add(Enemy('enemy.png', (enemy.px, enemy.py)))
         self.tilemap.layers.append(self.enemies)
 
     direction = 1
     def update(self, dt, game):
         self.rect.x += self.direction * 100 * dt
-        for cell in game.tilemap.layers_by_name['triggers'].collide(self.rect, 'reverse'):
+        for cell in game.tilemap.layers['triggers'].collide(self.rect, 'reverse'):
             if self.direction > 0:
                 self.rect.right = cell.left
             else:
 
         new = self.rect
         self.resting = False
-        for cell in game.tilemap.layers_by_name['triggers'].collide(new, 'blockers'):
+        for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
             blockers = cell.tile.properties['blockers']
             if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                 new.right = cell.left
         self.tilemap = tmx.load('map.tmx', screen.get_size())
 
         self.sprites = tmx.SpriteLayer()
-        start_cell = self.tilemap.layers_by_name['triggers'].find(player='')[0]
+        start_cell = self.tilemap.layers['triggers'].find('player')[0]
         self.player = Player((start_cell.px, start_cell.py))
         self.sprites.add(self.player)
         self.tilemap.layers.append(self.sprites)
 
         self.enemies = tmx.SpriteLayer()
-        for enemy in self.tilemap.layers_by_name['triggers'].find(enemy=''):
+        for enemy in self.tilemap.layers['triggers'].find('enemy'):
             self.enemies.add(Enemy('enemy.png', (enemy.px, enemy.py)))
         self.tilemap.layers.append(self.enemies)
 
                 cell = self.cells[i, j]
                 screen.blit(cell.tile.surface, (cell.px-ox, cell.py-oy))
 
-    def find(self, **properties):
-        '''Find all cells with the given properties.
+    def find(self, *properties):
+        '''Find all cells with the given properties set.
+        '''
+        r = []
+        for propname in properties:
+            for cell in self.cells.values():
+                p = cell.tile.properties
+                if propname in p:
+                    r.append(cell)
+        return r
+
+    def match(self, **properties):
+        '''Find all cells with the given properties set to the given values.
         '''
         r = []
         for propname in properties:
             sx, sy = sprite.rect.topleft
             screen.blit(sprite.image, (sx-ox, sy-oy))
 
+class Layers(list):
+    def __init__(self):
+        self.by_name = {}
+
+    def add_named(self, layer, name):
+        self.append(layer)
+        self.by_name[name] = layer
+
+    def __getitem__(self, item):
+        if isinstance(item, int):
+            return self[item]
+        return self.by_name[item]
+
 class TileMap(sax.ContentHandler):
     def __init__(self, size, origin=(0,0)):
         self.px_width = 0
         self.width = 0
         self.height  = 0
         self.properties = {}
-        self.layers = []
-        self.layers_by_name = {}
+        self.layers = Layers()
         self.tilesets = Tilesets()
         self.layer = None
         self.tile = None
         # starting counting
         elif name == 'layer':
             self.layer = Layer(attrs['name'], int(attrs.get('visible', 1)), self)
-            self.layers.append(self.layer)
-            self.layers_by_name[attrs['name']] = self.layer
+            self.layers.add_named(self.layer, attrs['name'])
 
         elif name == 'data':
             self.is_data = True