-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_functions.py
214 lines (153 loc) · 8.69 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
import libtcodpy as libtcod
from enum import Enum, auto
from game_states import GameStates
from menus import character_screen, equipment_menu, inventory_menu, level_up_menu, description_box
class RenderOrder(Enum):
"""Render order in which entities will be drawn (highest last)."""
TILE = auto()
STAIRS = auto()
CORPSE = auto()
ITEM = auto()
ACTOR = auto()
def get_names_under_mouse(mouse, entities, fov_map):
"""Return name of entity if mouse is on top."""
(x, y) = (mouse.cx, mouse.cy)
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 get_entity_information_under_mouse(mouse, entities, fov_map):
"""
Return description of entity if mouse is on top.
When applicable, show equipped items.
"""
(x, y) = (mouse.cx, mouse.cy)
under_mouse = [entity for entity in entities
if entity.x == x and entity.y == y and libtcod.map_is_in_fov(fov_map, entity.x, entity.y)]
description = [entity.description for entity in under_mouse if entity.description]
equipment = []
for entity in under_mouse:
if entity.equipment:
equipment.extend(entity.equipment.equipped)
information = ', '.join(description)
if equipment:
information += "\nEquipment:" + ', '.join([item.name for item in equipment])
return information
def show_target(cursor, mouse, key, map_width, map_height, targeting_item):
"""Show target and impact radius (when applicable)."""
(x, y) = (mouse.cx, mouse.cy)
# Check if item component has a radius defined
item_component = targeting_item.item
radius = item_component.function_kwargs.get('radius')
libtcod.console_clear(cursor)
#libtcod.console_set_key_color(cursor, libtcod.black)
if radius:
for a in range(x - radius, x + radius + 1):
for b in range(y - radius, y + radius + 1):
libtcod.console_set_char_background(cursor, a, b, targeting_item.color)
else:
libtcod.console_set_char_background(cursor, x, y, libtcod.lightest_grey)
libtcod.console_blit(cursor, 0, 0, map_width, map_height, 0, 0, 0, 1.0, 0.5)
def render_bar(panel, x, y, total_width, name, value, maximum, text_color, bar_color, back_color):
"""Draw health bar in bottom panel console."""
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, text_color)
libtcod.console_print_ex(panel, int(x + total_width / 2), y, libtcod.BKGND_NONE,
libtcod.CENTER, '{0}: {1}/{2}'.format(name, value, maximum))
def render_info(panel, x, y, name, color, value=None, bonus=0):
libtcod.console_set_default_foreground(panel, color)
info = f'{name}'
if value:
info = f'{name}: {value}'
if bonus:
info += f'(+{bonus})'
libtcod.console_print_ex(panel, x, y, libtcod.BKGND_NONE,
libtcod.LEFT, info)
def render_all(con, panel, cursor, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width,
screen_height, bar_width, panel_width, panel_x, mouse, colors, game_state, targeting_item, key):
"""
Draw all tiles on the game (FOV) map and all entities in the list (con).
Render panel and targeting (cursor) consoles.
"""
# === Game map ===
if fov_recompute:
for y in range(game_map.height):
for x in range(game_map.width):
# Checks if tiles are in FOV
visible = libtcod.map_is_in_fov(fov_map, x, y)
tile = game_map.tiles[x][y]
if visible:
tile_fg = libtcod.Color(*tile.colors_lit.get("fg"))
tile_bg = libtcod.Color(*tile.colors_lit.get("bg"))
libtcod.console_put_char_ex(con, x, y, tile.character, tile_fg, tile_bg)
game_map.tiles[x][y].explored = True
elif game_map.tiles[x][y].explored:
tile_fg = libtcod.Color(*tile.colors_dark.get("fg"))
tile_bg = libtcod.Color(*tile.colors_dark.get("bg"))
libtcod.console_put_char_ex(con, x, y, tile.character, tile_fg, tile_bg)
# === Entities ===
entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value)
for entity in entities_in_render_order:
draw_entity(con, entity, fov_map, game_map)
# offset console by panel_width
libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, panel_width, 0)
libtcod.console_set_default_background(con, colors['background_default'])
# === Panel console ===
libtcod.console_set_default_background(panel, colors['background_panel'])
libtcod.console_clear(panel)
# Print the message, one line at a time
y = 32
for message in message_log.messages:
# if y % 2 == 0 and message.color == libtcod.lightest_grey:
# libtcod.console_set_default_foreground(panel, libtcod.grey)
# else:
libtcod.console_set_default_foreground(panel, message.color)
libtcod.console_print_ex(panel, message_log.x, y, libtcod.BKGND_NONE, libtcod.LEFT, message.text)
y += 1
render_info(panel, 1, 1, player.name.upper(), colors['text_info_alt'], None)
render_info(panel, 1, 2, 'Dungeon Level', colors['text_emphasize'], game_map.dungeon_level)
render_bar(panel, 1, 4, bar_width, 'HP', player.fighter.hp, player.fighter.max_hp, colors['text_default'],
colors['render_bar_fg'], colors['render_bar_bg'])
render_bar(panel, 1, 5, bar_width, 'XP', player.level.current_xp, player.level.experience_to_next_level, colors['text_default'],
libtcod.orange, libtcod.dark_orange)
render_info(panel, 1, 7, 'Attack', colors['text_default'], player.fighter.power, player.equipment.power_bonus)
render_info(panel, 1, 8, 'Defense', colors['text_default'], player.fighter.defense, player.equipment.defense_bonus)
libtcod.console_set_default_foreground(panel, colors['text_desaturate'])
libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT,
get_names_under_mouse(mouse, entities, fov_map))
description_box(con, get_entity_information_under_mouse(mouse, entities, fov_map),
10, screen_width, screen_height, mouse.cx, mouse.cy, colors)
libtcod.console_blit(panel, 0, 0, panel_width, screen_height, 0, panel_x, 0)
if game_state in {GameStates.SHOW_INVENTORY, GameStates.SHOW_EQUIPMENT, GameStates.DROP_INVENTORY}:
if game_state == GameStates.SHOW_INVENTORY:
inventory_title = 'Press the key next to an item to use it, or Esc to cancel.\n'
inventory_menu(con, inventory_title, player, 30, screen_width, screen_height, colors)
elif game_state == GameStates.DROP_INVENTORY:
inventory_title = 'Press the key next to an item to drop it, or Esc to cancel.\n'
inventory_menu(con, inventory_title, player, 30, screen_width, screen_height, colors)
else:
inventory_title = 'Press the key next to an item to unequip it, or Esc to cancel.\n'
equipment_menu(con, inventory_title, player, 30, screen_width, screen_height, colors)
elif game_state == GameStates.LEVEL_UP:
level_up_menu(con, 'Level up! Choose at stat to raise:', player, 30, screen_width, screen_height, colors)
elif game_state == GameStates.CHARACTER_SCREEN:
character_screen(player, 30, 10, screen_width, screen_height, colors)
elif game_state == GameStates.TARGETING:
show_target(cursor, mouse, key, game_map.width, game_map.height, targeting_item)
def clear_all(con, entities):
"""Erase characters of all entities."""
for entity in entities:
clear_entity(con, entity)
def draw_entity(con, entity, fov_map, game_map):
"""Draw entity if it is in FOV."""
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(con, entity.color)
libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
def clear_entity(con, entity):
"""Erase the character that represents this object (prevents leaving a trail)."""
libtcod.console_put_char(con, entity.x, entity.y, ' ', libtcod.BKGND_NONE)