-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
153 lines (116 loc) · 5.1 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from __future__ import annotations
from numpy import invert
import color
import exceptions
from typing import Optional, Tuple, TYPE_CHECKING
if TYPE_CHECKING:
from Engine import Engine
from Entity import Actor,Entity, Item
class Action:
def __init__(self, entity: Actor) -> None:
super().__init__()
self.entity = entity
@property
def engine(self) -> Engine:
"""Return the engine this actio nbelangs to."""
return self.entity.gamemap.engine
def perform(self) -> None:
"""
Perform this action with the objects needed to determine its scope
`self.engine` is the scope this action is being performed in.
`self.entity` is the object performing the action
this method must be overridden by Action subclasses
"""
raise NotImplementedError()
class EscapeAction(Action):
def perform(self) -> None:
raise SystemExit()
class WaitAction(Action):
def perform(self) -> None:
pass
class ActionWitghDirection(Action):
def __init__(self, entity: Actor, dx:int, dy: int):
super().__init__(entity)
self.dx = dx
self.dy = dy
@property
def dest_xy(self) -> Tuple[int,int]:
"""returns action destination"""
return self.entity.x + self.dx, self.entity.y + self.dy
@property
def blocking_entity(self) -> Optional[Entity]:
"""Return the blocking entity at this action destination.."""
return self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy)
@property
def target_actor(self) -> Optional[Actor]:
"""Return the actor at this actions destination"""
return self.engine.game_map.get_actor_at_location(*self.dest_xy)
def perform(self) -> None:
raise NotImplementedError()
class MovementAction(ActionWitghDirection):
def perform(self) -> None:
dest_x, dest_y = self.dest_xy
if not self.engine.game_map.in_bounds(dest_x, dest_y):
raise exceptions.Impossible("That way is blocked.")
if not self.engine.game_map.tiles["walkable"][dest_x,dest_y]:
raise exceptions.Impossible("That way is blocked.")
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
raise exceptions.Impossible("That way is blocked.") #destination is blocked by an entity
self.entity.move(self.dx, self.dy)
class MeleeAction(ActionWitghDirection):
def perform(self) -> None:
target = self.target_actor
if not target:
raise exceptions.Impossible("Nothing to attack")
if self.target_actor is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
damage = self.entity.fighter.power - target.fighter.defense
attack_desc = f"{self.entity.name.capitalize()} attacks {target.name}"
if damage > 0:
self.engine.message_log.add_message(f"{attack_desc} for {damage} hit points.", attack_color)
target.fighter.hp -= damage
else:
self.engine.message_log.add_message(f"{attack_desc} but does no damage.", attack_color)
class BumpAction(ActionWitghDirection):
def perform(self) -> None:
if self.target_actor:
return MeleeAction(self.entity,self.dx, self.dy).perform()
else:
return MovementAction(self.entity, self.dx, self.dy).perform()
class ItemAction(Action):
def __init__(self, entity: Actor, item: Item, target_xy: Optional[Tuple[int,int]] = None) -> None:
super().__init__(entity)
self.item = item
if not target_xy:
target_xy = entity.x, entity.y # self targeted
self.target_xy = target_xy
@property
def target_actor(self) -> Optional[Actor]:
"""Return the actor at this actiosn destination."""
return self.engine.game_map.get_actor_at_location(*self.target_xy)
def perform(self) -> None:
"""Invoke the items ability, this action will be given to provide context"""
self.item.consumable.activate(self)
class PickupAction(Action):
"""Pickup an item an dadd it to the inventory, if there is room for it"""
def __init__(self, entity: Actor) -> None:
super().__init__(entity)
def perform(self) -> None:
actor_location_x = self.entity.x
actor_location_y = self.entity.y
inventory = self.entity.inventory
for item in self.entity.gamemap.items:
if actor_location_x == item.x and actor_location_y == item.y:
if len(inventory.items) >= inventory.capacity:
raise exceptions.Impossible("Your inventory is full")
self.engine.game_map.entities.remove(item)
item.parent = self.entity.inventory
inventory.items.append(item)
self.engine.message_log.add_message(f"You picked up the {item.name}!")
return
raise exceptions.Impossible("There is nothing here to pick up")
class DropItem(ItemAction):
def perform(self) -> None:
self.entity.inventory.drop(self.item)