-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_handlers.py
597 lines (470 loc) · 19.5 KB
/
input_handlers.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
from __future__ import annotations
import os
from typing import Callable, Optional, Tuple, TYPE_CHECKING, Union
import tcod.event
import actions
from actions import Action, BumpAction, PickupAction, WaitAction
import color
import constants
import exceptions
if TYPE_CHECKING:
from engine import Engine
from entity import Item
MOVE_KEYS = {
# arrow keys
tcod.event.K_UP: (0, -1),
tcod.event.K_DOWN: (0, 1),
tcod.event.K_LEFT: (-1, 0),
tcod.event.K_RIGHT: (1, 0),
# vi keys
tcod.event.K_h: (-1, 0),
tcod.event.K_j: (0, 1),
tcod.event.K_k: (0, -1),
tcod.event.K_l: (1, 0),
tcod.event.K_y: (-1, -1),
tcod.event.K_u: (1, -1),
tcod.event.K_b: (-1, 1),
tcod.event.K_n: (1, 1),
}
WAIT_KEYS = {tcod.event.K_PERIOD}
CONFIRM_KEYS = {tcod.event.K_RETURN, tcod.event.K_KP_ENTER}
ActionOrHandler = Union[Action, "BaseEventHandler"]
"""
An event handler return value can trigger an action or switch the active event handler
If an action is returned it will be attempted; if successful MainGameEventHandler will become active
If an event handler is returned it will become the active event handler
"""
class BaseEventHandler(tcod.event.EventDispatch[ActionOrHandler]):
def handle_events(self, event: tcod.event.Event) -> BaseEventHandler:
"""Handle an event and return the active event handler for the next event"""
state = self.dispatch(event)
if isinstance(state, BaseEventHandler):
return state
assert not isinstance(state, Action), f"{self!r} cannot handle Actions"
return self
def on_render(self, console: tcod.Console) -> None:
raise NotImplementedError()
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()
class PopupMessage(BaseEventHandler):
def __init__(self, parent_handler: BaseEventHandler, text: str):
self.parent = parent_handler
self.text = text
def on_render(self, console: tcod.Console) -> None:
# render the parent, dim it, then print the message on top
self.parent.on_render(console)
console.tiles_rgb["fg"] //= 8
console.tiles_rgb["bg"] //= 8
console.print(
console.width // 2,
console.height // 2,
self.text,
fg=color.WHITE,
bg=color.BLACK,
alignment=tcod.CENTER,
)
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[BaseEventHandler]:
# return to the parent on any key
return self.parent
class EventHandler(BaseEventHandler):
def __init__(self, engine: Engine):
self.engine = engine
def handle_events(self, event: tcod.event.Event) -> BaseEventHandler:
"""Handle events for input handlers with an engine"""
action_or_state = self.dispatch(event)
if isinstance(action_or_state, BaseEventHandler):
return action_or_state
if self.handle_action(action_or_state):
# a valid action was performed
if not self.engine.player.is_alive:
# the player died during the action processing
return GameOverEventHandler(self.engine)
elif self.engine.player.level.requires_level_up:
return LevelUpEventHandler(self.engine)
# return to main handler
return MainGameEventHandler(self.engine)
# no valid action performed; stay active
return self
def handle_action(self, action: Optional[Action]) -> bool:
"""
Handle actions generated by event methods
Returns True if the action consumes a turn
"""
if action is None:
return False
try:
action.perform()
except exceptions.Impossible as e:
self.engine.message_log.add_message(e.args[0], color.IMPOSSIBLE)
return False # skip enemy turn on exceptions
self.engine.handle_enemy_turns()
self.engine.update_fov()
return True
def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None:
if self.engine.game_map.in_bounds(event.tile.x, event.tile.y):
self.engine.mouse_location = event.tile.x, event.tile.y
def on_render(self, console: tcod.console) -> None:
self.engine.render(console)
class MainGameEventHandler(EventHandler):
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
action: Optional[Action] = None
key = event.sym
modifier = event.mod
player = self.engine.player
if key == tcod.event.K_PERIOD and modifier & (
tcod.event.KMOD_LSHIFT | tcod.event.KMOD_RSHIFT
):
return actions.TakeStairsAction(player)
if key in MOVE_KEYS:
dx, dy = MOVE_KEYS[key]
action = BumpAction(player, dx, dy)
elif key in WAIT_KEYS:
action = WaitAction(player)
elif key == tcod.event.K_g:
action = PickupAction(player)
elif key == tcod.event.K_v:
return HistoryViewer(self.engine)
elif key == tcod.event.K_i:
return InventoryActivateHandler(self.engine)
elif key == tcod.event.K_d:
return InventoryDropHandler(self.engine)
elif key == tcod.event.K_c:
return CharacterScreenEventHandler(self.engine)
elif key == tcod.event.K_SLASH:
return LookHandler(self.engine)
elif key == tcod.event.K_e:
self.engine.debug_mode = not self.engine.debug_mode
elif key == tcod.event.K_ESCAPE:
raise SystemExit()
return action
class GameOverEventHandler(EventHandler):
def on_quit(self) -> None:
if os.path.exists(constants.SAVE_FILE):
os.remove(constants.SAVE_FILE)
raise exceptions.QuitWithoutSaving()
def ev_quit(self, event: tcod.event.Quit) -> None:
self.on_quit()
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
if event.sym == tcod.event.K_ESCAPE:
self.on_quit()
class AskUserEventHandler(EventHandler):
"""Handles user input for actions that require special input"""
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
"""By default any key exits this handler"""
if event.sym in { # but ignore modifier keys
tcod.event.K_LSHIFT,
tcod.event.K_RSHIFT,
tcod.event.K_LCTRL,
tcod.event.K_RCTRL,
tcod.event.K_LALT,
tcod.event.K_RALT,
}:
return None
return self.on_exit()
def ev_mousebuttondown(
self, event: tcod.event.MouseButtonDown
) -> Optional[ActionOrHandler]:
"""By default mouse click exits this handler"""
return self.on_exit()
def on_exit(self) -> Optional[ActionOrHandler]:
return MainGameEventHandler(self.engine)
class CharacterScreenEventHandler(AskUserEventHandler):
TITLE = "Character information"
def on_render(self, console: tcod.Console) -> None:
super().on_render(console)
if self.engine.player.x <= 30:
x = 40
else:
x = 0
y = 0
width = len(self.TITLE) + 4
console.draw_frame(
x=x,
y=y,
width=width,
height=10,
title=self.TITLE,
clear=True,
fg=(255, 255, 255),
bg=(0, 0, 0),
)
player = self.engine.player
console.print(x=x + 1, y=y + 1, string=f"Level: {player.level.current_level}")
console.print(
x=x + 1,
y=y + 2,
string=f"XP: {player.level.current_xp} / {player.level.experience_to_next_level}",
)
console.print(x=x + 1, y=y + 3, string=f"Attack: {player.fighter.power}")
console.print(x=x + 3, y=y + 4, string=f"Base: {player.fighter.base_power}")
console.print(
x=x + 3, y=y + 5, string=f"Total bonus: {player.fighter.power_bonus}"
)
console.print(x=x + 1, y=y + 6, string=f"Defense: {player.fighter.defense}")
console.print(x=x + 3, y=y + 7, string=f"Base: {player.fighter.base_defense}")
console.print(
x=x + 3, y=y + 8, string=f"Total bonus: {player.fighter.defense_bonus}"
)
class LevelUpEventHandler(AskUserEventHandler):
TITLE = "Level up"
def on_render(self, console: tcod.Console) -> None:
super().on_render(console)
if self.engine.player.x <= 30:
x = 40
else:
x = 0
console.draw_frame(
x=x,
y=0,
width=40,
height=8,
title=self.TITLE,
clear=True,
fg=(255, 255, 255),
bg=(0, 0, 0),
)
console.print(x=x + 1, y=1, string="Congratulations! You level up!")
console.print(x=x + 1, y=2, string="Select an attribute to increase")
hp = self.engine.player.fighter.max_hp
console.print(
x=x + 1, y=4, string=f"(a) Constitution (+20 HP, {hp} -> {hp+20})"
)
power = self.engine.player.fighter.base_power
console.print(
x=x + 1, y=5, string=f"(b) Strength (+1 attack, {power} -> {power+1})"
)
agility = self.engine.player.fighter.base_defense
console.print(
x=x + 1, y=6, string=f"(c) Agility (+1 defense, {agility} -> {agility+1})"
)
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
player = self.engine.player
key = event.sym
index = key - tcod.event.K_a
if 0 <= index <= 2:
if index == 0:
player.level.increase_max_hp()
elif index == 1:
player.level.increase_power()
elif index == 2:
player.level.increase_defense()
else:
self.engine.message_log.add_message("Invalid selection", color.INVALID)
return None
return super().ev_keydown(event)
def ev_mousebuttondown(
self, event: tcod.event.MouseButtonDown
) -> Optional[ActionOrHandler]:
"""No clicking to exit the menu"""
return None
class InventoryEventHandler(AskUserEventHandler):
TITLE = "<missing title>"
def on_render(self, console: tcod.Console) -> None:
"""
Render the items in the inventory in a menu with letters to select them
The menu selects a screen position so as not to occlude the player sprite
"""
super().on_render(console)
number_of_items_in_inventory = len(self.engine.player.inventory.contents)
x = 40 if self.engine.player.x <= 30 else 0
y = 0
height = max(number_of_items_in_inventory + 2, 3)
item_strings = []
if number_of_items_in_inventory > 0:
for i, item_name in enumerate(self.engine.player.inventory.contents):
item_key = chr(ord("a") + i)
item_string = f"({item_key}) {item_name}"
item_list = self.engine.player.inventory.item_stack(item_name)
if len(item_list) > 1:
item_string = f"{item_string} x{len(item_list)}"
# assumption: equippable items do not stack
if self.engine.player.equipment.item_is_equipped(item_list[0]):
item_string = f"{item_string} (E)"
item_strings.append(item_string)
else:
item_strings.append("(Empty)")
width = max(len(self.TITLE) + 4, max(len(i) for i in item_strings) + 2)
console.draw_frame(
x=x,
y=y,
width=width,
height=height,
title=self.TITLE,
clear=True,
fg=(255, 255, 255),
bg=(0, 0, 0),
)
for i, item_string in enumerate(item_strings):
console.print(x + 1, y + 1 + i, item_string)
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
player = self.engine.player
key = event.sym
index = key - tcod.event.K_a
if 0 <= index <= 26:
try:
# this is disgusting, but oh well for now
selected_item_name = list(player.inventory.contents.keys())[index]
# grab the first item in the stack, if there's a stack
# if there's not a stack, grab the item
selected_item = player.inventory.item_stack(selected_item_name)[0]
except IndexError:
self.engine.message_log.add_message(
"Invalid inventory entry", color.INVALID
)
return None
return self.on_item_selected(selected_item)
return super().ev_keydown(event)
def on_item_selected(self, item: Item) -> Optional[ActionOrHandler]:
"""Called when the user selects a valid item"""
raise NotImplementedError()
class InventoryActivateHandler(InventoryEventHandler):
TITLE = "Select an item to use"
def on_item_selected(self, item: Item) -> Optional[ActionOrHandler]:
if item.consumable:
return item.consumable.get_action(self.engine.player)
elif item.equippable:
return actions.EquipAction(self.engine.player, item)
else:
return None
class InventoryDropHandler(InventoryEventHandler):
TITLE = "Select an item to drop"
def on_item_selected(self, item: Item) -> Optional[ActionOrHandler]:
return actions.DropItemAction(self.engine.player, item)
class SelectIndexHandler(AskUserEventHandler):
"""Handles asking the user for a location on the map"""
def __init__(self, engine: Engine):
super().__init__(engine)
player = self.engine.player
# convert from map-space back to viewport-space
engine.mouse_location = self.engine.game_map.map_to_viewport_coord(
(player.x, player.y)
)
def on_render(self, console: tcod.Console) -> None:
"""Highlight the tile under the cursor"""
super().on_render(console)
x, y = self.engine.mouse_location
console.tiles_rgb["bg"][x, y] = color.WHITE
console.tiles_rgb["fg"][x, y] = color.BLACK
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
key = event.sym
if key in MOVE_KEYS:
modifier = 1 # modifiers speed up key movement
if event.mod & (tcod.event.KMOD_LSHIFT | tcod.event.KMOD_RSHIFT):
modifier *= 3
if event.mod & (tcod.event.KMOD_LCTRL | tcod.event.KMOD_RCTRL):
modifier *= 10
if event.mod & (tcod.event.KMOD_LALT | tcod.event.KMOD_RALT):
modifier *= 20
x, y = self.engine.mouse_location
dx, dy = MOVE_KEYS[key]
x += dx * modifier
y += dy * modifier
# clamp to viewport
x = max(0, min(x, self.engine.viewport_width - 1))
y = max(0, min(y, self.engine.viewport_height - 1))
self.engine.mouse_location = x, y
return None
elif key in CONFIRM_KEYS:
# convert from viewport-space to map-space
mouse_location_map = self.engine.game_map.viewport_to_map_coord(
self.engine.mouse_location
)
return self.on_index_selected(*mouse_location_map)
return super().ev_keydown(event)
def ev_mousebuttondown(
self, event: tcod.event.MouseButtonDown
) -> Optional[ActionOrHandler]:
if self.engine.game_map.in_bounds(*event.tile):
if event.button == 1:
# convert from viewport-space to map-space
mouse_location_map = self.engine.game_map.viewport_to_map_coord(
event.tile
)
return self.on_index_selected(*mouse_location_map)
return super().ev_mousebuttondown(event)
def on_index_selected(self, x: int, y: int) -> Optional[ActionOrHandler]:
raise NotImplementedError()
class LookHandler(SelectIndexHandler):
"""Let the player look around with the keyboard"""
def on_index_selected(self, x: int, y: int) -> MainGameEventHandler:
"""Return to the main handler"""
return MainGameEventHandler(self.engine)
class SingleRangedAttackHandler(SelectIndexHandler):
"""Handles targeting a single enemy"""
def __init__(
self, engine: Engine, callback: Callable[[Tuple[int, int]], Optional[Action]]
):
super().__init__(engine)
self.callback = callback
def on_index_selected(self, x: int, y: int) -> Optional[Action]:
return self.callback((x, y))
class AreaRangedAttackHandler(SelectIndexHandler):
"""Handles targeting an area within a given radius"""
def __init__(
self,
engine: Engine,
radius: int,
callback: Callable[[Tuple[int, int]], Optional[Action]],
):
super().__init__(engine)
self.radius = radius
self.callback = callback
def on_render(self, console: tcod.Console) -> None:
"""Highlight the tile under the cursor and the targeted area"""
super().on_render(console)
x, y = self.engine.mouse_location
console.draw_frame(
x=x - self.radius - 1,
y=y - self.radius - 1,
width=self.radius ** 2,
height=self.radius ** 2,
fg=color.RED,
clear=False,
)
def on_index_selected(self, x: int, y: int) -> Optional[Action]:
return self.callback((x, y))
CURSOR_Y_KEYS = {tcod.event.K_UP: -1, tcod.event.K_DOWN: 1}
class HistoryViewer(EventHandler):
"""Print the history on a larger navigable window"""
def __init__(self, engine: Engine):
super().__init__(engine)
self.log_length = len(engine.message_log.messages)
self.cursor = self.log_length - 1
def on_render(self, console: tcod.Console) -> None:
super().on_render(console) # draw the main state as background
log_console = tcod.Console(console.width - 6, console.height - 6)
# draw a frame with a custom banner title
log_console.draw_frame(0, 0, log_console.width, log_console.height)
log_console.print_box(
0, 0, log_console.width, 1, "┤Message history├", alignment=tcod.CENTER
)
# render the message log with the cursor parameter
self.engine.message_log.render_messages(
log_console,
1,
1,
log_console.width - 2,
log_console.height - 2,
self.engine.message_log.messages[: self.cursor + 1],
)
log_console.blit(console, 3, 3)
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[MainGameEventHandler]:
# fancy conditional movement to make it feel right
if event.sym in CURSOR_Y_KEYS:
adjust = CURSOR_Y_KEYS[event.sym]
if adjust < 0 and self.cursor == 0:
# only move from top to bottom when on the edge
self.cursor = self.log_length - 1
elif adjust > 0 and self.cursor == self.log_length - 1:
# likewise for bottom to top
self.cursor = 0
else:
# otherwise move while clamping to the bounds of the history
self.cursor = max(0, min(self.cursor + adjust, self.log_length - 1))
elif event.sym == tcod.event.K_HOME:
self.cursor = 0
elif event.sym == tcod.event.K_END:
self.cursor = self.log_length - 1
else: # any other key closes the history
return MainGameEventHandler(self.engine)
return None