|
|
@@ -41,14 +41,27 @@ class Tileset(object):
|
|
|
y *= self.TILESIZE
|
|
|
self.screen.blit(tileset, pos, (x, y, self.TILESIZE, self.TILESIZE))
|
|
|
|
|
|
+ def _render_layer(self, layer):
|
|
|
+ for i, tile in enumerate(layer):
|
|
|
+ y, x = divmod(i, self.MAPSIZE)
|
|
|
+ x *= self.TILESIZE
|
|
|
+ y *= self.TILESIZE
|
|
|
+ if tile != 0:
|
|
|
+ tile_y, tile_x = divmod(tile - 1, self.TILESIZE) # those 1-indexing fuckers
|
|
|
+ self._render(tile_x, tile_y, (x, y))
|
|
|
+
|
|
|
def render_person(self, pos, direction):
|
|
|
self._render(0, 13, pos, direction)
|
|
|
|
|
|
+ def render_level(self):
|
|
|
+ self._render_layer(self.floor)
|
|
|
+ self._render_layer(self.walls)
|
|
|
+
|
|
|
def load_level(self): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241
|
|
|
level_data = ElementTree.parse('level1.tmx').getroot()
|
|
|
layers = level_data.findall('layer')
|
|
|
- if len(layers) != 2:
|
|
|
- raise Exception('expected 2 layers: level and units')
|
|
|
+ if len(layers) != 3:
|
|
|
+ raise Exception('expected 3 layers: floor, wall, and units')
|
|
|
loaded = {}
|
|
|
for layer in layers:
|
|
|
name = layer.attrib['name']
|
|
|
@@ -58,14 +71,18 @@ class Tileset(object):
|
|
|
array = list(struct.unpack('<{}i'.format(len(data)/4), data))
|
|
|
loaded[name] = array
|
|
|
|
|
|
- self.level_array = loaded['level']
|
|
|
- collidables = frozenset([6, 7, 8, 22, 24, 38, 39, 40])
|
|
|
+ # load floor
|
|
|
+ self.floor = loaded['floor']
|
|
|
+
|
|
|
+ # load wall
|
|
|
+ self.walls = loaded['wall']
|
|
|
collisions = set()
|
|
|
- for i, tile in enumerate(loaded['level']):
|
|
|
- if tile in collidables:
|
|
|
+ for i, tile in enumerate(loaded['wall']):
|
|
|
+ if tile != 0:
|
|
|
y, x = divmod(i, self.MAPSIZE)
|
|
|
collisions.add((x, y))
|
|
|
|
|
|
+ # load units
|
|
|
units = {
|
|
|
self.ENEMY1: [],
|
|
|
self.PLAYER: [],
|
|
|
@@ -76,12 +93,5 @@ class Tileset(object):
|
|
|
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 collisions, units
|
|
|
|
|
|
- def render_level(self):
|
|
|
- for i, tile in enumerate(self.level_array):
|
|
|
- y, x = divmod(i, self.MAPSIZE)
|
|
|
- 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, (x, y))
|
|
|
+ return collisions, units
|