22
33from typing import Optional , Tuple , TYPE_CHECKING
44
5+ import color
6+
57if TYPE_CHECKING :
68 from engine import Engine
7- from entity import Entity
9+ from entity import Actor , Entity
810
911
1012class Action :
11- def __init__ (self , entity : Entity ) -> None :
13+ def __init__ (self , entity : Actor ) -> None :
1214 super ().__init__ ()
1315 self .entity = entity
1416
@@ -29,7 +31,7 @@ def perform(self) -> None:
2931 raise NotImplementedError ()
3032
3133class ActionWithDirection (Action ):
32- def __init__ (self , entity : Entity , dx : int , dy : int ):
34+ def __init__ (self , entity : Actor , dx : int , dy : int ):
3335 super ().__init__ (entity )
3436
3537 self .dx = dx
@@ -43,6 +45,11 @@ def dest_xy(self) -> Tuple[int, int]:
4345 """Returns this actions destination."""
4446 return self .entity .x + self .dx , self .entity .y + self .dy
4547
48+ @property
49+ def target_actor (self ) -> Optional [Actor ]:
50+ """Return the actor at this actions destination."""
51+ return self .engine .game_map .get_actor_at_location (* self .dest_xy )
52+
4653 @property
4754 def blocking_entity (self ) -> Optional [Entity ]:
4855 """Return the blocking entity at this actions destination.."""
@@ -55,11 +62,28 @@ def perform(self) -> None:
5562
5663class MeleeAction (ActionWithDirection ):
5764 def perform (self ) -> None :
58- target = self .blocking_entity
65+ target = self .target_actor
5966 if not target :
6067 return # No entity to attack.
6168
62- print (f"You punch the { target .name } , it does not seem to be doing much damage..." )
69+ damage = self .entity .fighter .power - target .fighter .defense
70+
71+ attack_desc = f"{ self .entity .name .capitalize ()} attacks { target .name .capitalize ()} "
72+ if self .entity is self .engine .player :
73+ attack_color = color .player_atk
74+ else :
75+ attack_color = color .enemy_atk
76+ if damage > 0 :
77+ self .engine .message_log .add_message (
78+ f"{ attack_desc } for { damage } hit points!" ,
79+ attack_color
80+ )
81+ target .fighter .hp -= damage
82+ else :
83+ self .engine .message_log .add_message (
84+ f"{ attack_desc } , but does no damage." ,
85+ attack_color
86+ )
6387
6488class MovementAction (ActionWithDirection ):
6589 def perform (self ) -> None :
@@ -78,8 +102,12 @@ def perform(self) -> None:
78102
79103class BumpAction (ActionWithDirection ):
80104 def perform (self ) -> None :
81- if self .blocking_entity is not None :
105+ if self .target_actor :
82106 return MeleeAction (self .entity , self .dx , self .dy ).perform ()
83107
84108 else :
85- return MovementAction (self .entity , self .dx , self .dy ).perform ()
109+ return MovementAction (self .entity , self .dx , self .dy ).perform ()
110+
111+ class WaitAction (Action ):
112+ def perform (self ) -> None :
113+ pass
0 commit comments