Merge branch 'master' into map_generation
# Conflicts: # squirrelbattle/interfaces.py
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
from enum import Enum, auto
|
||||
from math import sqrt
|
||||
from random import choice, randint
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Any
|
||||
from itertools import product
|
||||
|
||||
from .display.texturepack import TexturePack
|
||||
@ -69,7 +69,8 @@ class Map:
|
||||
"""
|
||||
Unregister an entity from the map.
|
||||
"""
|
||||
self.entities.remove(entity)
|
||||
if entity in self.entities:
|
||||
self.entities.remove(entity)
|
||||
|
||||
def find_entities(self, entity_class: type) -> list:
|
||||
return [entity for entity in self.entities
|
||||
@ -77,12 +78,21 @@ class Map:
|
||||
|
||||
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 \
|
||||
self.tiles[y][x].can_walk() and \
|
||||
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
|
||||
def load(filename: str) -> "Map":
|
||||
"""
|
||||
@ -128,7 +138,7 @@ class Map:
|
||||
|
||||
def spawn_random_entities(self, count: int) -> None:
|
||||
"""
|
||||
Put randomly {count} hedgehogs on the map, where it is available.
|
||||
Put randomly {count} entities on the map, where it is available.
|
||||
"""
|
||||
for ignored in range(count):
|
||||
y, x = 0, 0
|
||||
@ -328,20 +338,34 @@ class Entity:
|
||||
from squirrelbattle.entities.items import Item
|
||||
return isinstance(self, Item)
|
||||
|
||||
def is_friendly(self) -> bool:
|
||||
"""
|
||||
Is this entity a friendly entity?
|
||||
"""
|
||||
return isinstance(self, FriendlyEntity)
|
||||
|
||||
def is_merchant(self) -> bool:
|
||||
"""
|
||||
Is this entity a merchant?
|
||||
"""
|
||||
from squirrelbattle.entities.friendly import Merchant
|
||||
return isinstance(self, Merchant)
|
||||
|
||||
@property
|
||||
def translated_name(self) -> str:
|
||||
return _(self.name.replace("_", " "))
|
||||
|
||||
@staticmethod
|
||||
def get_all_entity_classes():
|
||||
def get_all_entity_classes() -> list:
|
||||
"""
|
||||
Returns all entities subclasses
|
||||
"""
|
||||
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
|
||||
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
|
||||
Rabbit, TeddyBear
|
||||
return [BodySnatchPotion, Bomb, Heart, Hedgehog,
|
||||
Rabbit, TeddyBear, Tiger]
|
||||
from squirrelbattle.entities.friendly import Merchant, Sunflower
|
||||
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear,
|
||||
Sunflower, Tiger, Merchant]
|
||||
|
||||
@staticmethod
|
||||
def get_all_entity_classes_in_a_dict() -> dict:
|
||||
@ -351,7 +375,9 @@ class Entity:
|
||||
from squirrelbattle.entities.player import Player
|
||||
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
|
||||
TeddyBear
|
||||
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
|
||||
from squirrelbattle.entities.friendly import Merchant, Sunflower
|
||||
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, \
|
||||
Heart, Sword
|
||||
return {
|
||||
"Tiger": Tiger,
|
||||
"Bomb": Bomb,
|
||||
@ -361,6 +387,9 @@ class Entity:
|
||||
"Rabbit": Rabbit,
|
||||
"TeddyBear": TeddyBear,
|
||||
"Player": Player,
|
||||
"Merchant": Merchant,
|
||||
"Sunflower": Sunflower,
|
||||
"Sword": Sword,
|
||||
}
|
||||
|
||||
def save_state(self) -> dict:
|
||||
@ -436,7 +465,7 @@ class FightingEntity(Entity):
|
||||
|
||||
def keys(self) -> list:
|
||||
"""
|
||||
Returns a fighting entities specific attributes
|
||||
Returns a fighting entity's specific attributes
|
||||
"""
|
||||
return ["name", "maxhealth", "health", "level", "strength",
|
||||
"intelligence", "charisma", "dexterity", "constitution"]
|
||||
@ -449,3 +478,74 @@ class FightingEntity(Entity):
|
||||
for name in self.keys():
|
||||
d[name] = getattr(self, name)
|
||||
return d
|
||||
|
||||
|
||||
class FriendlyEntity(FightingEntity):
|
||||
"""
|
||||
Friendly entities are living entities which do not attack the player
|
||||
"""
|
||||
dialogue_option: list
|
||||
|
||||
def talk_to(self, player: Any) -> str:
|
||||
a = randint(0, len(self.dialogue_option) - 1)
|
||||
return "The " + self.translated_name \
|
||||
+ " said : " + self.dialogue_option[a]
|
||||
|
||||
def keys(self) -> list:
|
||||
"""
|
||||
Returns a friendly entity's specific attributes
|
||||
"""
|
||||
return ["maxhealth", "health"]
|
||||
|
||||
|
||||
class InventoryHolder(Entity):
|
||||
hazel: int # Currency of the game
|
||||
inventory: list
|
||||
|
||||
def translate_inventory(self, inventory: list) -> list:
|
||||
"""
|
||||
Translate the JSON-state of the inventory into a list of the items in
|
||||
the inventory.
|
||||
"""
|
||||
for i in range(len(inventory)):
|
||||
if isinstance(inventory[i], dict):
|
||||
inventory[i] = self.dict_to_inventory(inventory[i])
|
||||
return inventory
|
||||
|
||||
def dict_to_inventory(self, item_dict: dict) -> Entity:
|
||||
"""
|
||||
Translate a dict object that contains the state of an item
|
||||
into an item object.
|
||||
"""
|
||||
entity_classes = self.get_all_entity_classes_in_a_dict()
|
||||
|
||||
item_class = entity_classes[item_dict["type"]]
|
||||
return item_class(**item_dict)
|
||||
|
||||
def save_state(self) -> dict:
|
||||
"""
|
||||
We save the inventory of the merchant formatted as JSON
|
||||
"""
|
||||
d = super().save_state()
|
||||
d["hazel"] = self.hazel
|
||||
d["inventory"] = [item.save_state() for item in self.inventory]
|
||||
return d
|
||||
|
||||
def add_to_inventory(self, obj: Any) -> None:
|
||||
"""
|
||||
Adds an object to inventory
|
||||
"""
|
||||
self.inventory.append(obj)
|
||||
|
||||
def remove_from_inventory(self, obj: Any) -> None:
|
||||
"""
|
||||
Removes an object from the inventory
|
||||
"""
|
||||
self.inventory.remove(obj)
|
||||
|
||||
def change_hazel_balance(self, hz: int) -> None:
|
||||
"""
|
||||
Change the number of hazel the entity has by hz. hz is negative
|
||||
when the player loses money and positive when he gains money
|
||||
"""
|
||||
self.hazel += hz
|
||||
|
Reference in New Issue
Block a user