-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_functions.py
116 lines (89 loc) · 4.7 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
import tcod as libtcod
from enum import Enum
from game_states import GameStates
from menus import inventory_menu
# Low priority means closer to the ground
class RenderOrder(Enum):
CORPSE = 1
ITEM = 2
ACTOR = 3
def get_names_under_mouse(mouse, entities, fov_map):
(x, y) = (mouse.cx, mouse.cy)
names = []
for entity in entities:
if entity.x == x and entity.y == y and libtcod.map_is_in_fov(fov_map, entity.x, entity.y):
if entity.fighter:
names.append('{0} HP: {1}/{2}'.format(entity.name, entity.fighter.hp, entity.fighter.max_hp))
else:
names.append(entity.name)
names = '; '.join(names)
return names.capitalize()
# Render UI
# panel - Console for UI
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))
# Render Entities
def 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):
# Draw all the tiles in the game map
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_set_char_background(con, x, y, colors.get('light_wall'), libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x, y, colors.get('light_ground'), libtcod.BKGND_SET)
game_map.tiles[x][y].explored = True
elif game_map.tiles[x][y].explored:
if wall:
libtcod.console_set_char_background(con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET)
else:
libtcod.console_set_char_background(con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET)
# Draw all entities in the list (Sorted by RenderOrder)
entities_by_order = sorted(entities, key = lambda x: x.render_order.value)
for entity in entities_by_order:
draw_entity(con, entity, fov_map)
libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
# Render UI
libtcod.console_set_default_background(ui_panel, libtcod.black)
libtcod.console_clear(ui_panel)
## Print messages (1 line at a time)
y = 1
for message in message_log.messages:
libtcod.console_set_default_foreground(ui_panel, message.color)
libtcod.console_print_ex(ui_panel, message_log.x, y, libtcod.BKGND_NONE, libtcod.LEFT, message.text)
y += 1
render_bar(ui_panel, 1, 1, bar_width, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red)
libtcod.console_set_default_foreground(ui_panel, libtcod.light_gray)
### Hover mouse entities
libtcod.console_print_ex(ui_panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse(mouse, entities, fov_map))
# TODO: WHAT THE HECK blit IS DOING?
libtcod.console_blit(ui_panel, 0, 0, screen_width, ui_panel_height, 0, 0, ui_panel_y)
# Check for MENUS
if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
if game_state == GameStates.SHOW_INVENTORY:
inv_title = 'Press the key next to an item to use it, or Esc to cancel.\n'
else:
inv_title = 'Press the key next to an item to drop it, or Esc to cancel.\n'
inventory_menu(con, inv_title, player.inventory, 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):
if libtcod.map_is_in_fov(fov_map, entity.x, entity.y):
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 char that represents object
libtcod.console_put_char(con, entity.x, entity.y, ' ', libtcod.BKGND_NONE)