/
render_functions.py
227 lines (191 loc) · 7.27 KB
/
render_functions.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
import libtcodpy as libtcod
from enum import Enum, auto
from game_states import GameStates
from menus import inventory_menu, level_up_menu, character_screen_menu
class RenderOrder(Enum):
STAIRS = auto()
CORPSE = auto()
ITEM = auto()
ACTOR = auto()
def get_names_under_mouse(mouse, entities, fov_map):
x, y = mouse.cx, mouse.cy
# Get names of entities at mouse position within player FOV
names = [entity.name for entity in entities if
entity.x == x and entity.y == y and
libtcod.map_is_in_fov(fov_map, entity.x, entity.y)]
names = ', '.join(names)
return names.capitalize()
def render_bar(panel,
x,
y,
total_width,
name,
value,
maximum,
bar_color,
back_color):
bar_width = int(float(value) / maximum * total_width)
libtcod.console_set_default_background(panel, back_color)
libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)
libtcod.console_set_default_background(panel, bar_color)
if bar_width > 0:
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)
libtcod.console_set_default_foreground(panel, libtcod.white)
libtcod.console_print_ex(
panel,
x + total_width // 2,
y,
libtcod.BKGND_NONE,
libtcod.CENTER,
"{0}: {1}/{2}".format(name, value, maximum)
)
def render_all(window,
ui_panel,
entities,
game_map,
fov_map,
fov_recompute,
message_log,
screen_width,
screen_height,
ui_panel_width,
ui_panel_height,
ui_panel_y,
mouse,
colors,
game_state):
results = []
# draw game map
if fov_recompute or libtcod.map_is_in_fov(fov_map, mouse.cx, mouse.cy):
for y in range(game_map.height):
for x in range(game_map.width):
visible = libtcod.map_is_in_fov(fov_map, x, y)
wall = game_map.tiles[x][y].block_sight
if visible:
if wall:
libtcod.console_set_char_background(window, x, y, colors.get("light_wall"), libtcod.BKGND_SET)
libtcod.console_set_char_foreground(window, x, y, libtcod.white)
libtcod.console_set_char(window, x, y, '#')
else:
libtcod.console_set_char_background(window, x, y, colors.get("light_ground"), libtcod.BKGND_SET)
libtcod.console_set_char_foreground(window, x, y, libtcod.white)
libtcod.console_set_char(window, x, y, '.')
if mouse.cx == x and mouse.cy == y:
libtcod.console_set_char_background(window, x, y, libtcod.white, libtcod.BKGND_ALPHA(0.5))
game_map.tiles[x][y].explored = True
elif game_map.tiles[x][y].explored:
if wall:
libtcod.console_set_char_background(window, x, y, colors.get("dark_wall"), libtcod.BKGND_SET)
libtcod.console_set_char_foreground(window, x, y, libtcod.light_gray)
libtcod.console_set_char(window, x, y, '#')
else:
libtcod.console_set_char_background(window, x, y, colors.get("dark_ground"), libtcod.BKGND_SET)
libtcod.console_set_char_foreground(window, x, y, libtcod.lighter_gray)
libtcod.console_set_char(window, x, y, '.')
# Draw all entities in rendering order e.g. corpses should be below player.
entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value)
for entity in entities_in_render_order:
draw_entity(window, entity, fov_map, game_map)
# Blit offscreen window to main window
libtcod.console_blit(window, 0, 0, screen_width, screen_height, 0, 0, 0)
# Prepare offscreen UI panel for drawing
player = entities[0] # first entity is always player
libtcod.console_set_default_background(ui_panel, libtcod.black)
libtcod.console_clear(ui_panel)
# Draw event message log to offscreen UI panel
for y, message in enumerate(message_log.messages): # Draw message log
libtcod.console_set_default_foreground(ui_panel, message.color)
libtcod.console_print_ex(
ui_panel,
message_log.x,
y + 1,
libtcod.BKGND_NONE,
libtcod.LEFT,
message.text
)
# Draw player info bar to offscreen UI panel
render_bar(
ui_panel,
1,
1,
ui_panel_width,
"HP",
player.fighter.hp,
player.fighter.max_hp,
libtcod.light_red,
libtcod.darker_red
)
libtcod.console_print_ex(
ui_panel,
1,
3,
libtcod.BKGND_NONE,
libtcod.LEFT,
"Dungeon level: {0}".format(game_map.dungeon_level)
)
# Draw entity name at mouse position to offscreen UI panel
libtcod.console_set_default_foreground(ui_panel, libtcod.light_gray)
libtcod.console_print_ex(
ui_panel,
1,
0,
libtcod.BKGND_NONE,
libtcod.LEFT,
get_names_under_mouse(mouse, entities, fov_map)
)
# Blit UI panel to main window
libtcod.console_blit(
ui_panel,
0,
0,
screen_width,
ui_panel_height,
0,
0,
ui_panel_y
)
if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
if game_state == GameStates.SHOW_INVENTORY:
inventory_header = "Press the key next to item to use it. Press Esc to cancel.\n"
else:
inventory_header = "Press the key next to item to drop it. Press Esc to cancel.\n"
results.extend(inventory_menu(
window,
inventory_header,
player,
50,
screen_width,
screen_height,
mouse
))
elif game_state == GameStates.LEVEL_UP:
results.extend(level_up_menu(
window,
"Level up! Choose a stat to raise:",
player,
40,
screen_width,
screen_height,
mouse
))
elif game_state == GameStates.CHARACTER_SCREEN:
character_screen_menu(
player,
30,
10,
screen_width,
screen_height
)
return results
def draw_entity(window, entity, fov_map, game_map):
# If stairs have been discovered, always draw them
if libtcod.map_is_in_fov(fov_map, entity.x, entity.y) or (entity.stairs and game_map.tiles[entity.x][entity.y].explored):
libtcod.console_set_default_foreground(window, entity.color)
libtcod.console_put_char(window, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
def clear_all(window, entities):
# clear all entities from window
for entity in entities:
clear_entity(window, entity)
def clear_entity(window, entity):
# draw blank character over location of entity character
libtcod.console_put_char(window, entity.x, entity.y, ' ', libtcod.BKGND_NONE)