Refactor Action into Action and ActionWithActor
The base class Actor doesn't declare a (optional) actor attribute. The ActionWithActor has a non-optional actor attribute. This makes the type checker happier, and means we can have some actions that don't have actors.
This commit is contained in:
parent
8efd3ce207
commit
7e00f58a40
5 changed files with 72 additions and 39 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ..object import Actor
|
||||
from .result import ActionResult
|
||||
|
|
@ -8,12 +8,11 @@ from .result import ActionResult
|
|||
if TYPE_CHECKING:
|
||||
from ..engine import Engine
|
||||
|
||||
|
||||
class Action:
|
||||
'''An action that an Entity should perform.'''
|
||||
|
||||
def __init__(self, actor: Optional[Actor] = None):
|
||||
self.actor = actor
|
||||
'''An action with no specific actor'''
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def perform(self, engine: 'Engine') -> ActionResult:
|
||||
'''Perform this action.
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ class Action:
|
|||
A result object reflecting how the action was handled, and what follow-up actions, if any, are needed to
|
||||
complete the action.
|
||||
'''
|
||||
raise NotImplementedError()
|
||||
return self.success()
|
||||
|
||||
def failure(self) -> ActionResult:
|
||||
'''Create an ActionResult indicating failure with no follow-up'''
|
||||
|
|
@ -38,6 +37,20 @@ class Action:
|
|||
'''Create an ActionResult indicating success with no follow-up'''
|
||||
return ActionResult(self, success=True)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__class__.__name__
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}()'
|
||||
|
||||
|
||||
class ActionWithActor(Action):
|
||||
'''An action that assigned to an actor'''
|
||||
|
||||
def __init__(self, actor: Actor):
|
||||
super().__init__()
|
||||
self.actor = actor
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f'{self.__class__.__name__} for {self.actor!s}'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue