-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_functions.py
201 lines (134 loc) · 8.78 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
import libtcodpy as libtcod
from enum import Enum
from game_states import GameStates
from menus import character_screen, inventory_menu, level_up_menu, bag_menu, help_menu, equipment_inventory_menu, quit_menu, drop_menu, sell_menu, buy_menu, shop_menu, buy_equipment_menu, sell_equipment_menu
from components.inventory import Inventory
class RenderOrder(Enum):
STAIRS = 1
CORPSE = 2
ITEM = 3
ACTOR = 4
def get_names_under_mouse(mouse, entities, fov_map):
(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 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, int(x + total_width / 2), y, libtcod.BKGND_NONE, libtcod.CENTER, "{0}: {1}/{2}".format(name, value, maximum))
def render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, kill_count, game_state, wall_tile, floor_tile):
if fov_recompute:
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_put_char_ex(con, x, y, wall_tile, libtcod.white, libtcod.black)
else:
libtcod.console_put_char_ex(con, x, y, floor_tile, libtcod.white, libtcod.black)
game_map.tiles[x][y].explored = True
elif game_map.tiles[x][y].explored:
if wall:
libtcod.console_put_char_ex(con, x, y, wall_tile, libtcod.grey, libtcod.black)
else:
libtcod.console_put_char_ex(con, x, y, floor_tile, libtcod.grey, libtcod.black)
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, floor_tile)
libtcod.console_set_default_foreground(con, libtcod.white)
# Danny code to center player and have window scroll
#r = 20
#libtcod.console_blit(con, player.x - r, player.y - r, 2*r+1, 2*r+1, 0, int(screen_width/2) - r, int(screen_height/2) - r)
libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
libtcod.console_set_default_background(panel, libtcod.black)
libtcod.console_clear(panel)
y = 1
for message in message_log.messages:
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
# DESCRIPTION NAMES (0)
libtcod.console_set_default_foreground(panel, libtcod.light_gray)
libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse(mouse, entities, fov_map))
# HP BAR (1)
render_bar(panel, 1, 1, bar_width, "HP", player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red)
# MANA BAR (2)
render_bar(panel, 1, 2, bar_width, "MANA", player.fighter.mana, player.fighter.max_mana, libtcod.light_purple, libtcod.darker_purple)
# EXP BAR (3)
render_bar(panel, 1, 3, bar_width, "EXP", player.level.current_xp, player.level.experience_to_next_level, libtcod.light_blue, libtcod.darker_blue)
# HEALTH TALISMAN BAR (4)
if player.inventory.search("Health Talisman") is not None:
render_bar(panel, 1, 4, bar_width, "Talisman HP", player.fighter.talismanhp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red)
# GOLD COUNTER (5)
libtcod.console_print_ex(panel, 1, 5, libtcod.BKGND_NONE, libtcod.LEFT, "Gold: {0}".format(player.fighter.gold))
# DUNGEON LEVEL (6)
libtcod.console_print_ex(panel, 1, 6, libtcod.BKGND_NONE, libtcod.LEFT, "Dungeon Level: {0}".format(game_map.dungeon_level))
libtcod.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y)
# BORDERS
#libtcod.console_print_ex(con, 0, screen_height - 8, libtcod.BKGND_NONE, libtcod.LEFT, "*" * screen_width)
#libtcod.console_print_ex(con, 0, 0, libtcod.BKGND_NONE, libtcod.LEFT, "*" * screen_width)
#for i in range(9, screen_height + 1):
#libtcod.console_print_ex(con, 0, screen_height - i, libtcod.BKGND_NONE, libtcod.LEFT, "*")
#libtcod.console_print_ex(con, screen_width - 1, screen_height - i, libtcod.BKGND_NONE, libtcod.LEFT, "*")
# END BORDERS
if game_state in (GameStates.SHOW_INVENTORY, 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"
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, 50, screen_width, screen_height)
elif game_state in (GameStates.SHOW_EQUIPMENT_INVENTORY, GameStates.DROP_EQUIPMENT):
if game_state == GameStates.SHOW_EQUIPMENT_INVENTORY:
inventory_title = "Press the key next to an item to equip it, or ESC to cancel.\n"
else:
inventory_title = "Press the key next to the equipment to drop it, or ESC to cancel.\n"
equipment_inventory_menu(con, inventory_title, player, 50, screen_width, screen_height)
elif game_state == GameStates.LEVEL_UP:
level_up_menu(con, "Level up! Choose a stat to raise:\n", player, 46, screen_width, screen_height)
elif game_state == GameStates.CHARACTER_SCREEN:
character_screen(player, 30, 10, screen_width, screen_height, kill_count)
elif game_state == GameStates.SHOW_BAG:
bag_title = "Press the key next to the option to open the bag.\n"
bag_menu(con, bag_title, player, 50, screen_width, screen_height)
elif game_state == GameStates.HELP_MENU:
help_menu(player, 30, 10, screen_width, screen_height)
elif game_state == GameStates.QUIT_MENU:
quit_title = "ARE YOU SURE YOU WANT\n TO QUIT THE GAME?\n"
quit_menu(con, quit_title, player, 21, screen_width, screen_height)
elif game_state == GameStates.DROP_MENU:
drop_title = "Which inventory would you like to drop items from?"
drop_menu(con, drop_title, player, 50, screen_width, screen_height)
elif game_state == GameStates.SELL_MENU:
shop_title = "Welcome to the Shop!\nWhat would you like to sell?\n"
sell_menu(con, shop_title, player, 50, screen_width, screen_height)
elif game_state == GameStates.BUY_MENU:
shop_title = "Welcome to the Shop!\nWhat would you like to buy?\n"
buy_menu(con, shop_title, player, 50, screen_width, screen_height, game_map.shop_items)
elif game_state == GameStates.SELL_EQUIPMENT_MENU:
shop_title = "Welcome to the Shop!\nWhat would you like to sell?\n"
sell_equipment_menu(con, shop_title, player, 50, screen_width, screen_height)
elif game_state == GameStates.BUY_EQUIPMENT_MENU:
shop_title = "Welcome to the Shop!\nWhat would you like to buy?\n"
buy_equipment_menu(con, shop_title, player, 50, screen_width, screen_height, game_map.shop_equipment_items)
elif game_state == GameStates.SHOP_MENU:
shop_title = "Welcome to the Shop!\nWhat would you like to do?\n"
shop_menu(con, shop_title, player, 50, screen_width, screen_height)
def clear_all(con, entities):
for entity in entities:
clear_entity(con, entity)
def draw_entity(con, entity, fov_map, game_map, floor_tile):
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)
if not libtcod.map_is_in_fov(fov_map, entity.x, entity.y) and not entity.stairs and game_map.tiles[entity.x][entity.y].explored:
libtcod.console_set_default_foreground(con, entity.color)
libtcod.console_put_char_ex(con, entity.x, entity.y, floor_tile, libtcod.grey, libtcod.black)
def clear_entity(con, entity):
libtcod.console_put_char(con, entity.x, entity.y, ' ', libtcod.BKGND_NONE)