Navigate through different maps while climbing ladders

This commit is contained in:
Yohann D'ANELLO 2020-12-26 01:08:43 +01:00
parent 8636d571b5
commit 9a56b4d7e9
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 30 additions and 9 deletions

View File

@ -1,8 +1,8 @@
1 6
####### #############
#.H...# #...........#
#.H...# #...........#
#.....# #####...........#
#.....# #............H..#
#.....# #............H..#
#.##### #.###...........#
#.# #.# #...........#
#.# #.# #############

View File

@ -1,6 +1,6 @@
1 17
########### #########
#....H....# #.......#
#....H....# #.......#
#.........# ############.......#
#.........###############..........#.......##############
#.........#........................#....................#
@ -13,7 +13,7 @@
########.##########......# #.........# #.........#
#...........##......# #.........# #.........#
#...........##......# #.........# #.........#
#...........##..H...# #.........# ################.######
#...........##..H...# #.........# ################.######
#...........##......# #.........# #.................############
#...........##......# ########.########.......#.........#..........#
#...........##......# #...............#.......#.........#..........#

View File

@ -55,7 +55,7 @@ class Game:
# TODO generate a new map procedurally
self.maps = []
self.map_index = 0
self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt"))
self.map = Map.load(ResourceManager.get_asset_path("example_map.txt"))
self.map.logs = self.logs
self.logs.clear()
self.player = Player()
@ -102,6 +102,24 @@ class Game:
self.handle_key_pressed(
KeyValues.translate_key(key, self.settings), key)
# FIXME This code should not be there, but rather in Map.tick
y, x = self.player.y, self.player.x
if self.map.tiles[y][x].is_ladder():
# We move up on the ladder of the beginning,
# down at the end of the stage
move_down = y != self.map.start_y and x != self.map.start_x
old_map = self.map
self.map_index += 1 if move_down else -1
self.map_index = max(0, self.map_index)
while self.map_index >= len(self.maps):
self.maps.append(Map.load(ResourceManager.get_asset_path(
"example_map_2.txt")))
new_map = self.map
old_map.remove_entity(self.player)
new_map.add_entity(self.player)
self.player.move(self.map.start_y, self.map.start_x)
self.display_actions(DisplayActions.UPDATE)
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\
-> None:
"""

View File

@ -140,12 +140,15 @@ class Map:
Put randomly {count} entities on the map, where it is available.
"""
for ignored in range(count):
y, x = 0, 0
while True:
y, x = randint(0, self.height - 1), randint(0, self.width - 1)
tile = self.tiles[y][x]
if tile.can_walk():
break
try:
tile = self.tiles[y][x]
except Exception as e:
raise Exception(y, x, len(self.tiles))
else:
if tile.can_walk():
break
entity = choice(Entity.get_all_entity_classes())()
entity.move(y, x)
self.add_entity(entity)