-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
225 lines (198 loc) · 7.09 KB
/
input.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
import libtcodpy as libtcod
import gvar
def handle_keys():
""" | The player's main input method
| Returns a speed value as an integer
"""
from mechanics import distribute_exp, player_move_or_attack
from render import inventory_menu, stat_menu, menu, admin_menu
from nightcaste import next_level
from utils import sort_inventory
speed = 0
key = libtcod.Key()
mouse = libtcod.Mouse()
key_pressed = libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS,key,mouse,True)
if not key_pressed:
return
# Fullscreen
if key.vk == libtcod.KEY_F10:
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
# Exit
elif key.vk == libtcod.KEY_ESCAPE:
return 'exit'
# Movement
if gvar.game.game_state == 'playing':
if libtcod.console_is_key_pressed(libtcod.KEY_UP) or libtcod.console_is_key_pressed(libtcod.KEY_KP8):
speed = player_move_or_attack(0, -1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN) or libtcod.console_is_key_pressed(libtcod.KEY_KP2):
speed = player_move_or_attack(0, 1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT) or libtcod.console_is_key_pressed(libtcod.KEY_KP4):
speed = player_move_or_attack(-1, 0)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT) or libtcod.console_is_key_pressed(libtcod.KEY_KP6):
speed = player_move_or_attack(1, 0)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_KP7):
speed = player_move_or_attack(-1, -1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_KP9):
speed = player_move_or_attack(1, -1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_KP3):
speed = player_move_or_attack(1, 1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_KP1):
speed = player_move_or_attack(-1, 1)
gvar.fov_recompute = True
elif libtcod.console_is_key_pressed(libtcod.KEY_KP5):
speed = 1
elif libtcod.console_is_key_pressed(libtcod.KEY_F11):
admin_menu()
else:
key_char = chr(key.c)
if key_char == 'y': # Pick up an item
for object in gvar.game.player.currentmap().objects:
if object.x == gvar.game.player.x and object.y == gvar.game.player.y and object.item:
object.item.pick_up()
break
if key_char == 'i': # Show the inventory; if an item is selected, use it
sort_inventory()
chosen_item = inventory_menu('Inventory\nPress the key next to an item to use it, or any other to cancel.\n')
if chosen_item is not None:
chosen_item.use()
if key_char == 'd': # Show character information
chosen_item = inventory_menu('Press the key next to an item to drop it.\n')
if chosen_item is not None:
chosen_item.drop()
if key_char == 'c': # Show character information
choice = stat_menu()
if choice == 0:
distribute_exp()
if key_char == 'e': # Use a map tile (doors, stairs, etc.)
x, y = target_adjacent()
if gvar.game.player.currentmap().map[x][y].use_function is not None:
gvar.game.player.currentmap().map[x][y].use_function(x, y)
for object in gvar.game.player.currentmap().objects:
if object.x == x and object.y == y:
object.use()
if key_char == 's': # Ranged Attack
speed = gvar.game.player.fighter.ranged_attack()
if key_char == ' ': # Jump
direction = target_adjacent(True)
speed = gvar.game.player.jump(direction)
gvar.fov_recompute = True
if speed == 'cancelled':
return 'didnt-take-turn'
else:
return speed
def target_tile(max_range=None, radius=0):
""" | Target a distant tile
| For Ranged Attacks, like Archery, Thrown
| Also Fireballs or similar projectiles with Radius
"""
from components import Object
from mapcreation import Circle
from render import render_all
# Spawn a Target Object that can be moved with arrow Keys
(x, y) = (gvar.game.player.x, gvar.game.player.y)
target = Object(x, y, 'X', color=libtcod.red, name='target', blocks=False)
gvar.game.objects.append(target)
while True:
target.send_to_front()
render_all()
#Render Splash
libtcod.console_clear(gvar.overlay)
if radius > 0:
target.splash = Circle(target.x, target.y, radius)
for point in target.splash.circle:
libtcod.console_set_char_background(gvar.overlay, point[0], point[1], libtcod.yellow)
libtcod.console_blit(gvar.overlay, 0, 0, gvar.SCREEN_WIDTH, gvar.SCREEN_HEIGHT, 0, 0, 0, 1.0, 0.5)
libtcod.console_flush()
#Move the Target around
key = libtcod.Key()
mouse = libtcod.Mouse()
key_pressed = libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS,key,mouse,True)
if not key_pressed:
return 'cancelled'
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:
if libtcod.map_is_in_fov(gvar.fov_map, target.x, target.y-1):
target.y -= 1
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:
if libtcod.map_is_in_fov(gvar.fov_map, target.x, target.y+1):
target.y += 1
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:
if libtcod.map_is_in_fov(gvar.fov_map, target.x-1, target.y):
target.x -= 1
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:
if libtcod.map_is_in_fov(gvar.fov_map, target.x+1, target.y):
target.x += 1
elif key.vk == libtcod.KEY_KP7:
if libtcod.map_is_in_fov(gvar.fov_map, target.x-1, target.y-1):
target.x -= 1
target.y -= 1
elif key.vk == libtcod.KEY_KP9:
if libtcod.map_is_in_fov(gvar.fov_map, target.x+1, target.y-1):
target.x += 1
target.y -= 1
elif key.vk == libtcod.KEY_KP3:
if libtcod.map_is_in_fov(gvar.fov_map, target.x+1, target.y+1):
target.x += 1
target.y += 1
elif key.vk == libtcod.KEY_KP1:
if libtcod.map_is_in_fov(gvar.fov_map, target.x-1, target.y+1):
target.x -= 1
target.y += 1
elif key.vk == libtcod.KEY_ENTER:
gvar.game.objects.remove(target)
return target
break
elif key.vk == libtcod.KEY_ESCAPE:
gvar.game.objects.remove(target)
return 'cancelled'
break
elif chr(key.c) == 's':
# Returns String 'closest' for further evaluation by the calling function, e.g. for targeting the closest mob
gvar.game.objects.remove(target)
return 'closest'
break
def target_adjacent(relative=False):
""" | Target an adjacent tile,
| e.g. for using doors
| Returns absolute coordinates
| or the relative direction, if relative flag is set
"""
x = 0
y = 0
key = libtcod.Key()
mouse = libtcod.Mouse()
key_pressed = libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS,key,mouse,True)
if not key_pressed:
return
if key.vk == libtcod.KEY_KP4 or key.vk == libtcod.KEY_LEFT:
x -= 1
elif key.vk == libtcod.KEY_KP8 or key.vk == libtcod.KEY_UP:
y -= 1
elif key.vk == libtcod.KEY_KP6 or key.vk == libtcod.KEY_RIGHT:
x += 1
elif key.vk == libtcod.KEY_KP2 or key.vk == libtcod.KEY_DOWN:
y += 1
elif key.vk == libtcod.KEY_KP7:
x -= 1
y -= 1
elif key.vk == libtcod.KEY_KP9:
x += 1
y -= 1
elif key.vk == libtcod.KEY_KP3:
x += 1
y += 1
elif key.vk == libtcod.KEY_KP1:
x -= 1
y += 1
elif key.vk == libtcod.KEY_KP5:
pass
if relative:
return (x, y)
else:
return (gvar.game.player.x + x, gvar.game.player.y + y)