-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender_functions.py
190 lines (151 loc) · 7.77 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
import math
import tcod as libtcod
from enum import auto, Enum
from game_states import GameStates, TurnStates
from global_variables import fill_in_line
from map_objects.game_map import GameMap
from menus import weapon_menu
from ui_functions import draw_card
class RenderOrder(Enum):
CORPSE = auto()
ACTOR = auto()
CURSOR = auto()
def get_names_under_mouse(mouse, entities, fov_map, cursor):
# Check to see if the cursor is active. If yes, then giev the names that are under it.
(x, y) = (mouse.cx, mouse.cy)
if cursor.location is not None and cursor.location.x is not -1:
(x, y) = (cursor.location.x, cursor.location.y)
names = []
for entity in entities:
if entity != cursor and entity.location.x == x and entity.location.y == y and libtcod.map_is_in_fov(fov_map, entity.location.x, entity.location.y):
names.append(entity.name)
names = ', '.join(names)
return names.capitalize()
def render_bar(panel, x, y, total_width, name, value, maximum, bar_color, back_color, string_color):
# Render a bar (HP, experience, etc). first calculate the width of the bar
bar_width = int(float(value) / maximum * total_width)
# Render the background first
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, back_color)
# Now render the bar on top
if bar_width > 0:
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)
# Finally, some centered text with the values
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, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors,
status, status_height, status_width, status_x, game_state, turn_state, player, cursor):
"""
Print con console.
Displays the tiles and entities.
"""
# 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
explored = game_map.tiles[x][y].explored
highlighted = game_map.tiles[x][y].highlighted
targeted = game_map.tiles[x][y].targeted
pathable = game_map.tiles[x][y].pathable
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 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)
if highlighted:
libtcod.console_set_char_background(con, x, y, highlighted, libtcod.BKGND_SET)
if pathable and visible:
libtcod.console_set_char_background(con, x, y, pathable, libtcod.BKGND_SET)
if targeted:
libtcod.console_set_char_background(con, x, y, libtcod.light_red, libtcod.BKGND_SET)
# Draw all entities in the list
entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value)
for entity in entities_in_render_order:
if entity.location is not None and entity.render:
draw_entity(con, entity, fov_map, game_map)
# Draw a line from the player-controlled entity to the mouse.
# This has been commented out, as it's a little distracting... and ugly.
"""
if game_state == GameStates.PLAYER_TURN and turn_state == TurnStates.PRE_MOVEMENT_PHASE:
xo, yo = player.location.x, player.location.y
xd, yd = mouse.cx, mouse.cy
path = list(libtcod.line_iter(xo, yo, xd, yd))
if path:
fixed_path = fill_in_line(path)
for tile in fixed_path:
x, y = tile
if libtcod.map_is_in_fov(fov_map, x, y):
libtcod.console_set_char_background(con, x, y, libtcod.light_blue, libtcod.BKGND_SET)
"""
libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
"""
Print panel console.
Displays HP bar and message log.
"""
libtcod.console_set_default_background(panel, libtcod.black)
libtcod.console_clear(panel)
# Print the game messages, one line at a time
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
# Print what is under the mouse.
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, cursor))
# Print the HP bar.
render_bar(panel, 1, 1, bar_width, 'HP', player.chassis.hp, player.chassis.max_hp,
libtcod.light_red, libtcod.darker_red, libtcod.white)
libtcod.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y)
"""
Print status card.
Displays game state and turn state, momentum state and more.
Displays weapons stats.
"""
libtcod.console_set_default_background(status, libtcod.black)
libtcod.console_clear(status)
draw_card(status, 0, 0, status_width, status_height, libtcod.white,
turn=game_state.name, phase=turn_state.name,
speed=player.propulsion.speed, speed_x=player.propulsion.speed_x, speed_y=player.propulsion.speed_y)
if player.arsenal:
iterator = 1
# TODO: The weapon color is really determined by its projectile.
for weapon in player.arsenal.weapons:
draw_card(status, 0, iterator*10, status_width, status_height, libtcod.green,
weapon=weapon.name, dmg=weapon.damage, range=weapon.range, cur_targets=len(weapon.targets), max_targets=weapon.max_targets)
iterator += 1
libtcod.console_blit(status, 0, 0, status_width, status_height, 0, status_x, 0)
"""
Print menus.
"""
if game_state == GameStates.SHOW_WEAPONS_MENU:
weapon_menu(con, 'Choose your weapon to fire.', player.arsenal.weapons, 50, screen_height, screen_width)
def clear_all(con, entities):
for entity in entities:
clear_entity(con, entity)
def draw_entity(con, entity, fov_map, game_map):
if libtcod.map_is_in_fov(fov_map, entity.location.x, entity.location.y):
libtcod.console_set_default_foreground(con, entity.render.color)
libtcod.console_put_char(con, entity.location.x, entity.location.y, entity.render.char, libtcod.BKGND_NONE)
def clear_entity(con, entity):
# erase the character that represents this object
if entity.location is not None:
libtcod.console_put_char(con, entity.location.x, entity.location.y, ' ', libtcod.BKGND_NONE)
def erase_cell(con, x, y):
"""
Some temporary cell fg and bg should stick around until the end of a phase.
Those need to be manually erased.
"""
libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE)