-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.py
350 lines (261 loc) · 10.5 KB
/
renderer.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# coding: utf8
from bearlibterminal import terminal as blt
import libtcodpy as libtcod
import time
import itertools
from tile_lookups import get_char
from map_common import tiles_distance_to
import constants
import game_vars
from game_states import GameStates
import colors
def roll(dice, sides):
result = 0
for _ in range(0, dice, 1):
roll = libtcod.random_get_int(0, 1, sides)
result += roll
print 'Rolling ' + str(dice) + "d" + str(sides) + " result: " + str(result)
return result
def draw_iso(x,y,pos):
cam = game_vars.camera
tile_x, tile_y = pos[x][y]
return tile_x + cam.offset[0], tile_y + cam.offset[1]
# based on STI library for LOVE2D
def draw_iso_offset(x,y, pos, offset):
# moved to constants for precalculating
# isometric
# offset_x = constants.CAMERA_OFFSET
# hw = constants.HALF_TILE_WIDTH
# hh = constants.HALF_TILE_HEIGHT
# tile_x = (x - y) * constants.HALF_TILE_WIDTH + offset_x
# tile_y = (x + y) * constants.HALF_TILE_HEIGHT
#cam = game_vars.camera
tile_x, tile_y = pos[x][y]
return tile_x + offset[0], tile_y + offset[1]
#return tile_x + cam.offset[0], tile_y + cam.offset[1]
def cell_to_pix(val, width):
if width:
#print("Cell width is " + str(blt.state(blt.TK_CELL_WIDTH)))
res = val * blt.state(blt.TK_CELL_WIDTH)
else:
#print("Cell height is " + str(blt.state(blt.TK_CELL_HEIGHT)))
res = val * blt.state(blt.TK_CELL_HEIGHT)
#print("Result is " + str(res))
return res
def pix_to_iso(x,y):
x = float(x)
y = float(y)
offset_x = cell_to_pix(constants.MAP_WIDTH * 4, True)
iso_x = y / cell_to_pix(constants.TILE_HEIGHT, False) + (x - offset_x) / cell_to_pix(constants.TILE_WIDTH, True)
iso_y = y / cell_to_pix(constants.TILE_HEIGHT, False) - (x - offset_x) / cell_to_pix(constants.TILE_WIDTH, True)
# iso_x = y / 27 + (x - offset_x) / 54
# iso_y = y / 27 - (x - offset_x) / 54
# print("Iso_x " + str(int(iso_x)) + "iso_y " + str(int(iso_y)))
return int(iso_x), int(iso_y)
# the core drawing function
def draw_game():
# don't draw map and NPCs if sleeping
if not game_vars.player.creature.player.resting:
cam = game_vars.camera
width_start = cam.get_width_start()
width_end = cam.get_width_end(game_vars.level.current_map)
height_start = cam.get_height_start()
height_end = cam.get_height_end(game_vars.level.current_map)
offset = cam.offset
blt.layer(0)
draw_map(game_vars.level.current_map, game_vars.level.current_explored, game_vars.fov_map,
width_start, width_end, height_start, height_end, offset,
constants.DEBUG)
# moved outside since it has to be redrawn always
#renderer.draw_mouseover(x, y)
#blt.color("white")
blt.color(4294967295)
blt.layer(2)
labels = game_vars.labels
for ent in game_vars.level.current_entities:
if ent.x >= width_start and ent.x < width_end:
if ent.y >= height_start and ent.y < height_end:
ent.draw(fov_map=game_vars.fov_map, offset=offset)
if labels:
ent.draw_label()
# effects
for ef in game_vars.level.current_effects:
# ef.update()
if not ef.render:
game_vars.level.current_effects.remove(ef)
if ef.x >= width_start and ef.x < width_end:
if ef.y >= height_start and ef.y < height_end:
ef.draw()
else:
blt.puts(80,20, "SLEEPING...")
blt.color(4294967295)
if game_vars.game_state == GameStates.PLAYER_DEAD:
blt.puts(80, 20, "You are dead!")
def draw_map(map_draw, map_explored, fov_map,
width_start, width_end, height_start, height_end, offset,
debug=False):
# cam = game_vars.camera
#
# width_start = cam.get_width_start()
# width_end = cam.get_width_end(map_draw)
# height_start = cam.get_height_start()
# height_end = cam.get_height_end(map_draw)
render_pos = constants.RENDER_POSITIONS
#offset = cam.offset
#for x in range(width_start, width_end):
# for y in range(height_start, height_end):
for x,y in itertools.product(range(width_start, width_end), range(height_start, height_end)):
if debug:
is_visible = True
else:
#is_visible = libtcod.map_is_in_fov(fov_map, x, y)
is_visible = fov_map.lit(x,y)
if not is_visible:
if map_explored[x][y]:
# blt.color("gray")
blt.color(4286545791)
# cartesian
# tile_x = x * constants.TILE_WIDTH
# tile_y = y * constants.TILE_HEIGHT
tile_x, tile_y = draw_iso_offset(x, y,render_pos, offset)
blt.put(tile_x, tile_y, get_char(map_draw[x][y]))
else:
# tint light for the player
if game_vars.player is not None and game_vars.player.creature.get_light_radius() > 1:
#blt.color("white")
#print(str(blt.color_from_argb(165, 255, 255, 127)))
blt.color(dimmer(x,y, (255, 255, 127)))
#blt.color(4294967295)
else:
blt.color("white")
map_explored[x][y] = True
# map_draw[x][y].explored = True
# cartesian
# tile_x = x*constants.TILE_WIDTH
# tile_y = y*constants.TILE_HEIGHT
tile_x, tile_y = draw_iso_offset(x, y, render_pos, offset)
blt.put(tile_x, tile_y, get_char(map_draw[x][y]))
#elif map_explored[x][y]: # map_draw[x][y].explored:
# basically a copy of draw_map() but without the optimizations (to avoid having to pass camera parameters)
def draw_map_stub(map_draw, map_explored, fov_map,debug=False):
cam = game_vars.camera
width_start = cam.get_width_start()
width_end = cam.get_width_end(map_draw)
height_start = cam.get_height_start()
height_end = cam.get_height_end(map_draw)
render_pos = constants.RENDER_POSITIONS
offset = cam.offset
for x,y in itertools.product(range(width_start, width_end), range(height_start, height_end)):
if debug:
is_visible = True
else:
#is_visible = libtcod.map_is_in_fov(fov_map, x, y)
is_visible = fov_map.lit(x,y)
if not is_visible:
if map_explored[x][y]:
blt.color(4286545791) # gray
tile_x, tile_y = draw_iso_offset(x, y,render_pos, offset)
blt.put(tile_x, tile_y, get_char(map_draw[x][y]))
else:
# tint light for the player
if game_vars.player is not None and game_vars.player.creature.get_light_radius() > 1:
#blt.color("white")
#print(str(blt.color_from_argb(165, 255, 255, 127)))
blt.color(dimmer(x,y, (255, 255, 127)))
#blt.color(4294967295) #white
else:
blt.color("white")
map_explored[x][y] = True
tile_x, tile_y = draw_iso_offset(x, y, render_pos, offset)
blt.put(tile_x, tile_y, get_char(map_draw[x][y]))
# used by the player light tinting code
dist_to_alpha = { 0: 0, 1:0, 2: 25, 3: 50, 4: 75, 5:90}
dist_to_color = { 0: colors.light_yellow, 1: colors.light_yellow, 2: colors.light_yellow_dim1, 3: colors.light_yellow_dim2,
4: colors.light_yellow_dim3, 5: colors.light_yellow_dim4
}
def dimmer(x,y, color):
# paranoia
#if not isinstance(color, tuple):
# color = (255, 255, 255)
dist = tiles_distance_to((x, y), (game_vars.player.x, game_vars.player.y))
#if dist in dist_to_alpha:
if dist in dist_to_color:
return dist_to_color[dist]
#return blt.color_from_argb(a=255-dist_to_alpha[dist], r=color[0], g=color[1], b=color[2])
else:
return dist_to_color[1]
#return blt.color_from_argb(a=255, r=color[0], g=color[1], b=color[2])
def draw_mouseover(x,y):
tile_x, tile_y = pix_to_iso(x, y)
if 0 <= tile_x < len(game_vars.level.current_map) and 0 <= tile_y < len(game_vars.level.current_map[0]):
draw_x, draw_y = draw_iso(tile_x, tile_y, constants.RENDER_POSITIONS)
blt.color("light yellow")
blt.put(draw_x, draw_y, 0x2317)
def draw_messages(msg_history):
if len(msg_history) <= constants.NUM_MESSAGES:
to_draw = msg_history
else:
to_draw = msg_history[-constants.NUM_MESSAGES:]
start_y = blt.state(blt.TK_HEIGHT) - (constants.NUM_MESSAGES)
i = 0
for message, color, _ in to_draw:
string = "[color=" + str(color) + "] " + message
blt.puts(2, start_y+i, string)
i += 1
def draw_bar(x, y, total_width, name, value, maximum, bar_color, bg_color, label=None):
blt.color("white")
blt.puts(x, y-1, name)
bar_width = int(float(value) / maximum * total_width)
for i in range(total_width):
blt.color(bg_color)
if i < bar_width:
blt.color(bar_color)
blt.put(x + i, y, 0x2588)
if label:
blt.color("white")
blt.puts(x+int(total_width/2), y, label)
# drawing special effects
def wait(wait_time):
wait_time = wait_time * 0.01
start_time = time.time()
while time.time() - start_time < wait_time:
blt.refresh()
def draw_effect(x,y, tile, speed, clear, color="white"):
blt.layer(3)
blt.color(color)
blt.put_ext(x, y, 0, 0, tile)
wait(8*speed)
if clear:
blt.clear_area(x,y)
def draw_effect_mult(x,y,tile, speed, color="white"):
blt.layer(3)
blt.color(color)
blt.put_ext(x, y, 0, 0, tile)
def draw_effects_batch(effects, speed, clr_x, clr_y, clr_w=1, clr_h=1):
for eff in effects:
draw_effect_mult(eff[1], eff[2], eff[0], speed, eff[3])
wait(8*speed)
blt.clear_area(clr_x, clr_y, clr_w, clr_h)
def draw_effects(effects, speed, clr_x, clr_y, clr_w=1, clr_h=1):
for eff in effects:
draw_effect(eff[1],eff[2], eff[0], speed, False, eff[3])
blt.clear_area(clr_x,clr_y, clr_w, clr_h)
def draw_floating_text(x,y, string):
effects = []
w = 1
for l in str(string):
effects.append((l, x, y, "white"))
x +=1
w +=1
draw_effects_batch(effects, 5, x, y, w, 1)
def draw_floating_text_step(x,y, string):
effects = []
w = 1
for l in str(string):
effects.append((l,x,y, "white"))
x += 1
w += 1
draw_effects(effects, 2, x, y, w, 1)
if __name__ == "__main__":
# use it for testing
pass