Friendly entities are now a subclass of fighting entities, and can die. The T key is now used to talk to friendly entities

This commit is contained in:
eichhornchen 2020-12-04 00:27:25 +01:00
parent 654bab7c1d
commit 3886bee1ba
5 changed files with 57 additions and 16 deletions

View File

@ -11,6 +11,12 @@ class Merchant(FriendlyEntity) :
inventory = list inventory = list
hazel = int hazel = int
def keys(self) -> list :
"""
Returns a friendly entitie's specific attributes
"""
return ["maxhealth", "health", "inventory", "hazel"]
def __init__(self, inventory : list, hazel : int = 75): def __init__(self, inventory : list, hazel : int = 75):
super().__init__() super().__init__()
self.inventory = inventory self.inventory = inventory
@ -23,7 +29,6 @@ class Merchant(FriendlyEntity) :
""" """
# TODO # TODO
class Sunflower(FriendlyEntity) : class Sunflower(FriendlyEntity) :
""" """
A friendly sunflower A friendly sunflower

View File

@ -84,7 +84,10 @@ class Player(FightingEntity):
elif entity.is_item(): elif entity.is_item():
entity.hold(self) entity.hold(self)
elif entity.is_friendly(): elif entity.is_friendly():
self.map.logs.add_message(entity.talk_to(self)) # self.map.logs.add_message(entity.talk_to(self))
self.map.logs.add_message(self.hit(entity))
if entity.dead:
self.add_xp(randint(3, 7))
return super().check_move(y, x, move_if_possible) return super().check_move(y, x, move_if_possible)
def recalculate_paths(self, max_distance: int = 8) -> None: def recalculate_paths(self, max_distance: int = 8) -> None:

View File

@ -38,6 +38,7 @@ class KeyValues(Enum):
RIGHT = auto() RIGHT = auto()
ENTER = auto() ENTER = auto()
SPACE = auto() SPACE = auto()
T = auto()
@staticmethod @staticmethod
def translate_key(key: str, settings: Settings) -> Optional["KeyValues"]: def translate_key(key: str, settings: Settings) -> Optional["KeyValues"]:
@ -60,4 +61,6 @@ class KeyValues(Enum):
return KeyValues.ENTER return KeyValues.ENTER
elif key == ' ': elif key == ' ':
return KeyValues.SPACE return KeyValues.SPACE
elif key == 't':
return KeyValues.T
return None return None

View File

@ -24,6 +24,7 @@ class Game:
""" """
map: Map map: Map
player: Player player: Player
screen: Any
# display_actions is a display interface set by the bootstrapper # display_actions is a display interface set by the bootstrapper
display_actions: Callable[[DisplayActions], None] display_actions: Callable[[DisplayActions], None]
@ -61,6 +62,7 @@ class Game:
We wait for the player's action, then we do what that should be done We wait for the player's action, then we do what that should be done
when the given key gets pressed. when the given key gets pressed.
""" """
self.screen = screen
while True: # pragma no cover while True: # pragma no cover
screen.erase() screen.erase()
screen.refresh() screen.refresh()
@ -106,6 +108,30 @@ class Game:
self.map.tick() self.map.tick()
elif key == KeyValues.SPACE: elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
elif key == KeyValues.T :
keykey = self.screen.getkey()
keykey = KeyValues.translate_key(keykey, self.settings)
if keykey == KeyValues.UP:
xp = self.player.x
yp = self.player.y+1
elif keykey == KeyValues.DOWN:
xp = self.player.x
yp = self.player.y-1
elif keykey == KeyValues.LEFT:
xp = self.player.x-1
yp = self.player.y
elif keykey == KeyValues.RIGHT:
xp = self.player.x+1
yp = self.player.y
else :
raise Exception(keykey)
if self.map.entity_is_present(yp, xp) :
for entity in self.map.entities :
if entity.is_friendly() and entity.x == xp and entity.y == yp :
msg = entity.talk_to(self.player)
self.logs.add_message(msg)
def handle_key_pressed_main_menu(self, key: KeyValues) -> None: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
""" """

View File

@ -76,12 +76,20 @@ class Map:
def is_free(self, y: int, x: int) -> bool: def is_free(self, y: int, x: int) -> bool:
""" """
Indicates that the case at the coordinates (y, x) is empty. Indicates that the tile at the coordinates (y, x) is empty.
""" """
return 0 <= y < self.height and 0 <= x < self.width and \ return 0 <= y < self.height and 0 <= x < self.width and \
self.tiles[y][x].can_walk() and \ self.tiles[y][x].can_walk() and \
not any(entity.x == x and entity.y == y for entity in self.entities) not any(entity.x == x and entity.y == y for entity in self.entities)
def entity_is_present(self, y: int, x: int) -> bool:
"""
Indicates that the tile at the coordinates (y, x) contains a killable entity
"""
return 0 <= y < self.height and 0 <= x < self.width and \
any(entity.x == x and entity.y == y and \
entity.is_friendly() for entity in self.entities)
@staticmethod @staticmethod
def load(filename: str) -> "Map": def load(filename: str) -> "Map":
""" """
@ -431,7 +439,7 @@ class FightingEntity(Entity):
def keys(self) -> list: def keys(self) -> list:
""" """
Returns a fighting entities specific attributes Returns a fighting entity's specific attributes
""" """
return ["maxhealth", "health", "level", "strength", return ["maxhealth", "health", "level", "strength",
"intelligence", "charisma", "dexterity", "constitution"] "intelligence", "charisma", "dexterity", "constitution"]
@ -445,22 +453,18 @@ class FightingEntity(Entity):
d[name] = getattr(self, name) d[name] = getattr(self, name)
return d return d
class FriendlyEntity(Entity): class FriendlyEntity(FightingEntity):
""" """
Friendly entities are living entities which do not attack the player Friendly entities are living entities which do not attack the player
""" """
maxhealth: int
health: int #Friendly entities can be killed
dialogue_option : list dialogue_option : list
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
*args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.maxhealth = maxhealth
self.health = maxhealth if health is None else health
def talk_to(self, player : Any) -> str: def talk_to(self, player : Any) -> str:
a = randint(0,len(self.dialogue_option)-1) a = randint(0,len(self.dialogue_option)-1)
return "The sunflower said : "+self.dialogue_option[a] return "The "+self.name+" said : "+self.dialogue_option[a]
def keys(self) -> list :
"""
Returns a friendly entity's specific attributes
"""
return ["maxhealth", "health", "dialogue_option"]