-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
414 lines (337 loc) · 15.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from __future__ import annotations
from typing import Optional, Tuple, TYPE_CHECKING
from Entities.Components.status_effects import StatusEffect, FrostShockStatus, MawSiphonStatus
from UI import color
import exceptions
if TYPE_CHECKING:
from engine import Engine
from Entities.entity import Actor, Entity, Item
from Entities.Components.skill import Skill
from Entities.Components.fighter import Reason
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.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 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 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 SkillAction(Action):
def __init__(
self, entity: Actor, skill: Skill, target_xy: Optional[Tuple[int, int]] = None
):
super().__init__(entity)
self.skill = skill
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."""
self.skill.perform(self.target_xy)
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 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 WaitAction(Action):
def perform(self) -> None:
pass
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()
if self.engine.game_world.current_floor == 1:
self.engine.message_log.add_message(
"You climb down the stone steps, wondering what horrors await you below", color.descend
)
else:
self.engine.message_log.add_message(
"You descend the staircase deeper into the frozen heart of the mountain.", color.descend
)
else:
raise exceptions.Impossible("There are no stairs here.")
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 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)
@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 MeleeAction(ActionWithDirection):
def perform(self) -> None:
from Entities.Components.fighter import Reason
target = self.target_actor
if not target:
raise exceptions.Impossible("Nothing to attack.")
damage = 0
if target.melee_neighbors() > 3:
damage = self.entity.fighter.power
elif target.melee_neighbors() > 1:
damage = self.entity.fighter.power - target.fighter.defense / 2
else:
damage = self.entity.fighter.power - target.fighter.defense
attack_desc = ""
if (target.name == "Player"):
attack_desc = f"{self.entity.name.capitalize()} attacks you"
else:
attack_desc = f"{self.entity.name.capitalize()} attacks the {target.name.capitalize()}"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
if damage > 0:
result = target.fighter.melee_attack(int(damage), self.entity)
reason=result[1]
if not reason==Reason.NONE:
if reason==Reason.DODGED:
target.fighter.Dodge()
if (target.name == "Player"):
self.engine.message_log.add_message(
f"You dodge an incoming attack from the {self.entity.name.capitalize()}", attack_color
)
else:
self.engine.message_log.add_message(
f"The {target.name.capitalize()} dodges an attack from the {self.entity.name.capitalize()}", attack_color
)
elif reason==Reason.BLOCKED:
if (target.name == "Player"):
self.engine.message_log.add_message(
f"You block the attack from the {self.entity.name.capitalize()} but take {result[0]}({int(damage)})", attack_color
)
else:
self.engine.message_log.add_message(
f"The {target.name.capitalize()} blocks the attack from the {self.entity.name.capitalize()} but takes {result[0]}({int(damage)})", attack_color
)
else:
damage=result[0]
self.engine.message_log.add_message(
f"{attack_desc} for {int(damage)} hit points.", attack_color
)
else:
self.engine.message_log.add_message(
f"{attack_desc} but does no damage.", attack_color
)
for effect in self.entity.status_effects:
effect.on_deal_damage(target,int(damage))
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("You cannot escape the frozen evil that way.")
if not self.engine.game_map.tiles["walkable"][dest_x, dest_y]:
# Destination is out of bounds.
raise exceptions.Impossible("That way is blocked.")
if self.engine.game_map.get_blocking_entity_at_location(dest_x, dest_y):
# Destination is out of bounds.
raise exceptions.Impossible("That way is blocked.")
tile_entities=self.engine.game_map.get_entities_at_location(dest_x, dest_y)
if tile_entities:
for entity in tile_entities:
if entity.trigger==True:
entity.on_press(self.engine)
self.entity.move(self.dx, self.dy)
class BumpAction(ActionWithDirection):
"""
Bump checks if an impassable entity is in the direction being bumped and attacks if there is and tries to
move there if there isn't
"""
def perform(self) -> None:
if self.target_actor and self.target_actor.is_alive:
return MeleeAction(self.entity, self.dx, self.dy).perform()
else:
return MovementAction(self.entity, self.dx, self.dy).perform()
class FreezeSpellAction(ActionWithDirection):
def __init__(self,entity,dx,dy,magnitude):
super().__init__(entity=entity,dx=dx,dy=dy)
self.magnitude=magnitude
def perform(self) -> None:
target = self.target_actor
if not target and self.entity.name == "Player":
raise exceptions.Impossible("Nothing to attack.")
damage = 6 - target.fighter.defense
attack_desc = ""
target_desc = ""
target_desc2 = ""
target_desc3 = ""
if (target.name == "Player"):
attack_desc = f"{self.entity.name.capitalize()} casts Frost Shock at you"
target_desc = "you"
target_desc2 = "your"
target_desc3 = "grow"
else:
attack_desc = f"{self.entity.name.capitalize()} casts Frost Shock at the {target.name.capitalize()}"
target_desc = f"the {target.name.capitalize()}"
target_desc2 = f"the {target.name.capitalize()}'s"
target_desc3 = "grows"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
if damage > 0:
self.engine.message_log.add_message(
f"{attack_desc} for {int(damage)} hit points and freezing {target_desc}. Ice spikes begin to "
f"pierce {target_desc2} skin...",
attack_color
)
target.fighter.take_damage(int(damage))
status = FrostShockStatus("Frost Shock", self.magnitude, target, 10)
else:
self.engine.message_log.add_message(
f"{attack_desc} but does no damage. However, {target_desc} {target_desc3} colder and ice spikes "
f"begin to pierce {target_desc2} skin...", attack_color
)
status = FrostShockStatus("Frost Shock", 1, target, 5)
class DrainSpellAction(ActionWithDirection):
def __init__(self,entity,dx,dy,magnitude):
super().__init__(entity=entity,dx=dx,dy=dy)
self.magnitude=magnitude
def perform(self) -> None:
target = self.target_actor
if not target and self.entity.name == "Player":
raise exceptions.Impossible("Nothing to attack.")
drain=self.magnitude
if target.fighter.energy<self.magnitude:
drain=target.fighter.energy
attack_desc = ""
target_desc = ""
target_desc2 = ""
target_desc3 = ""
if (target.name == "Player"):
attack_desc = f"{self.entity.name.capitalize()} casts Drain on you"
target_desc = "you"
target_desc2 = "your"
target_desc3 = "grow"
else:
attack_desc = f"{self.entity.name.capitalize()} casts Drain at the {target.name.capitalize()}"
target_desc = f"the {target.name.capitalize()}"
target_desc2 = f"the {target.name.capitalize()}'s"
target_desc3 = "grows"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
self.engine.message_log.add_message(
f"{attack_desc}, siphoning {drain} energy",
attack_color
)
target.fighter.energy=max(0,target.fighter.energy-5)
class MawSpellAction(ActionWithDirection):
def __init__(self,entity,dx,dy,magnitude):
super().__init__(entity=entity,dx=dx,dy=dy)
self.magnitude=magnitude
def perform(self) -> None:
target = self.target_actor
if not target and self.entity.name == "Player":
raise exceptions.Impossible("Nothing to attack.")
damage = max(0,self.magnitude+3 - target.fighter.defense)
attack_desc = ""
target_desc = ""
if (target.name == "Player"):
attack_desc = f"{self.entity.name.capitalize()} bites your leg, injecting maw-venom"
target_desc = "your"
else:
attack_desc = f"{self.entity.name.capitalize()} bites the {target.name.capitalize()}, injecting maw-venom"
target_desc = f"the {target.name.capitalize()}'s"
if self.entity is self.engine.player:
attack_color = color.player_atk
else:
attack_color = color.enemy_atk
if damage > 0:
self.engine.message_log.add_message(
f"{attack_desc} for {int(damage)} hit points and {target_desc} energy starts to be siphoned",
attack_color
)
target.fighter.take_damage(int(damage))
status = MawSiphonStatus("Maw Siphon", self.magnitude, target, 10)
else:
self.engine.message_log.add_message(
f"{attack_desc} and {target_desc} energy starts to be siphoned", attack_color
)
status = MawSiphonStatus("Maw Siphon", self.magnitude, target, 10)
class TeleportAction(Action):
def __init__(
self, entity: Actor, target_xy: Optional[Tuple[int, int]] = None
):
super().__init__(entity)
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."""
self.entity.x = self.target_xy[0]
self.entity.x = self.target_xy[1]
self.entity.place(self.target_xy[0], self.target_xy[1], self.entity.gamemap)