-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions.py
More file actions
307 lines (240 loc) · 11.3 KB
/
Copy pathactions.py
File metadata and controls
307 lines (240 loc) · 11.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from __future__ import annotations
from typing import Optional, Tuple, TYPE_CHECKING
import color
from entity import Actor
import exceptions
import random
if TYPE_CHECKING:
from engine import Engine
from entity import Actor, Entity, Item, NPC
from status_effect import StatusEffect
class Action:
def __init__(self, entity: Actor) -> None:
super().__init__()
self.entity = entity
@property
def engine(self) -> Engine:
"""Return the engine this action belongs to."""
return self.entity.parent.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 ActionWithDirection(Action):
def __init__(self, entity: Actor, dx: int, dy: int):
super().__init__(entity)
self.dx = dx
self.dy = dy
def perform(self, engine: Engine, entity: Entity) -> None:
raise NotImplementedError()
@property
def dest_xy(self) -> Tuple[int, int]:
"""Returns this actions destination."""
return self.entity.x + self.dx, self.entity.y + self.dy
@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)
@property
def target_NPC(self) -> Optional[NPC]:
"""Return the NPC at this actions destination."""
return self.engine.game_map.get_NPC_at_location(*self.dest_xy)
@property
def blocking_entity(self) -> Optional[Entity]:
"""Return the blocking entity at this actions destination.."""
return self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy)
class ItemAction(Action):
def __init__(
self, entity: Actor, item: Item, target_xy: Optional[Tuple[int, int]] = None
):
super().__init__(entity)
self.item = item
if not target_xy:
target_xy = entity.x, entity.y
self.target_xy = target_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.target_xy)
def perform(self) -> None:
"""Invoke the items ability, this action will be given to provide context."""
if self.item.consumable:
self.item.consumable.activate(self)
class MeleeAction(ActionWithDirection):
def __init__(self, entity: Actor, dx: int, dy: int, effect: Optional[StatusEffect] = None):
super().__init__(entity, dx, dy)
self.effect = effect
def perform(self) -> None:
target = self.target_actor
if not target:
raise exceptions.Impossible("Nothing to attack.")
# Chance for the attacker to dodge, based on his dodge chance
roll = random.randint(1, 100)
if roll <= target.fighter.dodge:
self.engine.message_log.add_message(
f"{self.entity.name.capitalize()} Tries to attack, but {target.name.capitalize()} dodges the attack!",
color.status_effect_applied
)
return
damage = self.entity.fighter.power - target.fighter.defense
# If the attacker is the player and godmode is on, then the attacker will do max damage
if self.entity is self.engine.player and self.engine.game_world.godmode:
damage = 99999999
attack_desc = f"{self.entity.name.capitalize()} attacks {target.name.capitalize()}"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
if damage > 0:
# if the target is the player and godmode is on, then the target will take no damage
if target is self.engine.player and self.engine.game_world.godmode:
return
self.engine.message_log.add_message(
f"{attack_desc} for {damage} hit points!",
attack_color
)
target.fighter.hp -= damage
target.fighter.last_actor_hurt_by = self.entity.internal_name
# trigger any on_attack of the equipment of the attacker, if any.
if self.entity.equipment and self.entity.equipment.weapon:
self.entity.equipment.weapon.equippable.on_attack(target)
# trigger any on_hit of the equipment of the target, if any.
if target.equipment and target.equipment.armor:
target.equipment.armor.equippable.on_hit(self.entity, damage)
# Apply the status effect to the target, if any.
if self.effect is not None:
target.fighter.apply_status_effect(self.effect)
else:
self.engine.message_log.add_message(
f"{attack_desc}, but does no damage.",
attack_color
)
class MovementAction(ActionWithDirection):
def perform(self) -> None:
dest_x, dest_y = self.dest_xy
if not self.engine.game_map.in_bounds(dest_x, dest_y):
# Destination is out of bounds.
raise exceptions.Impossible("That way is blocked.")
if not self.engine.game_map.tiles["walkable"][dest_x, dest_y]:
# Destination is blocked by a tile.
raise exceptions.Impossible("That way is blocked.")
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
# Destination is blocked by an entity.
raise exceptions.Impossible("There's something blocking the way!")
self.entity.move(self.dx, self.dy)
class BumpAction(ActionWithDirection):
def perform(self) -> None:
if self.target_actor:
self.entity.last_position = (self.entity.x, self.entity.y)
return MeleeAction(self.entity, self.dx, self.dy).perform()
elif self.target_NPC:
self.entity.last_position = (self.entity.x, self.entity.y)
return InteractNPCAction(self.entity, self.dx, self.dy).perform()
else:
return MovementAction(self.entity, self.dx, self.dy).perform()
class InteractNPCAction(ActionWithDirection):
def __init__(self, entity: Actor, dx: int, dy: int):
super().__init__(entity, dx, dy)
def perform(self) -> None:
if self.target_NPC:
input_handler = self.target_NPC.interact()
if input_handler:
self.engine.future_event_handler = input_handler
else:
raise exceptions.Impossible("There's nothing to interact with.")
class WaitAction(Action):
def perform(self) -> None:
self.entity.last_position = (self.entity.x, self.entity.y)
class PickupAction(Action):
"""Pickup an item and add it to the inventory, if there is room for it."""
def __init__(self, entity: Actor):
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.engine.game_map.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 TakeStairsAction(Action):
def perform(self) -> None:
"""
Take the stairs, if any exist at the entity's location.
"""
if (self.entity.x, self.entity.y) == self.engine.game_map.downstairs_location:
self.engine.game_world.generate_floor()
self.engine.message_log.add_message(
"You descend the staircase.", color.descend
)
else:
raise exceptions.Impossible("There are no stairs here.")
class EquipAction(Action):
def __init__(self, entity: Actor, item: Item):
super().__init__(entity)
self.item = item
def perform(self) -> None:
self.entity.equipment.toggle_equip(self.item)
class DropItem(ItemAction):
def perform(self) -> None:
if self.entity.equipment.item_is_equipped(self.item):
self.entity.equipment.toggle_equip(self.item)
self.entity.inventory.drop(self.item)
class PounceAction(Action):
def __init__(self, entity: Actor, x: int, y: int, effect:StatusEffect):
super().__init__(entity)
self.x = x
self.y = y
self.effect = effect
def perform(self) -> None:
self.entity.move(self.x - self.entity.x, self.y - self.entity.y)
target = None
for entity in self.engine.game_map.actors:
if entity.x == self.x and entity.y == self.y:
target = entity
break
if not target:
self.engine.message_log.add_message(f"{self.entity.name} pounces, but misses the target.")
else:
damage = (self.entity.fighter.power ** 2) - target.fighter.defense # Deal 2x the power of the attacker's power
# If the attacker is the player and godmode is on, then the attacker will do max damage
if self.entity is self.engine.player and self.engine.game_world.godmode:
damage = 99999999
attack_desc = f"{self.entity.name.capitalize()} pounces and attacks {target.name.capitalize()}"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
if damage > 0:
# if the target is the player and godmode is on, then the target will take no damage
if target is self.engine.player and self.engine.game_world.godmode:
return
self.engine.message_log.add_message(
f"{attack_desc} for {damage} hit points!",
attack_color
)
target.fighter.hp -= damage
target.fighter.last_actor_hurt_by = self.entity.internal_name
# trigger any on_attack of the equipment of the attacker, if any.
if self.entity.equipment and self.entity.equipment.weapon:
self.entity.equipment.weapon.equippable.on_attack(target)
# trigger any on_hit of the equipment of the target, if any.
if target.equipment and target.equipment.armor:
target.equipment.armor.equippable.on_hit(self.entity, damage)
# Apply the status effect to the target, if any.
if self.effect is not None:
target.fighter.apply_status_effect(self.effect)
else:
self.engine.message_log.add_message(
f"{attack_desc}, but does no damage.",
attack_color
)