Address some linter issues; add doc strings

- Clean up some import ordering
- Write some Numpy style doc strings for classes and functions
This commit is contained in:
Eryn Wells 2022-05-07 08:55:10 -07:00
parent f3d5e273db
commit 7720bc525a
3 changed files with 56 additions and 10 deletions

View file

@ -3,7 +3,7 @@
import logging import logging
from .geometry import Direction from .geometry import Direction
from typing import TYPE_CHECKING from typing import Optional, TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from .engine import Engine from .engine import Engine
@ -11,11 +11,41 @@ if TYPE_CHECKING:
LOG = logging.getLogger('events') LOG = logging.getLogger('events')
class ActionResult:
'''An object that represents the result of an Action.
Attributes
----------
success : bool
True if the action succeeded
done : bool
True if the action is complete, and no follow-up action is needed.
alternate : Action, optional
An alternate action to perform if this action failed
'''
def __init__(self, success: bool, done: bool = True, alternate: Optional['Action'] = None):
self.success = success
self.done = done
self.alternate = alternate
class Action: class Action:
def perform(self, engine: 'Engine', entity: 'Entity') -> None: def perform(self, engine: 'Engine', entity: 'Entity') -> ActionResult:
''' '''Perform this action.
Perform this action. This is an abstract method that all subclasses
should implement. Parameters
----------
engine : Engine
The game engine
entity : Entity
The entity that this action is being performed on
Returns
-------
ActionResult
A result object reflecting how the action was handled, and what follow-up actions, if any, are needed to
complete the action.
''' '''
raise NotImplementedError() raise NotImplementedError()
@ -23,12 +53,12 @@ class Action:
return f'{self.__class__.__name__}()' return f'{self.__class__.__name__}()'
class ExitAction(Action): class ExitAction(Action):
def perform(self, engine: 'Engine', entity: 'Entity') -> None: def perform(self, engine: 'Engine', entity: 'Entity') -> ActionResult:
raise SystemExit() raise SystemExit()
class RegenerateRoomsAction(Action): class RegenerateRoomsAction(Action):
def perform(self, engine: 'Engine', entity: 'Entity') -> None: def perform(self, engine: 'Engine', entity: 'Entity') -> ActionResult:
... return ActionResult(True)
class MovePlayerAction(Action): class MovePlayerAction(Action):
def __init__(self, direction: Direction): def __init__(self, direction: Direction):

View file

@ -1,17 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me> # Eryn Wells <eryn@erynwells.me>
'''Defines the core game engine.'''
import logging import logging
import random import random
from dataclasses import dataclass
from typing import MutableSet
import tcod import tcod
from . import monsters from . import monsters
from .events import EventHandler from .events import EventHandler
from .geometry import Direction, Size from .geometry import Direction, Size
from .map import Map from .map import Map
from .monsters import Monster from .monsters import Monster
from .object import Entity, Hero from .object import Entity, Hero
from dataclasses import dataclass
from typing import MutableSet
LOG = logging.getLogger('engine') LOG = logging.getLogger('engine')
EVENT_LOG = logging.getLogger('events') EVENT_LOG = logging.getLogger('events')
@ -82,6 +86,7 @@ class Engine:
self.update_field_of_view() self.update_field_of_view()
def print_to_console(self, console): def print_to_console(self, console):
'''Print the whole game to the given console.'''
self.map.print_to_console(console) self.map.print_to_console(console)
for ent in self.entities: for ent in self.entities:

View file

@ -205,6 +205,7 @@ class RoomsAndCorridorsGenerator(MapGenerator):
return tiles return tiles
def __rect_from_bsp_node(self, node: tcod.bsp.BSP) -> Rect: def __rect_from_bsp_node(self, node: tcod.bsp.BSP) -> Rect:
'''Create a Rect from the given BSP node object'''
return Rect(Point(node.x, node.y), Size(node.width, node.height)) return Rect(Point(node.x, node.y), Size(node.width, node.height))
class Room: class Room:
@ -215,11 +216,21 @@ class Room:
raise NotImplementedError() raise NotImplementedError()
class RectangularRoom(Room): class RectangularRoom(Room):
'''A rectangular room defined by a Rect.
Attributes
----------
bounds : Rect
A rectangle that defines the room. This rectangle includes the tiles used for the walls, so the floor is 1 tile
inset from the bounds.
'''
def __init__(self, bounds: Rect): def __init__(self, bounds: Rect):
self.bounds = bounds self.bounds = bounds
@property @property
def center(self) -> Point: def center(self) -> Point:
'''The center of the room, truncated according to integer math rules'''
return self.bounds.midpoint return self.bounds.midpoint
@property @property