-
Notifications
You must be signed in to change notification settings - Fork 28
/
input_handlers.py
153 lines (111 loc) · 3.67 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
import libtcodpy as libtcod
from game_states import GameStates
def handle_keys(key, game_state):
if game_state == GameStates.PLAYERS_TURN:
return handle_player_turn_keys(key)
elif game_state == GameStates.PLAYER_DEAD:
return handle_player_dead_keys(key)
elif game_state == GameStates.TARGETING:
return handle_targeting_keys(key)
elif game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
return handle_inventory_keys(key)
elif game_state == GameStates.LEVEL_UP:
return handle_level_up_menu(key)
elif game_state == GameStates.CHARACTER_SCREEN:
return handle_character_screen(key)
return {}
def handle_player_turn_keys(key):
key_char = chr(key.c)
# Movement keys
if key.vk == libtcod.KEY_UP or key_char == 'k':
return {'move': (0, -1)}
elif key.vk == libtcod.KEY_DOWN or key_char == 'j':
return {'move': (0, 1)}
elif key.vk == libtcod.KEY_LEFT or key_char == 'h':
return {'move': (-1, 0)}
elif key.vk == libtcod.KEY_RIGHT or key_char == 'l':
return {'move': (1, 0)}
elif key_char == 'y':
return {'move': (-1, -1)}
elif key_char == 'u':
return {'move': (1, -1)}
elif key_char == 'b':
return {'move': (-1, 1)}
elif key_char == 'n':
return {'move': (1, 1)}
elif key_char == 'z':
return {'wait': True}
if key_char == 'g':
return {'pickup': True}
elif key_char == 'i':
return {'show_inventory': True}
elif key_char == 'd':
return {'drop_inventory': True}
elif key.vk == libtcod.KEY_ENTER:
return {'take_stairs': True}
elif key_char == 'c':
return {'show_character_screen': True}
if key.vk == libtcod.KEY_ENTER and key.lalt:
# Alt+Enter: toggle full screen
return {'fullscreen': True}
elif key.vk == libtcod.KEY_ESCAPE:
# Exit the game
return {'exit': True}
# No key was pressed
return {}
def handle_targeting_keys(key):
if key.vk == libtcod.KEY_ESCAPE:
return {'exit': True}
return {}
def handle_player_dead_keys(key):
key_char = chr(key.c)
if key_char == 'i':
return {'show_inventory': True}
if key.vk == libtcod.KEY_ENTER and key.lalt:
# Alt+Enter: toggle full screen
return {'fullscreen': True}
elif key.vk == libtcod.KEY_ESCAPE:
# Exit the menu
return {'exit': True}
return {}
def handle_inventory_keys(key):
index = key.c - ord('a')
if index >= 0:
return {'inventory_index': index}
if key.vk == libtcod.KEY_ENTER and key.lalt:
# Alt+Enter: toggle full screen
return {'fullscreen': True}
elif key.vk == libtcod.KEY_ESCAPE:
# Exit the menu
return {'exit': True}
return {}
def handle_main_menu(key):
key_char = chr(key.c)
if key_char == 'a':
return {'new_game': True}
elif key_char == 'b':
return {'load_game': True}
elif key_char == 'c' or key.vk == libtcod.KEY_ESCAPE:
return {'exit': True}
return {}
def handle_level_up_menu(key):
if key:
key_char = chr(key.c)
if key_char == 'a':
return {'level_up': 'hp'}
elif key_char == 'b':
return {'level_up': 'str'}
elif key_char == 'c':
return {'level_up': 'def'}
return {}
def handle_character_screen(key):
if key.vk == libtcod.KEY_ESCAPE:
return {'exit': True}
return {}
def handle_mouse(mouse):
(x, y) = (mouse.cx, mouse.cy)
if mouse.lbutton_pressed:
return {'left_click': (x, y)}
elif mouse.rbutton_pressed:
return {'right_click': (x, y)}
return {}