|
|
@@ -10,6 +10,9 @@ class Tileset(object):
|
|
|
LEFT = 2
|
|
|
UP = 3
|
|
|
|
|
|
+ PLAYER = 209
|
|
|
+ ENEMY1 = 161
|
|
|
+
|
|
|
def __init__(self, screen):
|
|
|
self.screen = screen
|
|
|
image = pygame.image.load('tileset.png').convert()
|
|
|
@@ -42,15 +45,35 @@ class Tileset(object):
|
|
|
|
|
|
def load_level(self): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241
|
|
|
level_data = ElementTree.parse('level1.tmx').getroot()
|
|
|
- raw_data = level_data.findall('layer')[0].findall('data')[0].text.strip()
|
|
|
- data = raw_data.decode('base64').decode('zlib')
|
|
|
- # read as array of 4-byte ints
|
|
|
- self.level_array = struct.unpack('<{}i'.format(len(data)/4), data)
|
|
|
+ layers = level_data.findall('layer')
|
|
|
+ if len(layers) != 2:
|
|
|
+ raise Exception('expected 2 layers: level and units')
|
|
|
+ loaded = {}
|
|
|
+ for layer in layers:
|
|
|
+ name = layer.attrib['name']
|
|
|
+ raw_data = layer.findall('data')[0].text.strip()
|
|
|
+ data = raw_data.decode('base64').decode('zlib')
|
|
|
+ # read as array of 4-byte ints
|
|
|
+ array = list(struct.unpack('<{}i'.format(len(data)/4), data))
|
|
|
+ loaded[name] = array
|
|
|
+ self.level_array = loaded['level']
|
|
|
+
|
|
|
+ units = {
|
|
|
+ self.ENEMY1: [],
|
|
|
+ self.PLAYER: [],
|
|
|
+ }
|
|
|
+ for i, tile in enumerate(loaded['units']):
|
|
|
+ if tile in units.iterkeys():
|
|
|
+ y, x = divmod(i, 50)
|
|
|
+ units[tile].append((x, y))
|
|
|
+ if len(units[self.PLAYER]) != 1:
|
|
|
+ raise Exception('expected exactly 1 player tile in units layer, found ' + str(len(units[self.PLAYER])))
|
|
|
+ return units
|
|
|
|
|
|
def render_level(self):
|
|
|
for i, tile in enumerate(self.level_array):
|
|
|
- x, y = divmod(i, 50)
|
|
|
+ y, x = divmod(i, 50)
|
|
|
x *= self.tilesize
|
|
|
y *= self.tilesize
|
|
|
tile_y, tile_x = divmod(tile - 1, self.tilesize) # those 1-indexing fuckers
|
|
|
- self._render(tile_x, tile_y, (y, x))
|
|
|
+ self._render(tile_x, tile_y, (x, y))
|