-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
253 lines (200 loc) · 9.18 KB
/
engine.py
File metadata and controls
253 lines (200 loc) · 9.18 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
import tcod as libtcod
import tcod.event as libevent
from map_objects.game_map import GameMap
from components.fighter import Fighter
from components.inventory import Inventory
from game_states import GameStates
from game_messages import MessageLog, Message
from entity import Entity, get_blocking_entities_at_location
from input_handlers import handle_keys, handle_mouse
from render_functions import clear_all, render_all, RenderOrder
from fov_functions import initialize_fov, recompute_fov
from death_functions import kill_player, kill_monster
def main():
# console props
screen_width = 80
screen_height = 50
# UI settings
bar_width = 20
ui_panel_height = 7
ui_panel_y = screen_height - ui_panel_height
## Message system
message_x = bar_width + 2
message_width = screen_width - bar_width - 2
message_height = ui_panel_height - 1
message_log = MessageLog(message_x, message_width, message_height)
# map props
map_width = 80
map_height = 43
room_max_size = 10
room_min_size = 6
max_rooms = 30
max_monsters_per_room = 3
max_items_per_room = 4
# FOV settings
fov_algorithm = 0
fov_light_walls = True
fov_radius = 10
fov_recompute = True # we don't need to recompute FOV everytime (wait, fight, use item)
# Colors dictionary
colors = {
'dark_wall': libtcod.Color(0, 0, 100),
'dark_ground': libtcod.Color(50, 50, 150),
'light_wall': libtcod.Color(130, 110, 50),
'light_ground': libtcod.Color(200, 180, 50)
}
# Entities setup
player_fighter_comp = Fighter(30, 2, 5)
player_inventory_comp = Inventory(12)
player = Entity(0, 0, '@', libtcod.white, 'Player', True, RenderOrder.ACTOR,
player_fighter_comp, inventory=player_inventory_comp)
entities = [player]
game_state = GameStates.PLAYERS_TURN
previous_game_state = game_state
# Consoles setup
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(screen_width, screen_height, 'First Shem RL. Thanks Tutorial', False)
con = libtcod.console.Console(screen_width, screen_height)
ui_panel = libtcod.console.Console(screen_width, ui_panel_height)
# Gamemap setup
game_map = GameMap(map_width, map_height)
game_map.make_map(max_rooms, room_min_size, room_max_size, player, entities, max_monsters_per_room, max_items_per_room)
fov_map = initialize_fov(game_map)
# Input setup
key = libtcod.Key()
mouse = libtcod.Mouse()
targeting_item = None
# WARN Check tcod.event for QUIT events
while not libtcod.console_is_window_closed():
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)
if fov_recompute:
recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)
render_all(con, entities, player, game_map, fov_map, fov_recompute, screen_width, screen_height,
ui_panel, bar_width, ui_panel_height, ui_panel_y, message_log, mouse, game_state, colors)
fov_recompute = False
libtcod.console_flush()
clear_all(con, entities)
# Input handling
action = handle_keys(key, game_state)
mouse_action = handle_mouse(mouse)
move = action.get('move')
exit = action.get('exit')
pickup = action.get('pickup')
show_inventory = action.get('show_inventory')
drop_inventory = action.get('drop_inventory')
inventory_index = action.get('inventory_index')
fullscreen = action.get('fullscreen')
left_click = mouse_action.get('left_click')
right_click = mouse_action.get('right_click')
player_turn_results = []
# Movement handling
if move and game_state == GameStates.PLAYERS_TURN:
dx, dy = move
dest_x = player.x + dx
dest_y = player.y + dy
if not game_map.is_blocked(dest_x, dest_y):
target = get_blocking_entities_at_location(entities, dest_x, dest_y)
if target:
attack_results = player.fighter.attack(target)
player_turn_results.extend(attack_results)
else:
player.move(dx, dy)
fov_recompute = True
game_state = GameStates.ENEMY_TURN
# Pickup handling
elif pickup and game_state == GameStates.PLAYERS_TURN:
for entity in entities:
if entity.item and entity.x == player.x and entity.y == player.y:
pickup_results = player.inventory.add_item(entity)
player_turn_results.extend(pickup_results)
break
else:
message_log.add_message(Message('There is nothing here to pickup', libtcod.yellow))
# Menus display handling
if show_inventory:
previous_game_state = game_state
game_state = GameStates.SHOW_INVENTORY
if drop_inventory:
previous_game_state = game_state
game_state = GameStates.DROP_INVENTORY
# Items usage handling
if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(player.inventory.items):
item = player.inventory.items[inventory_index]
if game_state == GameStates.SHOW_INVENTORY:
player_turn_results.extend(player.inventory.use(item, entities=entities, fov_map=fov_map))
elif game_state == GameStates.DROP_INVENTORY:
player_turn_results.extend(player.inventory.drop_item(item))
if game_state == GameStates.TARGETING:
if left_click:
target_x, target_y = left_click
item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map,
target_x=target_x, target_y=target_y)
player_turn_results.extend(item_use_results)
elif right_click:
player_turn_results.append({'targeting_cancelled': True})
if exit:
if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
game_state = previous_game_state
elif game_state == GameStates.TARGETING:
player_turn_results.append({'targeting_cancelled': True})
else:
return True
if fullscreen:
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)
# Print turn results
for p_t_result in player_turn_results:
message = p_t_result.get('message')
dead_entity = p_t_result.get('dead')
item_added =p_t_result.get('item_added')
item_consumed = p_t_result.get('item_consumed')
item_dropped = p_t_result.get('item_dropped')
targeting = p_t_result.get('targeting')
targeting_cancelled = p_t_result.get('targeting_cancelled')
if message:
message_log.add_message(message)
if dead_entity:
if dead_entity == player:
message, game_state = kill_player(dead_entity)
else:
message = kill_monster(dead_entity)
message_log.add_message(message)
if item_added:
entities.remove(item_added)
game_state = GameStates.ENEMY_TURN
if item_consumed:
game_state = GameStates.ENEMY_TURN
if targeting:
previous_game_state = GameStates.PLAYERS_TURN
game_state = GameStates.TARGETING
targeting_item = targeting
message_log.add_message(targeting_item.item.targeting_message)
if targeting_cancelled:
game_state = previous_game_state
message_log.add_message(Message('Targeting cancelled'))
if item_dropped:
entities.append(item_dropped)
game_state = GameStates.ENEMY_TURN
# ENEMIES TURN
if game_state == GameStates.ENEMY_TURN:
for entity in entities:
if entity.ai:
enemy_turn_results = entity.ai.take_turn(player, fov_map, game_map, entities)
for e_t_result in enemy_turn_results:
message = e_t_result.get('message')
dead_entity = e_t_result.get('dead')
if message:
message_log.add_message(message)
if dead_entity:
if dead_entity == player:
message, game_state = kill_player(dead_entity)
else:
message = kill_monster(dead_entity)
message_log.add_message(message)
if game_state == GameStates.PLAYER_DEAD:
break
if game_state == GameStates.PLAYER_DEAD:
break
else:
game_state = GameStates.PLAYERS_TURN
if __name__ == "__main__":
main()