Fixed grammar, unified the docstring's format and added documentation to some classes that did not have any. Closes #32.

This commit is contained in:
eichhornchen
2020-12-13 21:29:25 +01:00
parent 3f62fbaa2b
commit 646e0063be
24 changed files with 254 additions and 170 deletions

View File

@ -12,7 +12,7 @@ from .translations import gettext as _
class Logs:
"""
The logs object stores the messages to display. It is encapsulating a list
The logs object stores the messages to display. It encapsulates a list
of such messages, to allow multiple pointers to keep track of it even if
the list was to be reassigned.
"""
@ -32,7 +32,7 @@ class Logs:
class Map:
"""
Object that represents a Map with its width, height
The Map object represents a with its width, height
and tiles, that have their custom properties.
"""
width: int
@ -59,14 +59,14 @@ class Map:
def add_entity(self, entity: "Entity") -> None:
"""
Register a new entity in the map.
Registers a new entity in the map.
"""
self.entities.append(entity)
entity.map = self
def remove_entity(self, entity: "Entity") -> None:
"""
Unregister an entity from the map.
Unregisters an entity from the map.
"""
if entity in self.entities:
self.entities.remove(entity)
@ -86,7 +86,7 @@ class Map:
def entity_is_present(self, y: int, x: int) -> bool:
"""
Indicates that the tile at the coordinates (y, x) contains a killable
entity
entity.
"""
return 0 <= y < self.height and 0 <= x < self.width and \
any(entity.x == x and entity.y == y and entity.is_friendly()
@ -95,7 +95,7 @@ class Map:
@staticmethod
def load(filename: str) -> "Map":
"""
Read a file that contains the content of a map, and build a Map object.
Reads a file that contains the content of a map, and builds a Map object.
"""
with open(filename, "r") as f:
file = f.read()
@ -104,7 +104,7 @@ class Map:
@staticmethod
def load_from_string(content: str) -> "Map":
"""
Load a map represented by its characters and build a Map object.
Loads a map represented by its characters and builds a Map object.
"""
lines = content.split("\n")
first_line = lines[0]
@ -120,7 +120,7 @@ class Map:
@staticmethod
def load_dungeon_from_string(content: str) -> List[List["Tile"]]:
"""
Transforms a string into the list of corresponding tiles
Transforms a string into the list of corresponding tiles.
"""
lines = content.split("\n")
tiles = [[Tile.from_ascii_char(c)
@ -129,7 +129,7 @@ class Map:
def draw_string(self, pack: TexturePack) -> str:
"""
Draw the current map as a string object that can be rendered
Draws the current map as a string object that can be rendered
in the window.
"""
return "\n".join("".join(tile.char(pack) for tile in line)
@ -137,7 +137,7 @@ class Map:
def spawn_random_entities(self, count: int) -> None:
"""
Put randomly {count} entities on the map, where it is available.
Puts randomly {count} entities on the map, only on empty ground tiles.
"""
for ignored in range(count):
y, x = 0, 0
@ -152,14 +152,14 @@ class Map:
def tick(self) -> None:
"""
Trigger all entity events.
Triggers all entity events.
"""
for entity in self.entities:
entity.act(self)
def save_state(self) -> dict:
"""
Saves the map's attributes to a dictionary
Saves the map's attributes to a dictionary.
"""
d = dict()
d["width"] = self.width
@ -176,7 +176,7 @@ class Map:
def load_state(self, d: dict) -> None:
"""
Loads the map's attributes from a dictionary
Loads the map's attributes from a dictionary.
"""
self.width = d["width"]
self.height = d["height"]
@ -193,7 +193,7 @@ class Map:
class Tile(Enum):
"""
The internal representation of the tiles of the map
The internal representation of the tiles of the map.
"""
EMPTY = auto()
WALL = auto()
@ -202,7 +202,7 @@ class Tile(Enum):
@staticmethod
def from_ascii_char(ch: str) -> "Tile":
"""
Maps an ascii character to its equivalent in the texture pack
Maps an ascii character to its equivalent in the texture pack.
"""
for tile in Tile:
if tile.char(TexturePack.ASCII_PACK) == ch:
@ -212,7 +212,7 @@ class Tile(Enum):
def char(self, pack: TexturePack) -> str:
"""
Translates a Tile to the corresponding character according
to the texture pack
to the texture pack.
"""
return getattr(pack, self.name)
@ -224,14 +224,14 @@ class Tile(Enum):
def can_walk(self) -> bool:
"""
Check if an entity (player or not) can move in this tile.
Checks if an entity (player or not) can move in this tile.
"""
return not self.is_wall() and self != Tile.EMPTY
class Entity:
"""
An Entity object represents any entity present on the map
An Entity object represents any entity present on the map.
"""
y: int
x: int
@ -249,7 +249,7 @@ class Entity:
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
-> bool:
"""
Checks if moving to (y,x) is authorized
Checks if moving to (y,x) is authorized.
"""
free = self.map.is_free(y, x)
if free and move_if_possible:
@ -258,7 +258,7 @@ class Entity:
def move(self, y: int, x: int) -> bool:
"""
Moves an entity to (y,x) coordinates
Moves an entity to (y,x) coordinates.
"""
self.y = y
self.x = x
@ -266,49 +266,49 @@ class Entity:
def move_up(self, force: bool = False) -> bool:
"""
Moves the entity up one tile, if possible
Moves the entity up one tile, if possible.
"""
return self.move(self.y - 1, self.x) if force else \
self.check_move(self.y - 1, self.x, True)
def move_down(self, force: bool = False) -> bool:
"""
Moves the entity down one tile, if possible
Moves the entity down one tile, if possible.
"""
return self.move(self.y + 1, self.x) if force else \
self.check_move(self.y + 1, self.x, True)
def move_left(self, force: bool = False) -> bool:
"""
Moves the entity left one tile, if possible
Moves the entity left one tile, if possible.
"""
return self.move(self.y, self.x - 1) if force else \
self.check_move(self.y, self.x - 1, True)
def move_right(self, force: bool = False) -> bool:
"""
Moves the entity right one tile, if possible
Moves the entity right one tile, if possible.
"""
return self.move(self.y, self.x + 1) if force else \
self.check_move(self.y, self.x + 1, True)
def act(self, m: Map) -> None:
"""
Define the action of the entity that is ran each tick.
Defines the action the entity will do at each tick.
By default, does nothing.
"""
pass
def distance_squared(self, other: "Entity") -> int:
"""
Get the square of the distance to another entity.
Useful to check distances since square root takes time.
Gives the square of the distance to another entity.
Useful to check distances since taking the square root takes time.
"""
return (self.y - other.y) ** 2 + (self.x - other.x) ** 2
def distance(self, other: "Entity") -> float:
"""
Get the cartesian distance to another entity.
Gives the cartesian distance to another entity.
"""
return sqrt(self.distance_squared(other))
@ -340,12 +340,15 @@ class Entity:
@property
def translated_name(self) -> str:
"""
Translates the name of entities.
"""
return _(self.name.replace("_", " "))
@staticmethod
def get_all_entity_classes() -> list:
"""
Returns all entities subclasses
Returns all entities subclasses.
"""
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
@ -357,7 +360,7 @@ class Entity:
@staticmethod
def get_all_entity_classes_in_a_dict() -> dict:
"""
Returns all entities subclasses in a dictionary
Returns all entities subclasses in a dictionary.
"""
from squirrelbattle.entities.player import Player
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
@ -381,7 +384,7 @@ class Entity:
def save_state(self) -> dict:
"""
Saves the coordinates of the entity
Saves the coordinates of the entity.
"""
d = dict()
d["x"] = self.x
@ -393,7 +396,7 @@ class Entity:
class FightingEntity(Entity):
"""
A FightingEntity is an entity that can fight, and thus has a health,
level and stats
level and stats.
"""
maxhealth: int
health: int
@ -420,11 +423,15 @@ class FightingEntity(Entity):
@property
def dead(self) -> bool:
"""
Is this entity dead ?
"""
return self.health <= 0
def hit(self, opponent: "FightingEntity") -> str:
"""
Deals damage to the opponent, based on the stats
The entity deals damage to the opponent
based on their respective stats.
"""
return _("{name} hits {opponent}.")\
.format(name=_(self.translated_name.capitalize()),
@ -433,7 +440,8 @@ class FightingEntity(Entity):
def take_damage(self, attacker: "Entity", amount: int) -> str:
"""
Take damage from the attacker, based on the stats
The entity takes damage from the attacker
based on their respective stats.
"""
self.health -= amount
if self.health <= 0:
@ -446,20 +454,20 @@ class FightingEntity(Entity):
def die(self) -> None:
"""
If a fighting entity has no more health, it dies and is removed
If a fighting entity has no more health, it dies and is removed.
"""
self.map.remove_entity(self)
def keys(self) -> list:
"""
Returns a fighting entity's specific attributes
Returns a fighting entity's specific attributes.
"""
return ["name", "maxhealth", "health", "level", "strength",
"intelligence", "charisma", "dexterity", "constitution"]
def save_state(self) -> dict:
"""
Saves the state of the entity into a dictionary
Saves the state of the entity into a dictionary.
"""
d = super().save_state()
for name in self.keys():
@ -469,7 +477,7 @@ class FightingEntity(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.
"""
dialogue_option: list
@ -480,7 +488,7 @@ class FriendlyEntity(FightingEntity):
def keys(self) -> list:
"""
Returns a friendly entity's specific attributes
Returns a friendly entity's specific attributes.
"""
return ["maxhealth", "health"]
@ -491,7 +499,7 @@ class InventoryHolder(Entity):
def translate_inventory(self, inventory: list) -> list:
"""
Translate the JSON-state of the inventory into a list of the items in
Translates the JSON save of the inventory into a list of the items in
the inventory.
"""
for i in range(len(inventory)):
@ -501,7 +509,7 @@ class InventoryHolder(Entity):
def dict_to_inventory(self, item_dict: dict) -> Entity:
"""
Translate a dict object that contains the state of an item
Translates a dictionnary that contains the state of an item
into an item object.
"""
entity_classes = self.get_all_entity_classes_in_a_dict()
@ -511,7 +519,7 @@ class InventoryHolder(Entity):
def save_state(self) -> dict:
"""
We save the inventory of the merchant formatted as JSON
The inventory of the merchant is saved in a JSON format.
"""
d = super().save_state()
d["hazel"] = self.hazel
@ -520,19 +528,19 @@ class InventoryHolder(Entity):
def add_to_inventory(self, obj: Any) -> None:
"""
Adds an object to inventory
Adds an object to the inventory.
"""
self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None:
"""
Removes an object from the inventory
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
Changes the number of hazel the entity has by hz. hz is negative
when the entity loses money and positive when it gains money.
"""
self.hazel += hz