From ed3e525ba11d3383fdf0041e26286c72f342e91a Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 3 Mar 2016 13:32:35 +0200 Subject: [PATCH] Load tunnel tiles --- koperkapel/loaders/levelloader.py | 75 +++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/koperkapel/loaders/levelloader.py b/koperkapel/loaders/levelloader.py index 6820b15..8e47b5c 100644 --- a/koperkapel/loaders/levelloader.py +++ b/koperkapel/loaders/levelloader.py @@ -18,6 +18,21 @@ class Tile: raise NotImplementedError() return images.load(os.path.join(cls.TILESET, cls.IMG)) +class OrientatedTile: + IMG = None + TILESET = None + ANGLE = None + + @classmethod + def image(cls, neighbors): + if cls.IMG is None or cls.TILESET is None: + raise NotImplementedError() + img = images.load(os.path.join(cls.TILESET, cls.IMG)) + if cls.ANGLE: + img = rotate(img, cls.ANGLE) + return img + + class RandomizedTile(Tile): IMGDIR = None TILESET = None @@ -47,29 +62,61 @@ class Wall(RandomizedTile): class Underground(RandomizedTile): IMGDIR = "underground" -class Tunnel(Tile): +class Tunnel(OrientatedTile): @classmethod def image(cls, neighbors): - connections = len([x for x in neighbors if 'walk' in x['behaviour']]) + connections = [True if 'walk' in x['behaviour'] else False for x in neighbors] + conn_count = connections.count(True) # simple cases - if connections == 0: + cls.ANGLE = 0 + if conn_count == 0: # return single point tunnel - pass - elif connections == 4: + cls.IMG = os.path.join('tunnel', 'tunnel_none') + elif conn_count == 4: # crossroads - pass - elif connections == 3: - # 3 point connector, rotated correctly - pass - elif connections == 1: + cls.IMG = os.path.join('tunnel', 'tunnel_crossroads') + elif conn_count == 1: # 1 point connector, roatated correctly - pass - elif connections == 2: + cls.IMG = os.path.join('tunnel', 'tunnel_1way') + # because of the ordering of neighbors, we use this formulation + for x, angle in zip(connections, (90, 270, 0, 180)): + if x: + cls.ANGLE = angle + break + elif conn_count == 3: + # 3 point connector, rotated correctly + cls.IMG = os.path.join('tunnel', 'tunnel_3way') + # find the missing connection. + for x, angle in zip(connections, (0, 180, 270, 90)): + if not x: + cls.ANGLE = angle + break + elif conn_count == 2: # Need to distinguish pass-through or corner, and # rotate correctly - pass - cls.IMG = os.path.join('tunnel', 'tunnel_none') + # neighbors is left, right then up, down + if connections[0] == connections[1]: + cls.IMG = os.path.join('tunnel', 'tunnel_passthrough') + if connections[0]: + cls.ANGLE = 90 + else: + cls.IMG = os.path.join('tunnel', 'tunnel_corner') + if connections[0]: + if connections[2]: + # left, up + cls.ANGLE = 90 + else: + # left, down + cls.ANGLE = 180 + else: + if connections[2]: + # right, up + cls.ANGLE = 0 + else: + # right, down + cls.ANGLE = 270 + return super(Tunnel, cls).image(neighbors) -- 2.34.1