Locations: Add location connections and discoverability

When connecting a location, a new connection is appended.
Each connection holds a discovered flag.
Only discovered connections are show in the options menu.
When looking around, undiscovered connections are revealed
This commit is contained in:
2023-06-28 17:14:50 +02:00
parent d4e5378883
commit adaeac9ee9
2 changed files with 34 additions and 5 deletions

View File

@@ -17,9 +17,13 @@ class Game:
#ToDo: move this out of the main file
locations = {
"City": SafeZone(self, "Mayberry", "You find yourself in the small rural town of Mayberry. \nThe town is quaint and charming, with a population of only a few hundred people. \nIt is surrounded by a thick forest, and the only way to get here is on foot. \nThere are a few shops and businesses around."),
"Berrys": SafeZone(self, "Berry's Brews", "The tavern is abuzz with lively chatter and laughter, creating a warm and inviting atmosphere. The patrons, a diverse mix of locals and travelers, gather around wooden tables and cozy booths, each immersed in their own tales and mugs of frothy drinks."),
"Forest": Location(self, "the Forest", "You find yourself in a warm and lush oak forest. \nThe dense trees provide plenty of cover from the sun, and the forest floor is carpeted with a thick layer of leaves. \nYou hear the sound of birdsong all around you, and the occasional rustle of small animals in the undergrowth."),
}
locations["City"].connect_location(locations["Forest"])
locations["City"].connect_location(locations["Berrys"], hidden=True)
locations["Berrys"].connect_location(locations["City"])
locations["Forest"].connect_location(locations["City"])
self.current_location = locations["City"]
@@ -35,7 +39,7 @@ class Game:
Text.print_header("Welcome", HeaderStyle.ROUND)
Text.type(mt.intro_text, delay=0.01, newline_delay=1.0)
while not self.quit:
Menu("What would you like to do?", self.current_location.options).show()
Menu("What would you like to do?", self.current_location.get_options()).show()

View File

@@ -1,6 +1,7 @@
"""Generic Location"""
from utils.input import Option
from utils.text import HeaderStyle
from typing import List, Type
import utils.text as Text
import time
@@ -9,22 +10,38 @@ class Location:
super().__init__()
self.name = name
self.description = description
self.options = [Option("Look around", self.look_around)]
self.options: List[Option] = [Option("Look around", self.look_around)]
self.connections: List[Connection] = []
self.game = game
def look_around(self) -> None:
Text.print_header(self.name, HeaderStyle.ROUND)
Text.type(f"{self.description}\n", 0.01, 0.1)
for connection in self.connections:
if not connection.discovered:
connection.discover()
time.sleep(0.5)
def enter(self) -> None:
Text.print_header(self.name, HeaderStyle.ROUND)
print(f"You enter {self.name}.\n")
print(f"> You enter {self.name}.\n")
self.game.current_location = self
time.sleep(0.5)
def connect_location(self, location) -> None:
self.options.append(Option(f"Enter {location.name}", location.enter))
def connect_location(self, location, hidden: bool = False) -> None:
connection: Connection = Connection(location, not hidden)
self.connections.append(connection)
def get_options(self) -> List[Option]:
all_options = []
for connection in self.connections:
if connection.discovered:
all_options.append(Option(f'Enter {connection.target_location.name}', connection.target_location.enter))
all_options.extend(self.options)
return all_options
class SafeZone(Location):
def __init__(self, *args, **kwargs) -> None:
@@ -48,3 +65,11 @@ class Shop(Location):
def look_around(self) -> None:
print(f"You find yourself in a small shop called {self.name}.\n{self.description}")
class Connection:
def __init__(self, target_location: Location, discovered: bool = True) -> None:
self.target_location = target_location
self.discovered = discovered
def discover(self) -> None:
self.discovered = True
Text.type(f"> You discovered {self.target_location.name}")