Skip to content

Commit

Permalink
Port fancy_lights switch from multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
geraintwhite committed Feb 6, 2016
1 parent 991a072 commit ff1c552
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
7 changes: 3 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ def main():
print(HIDE_CUR + CLS)

saves.check_map_dir()

meta = saves.get_global_meta()
features = meta.get('features', {})

init_colours(features.get('colours', True))

blocks = render.gen_blocks()

# Menu loop
Expand Down Expand Up @@ -103,7 +101,7 @@ def game(blocks, features, meta, map_, save):
redraw = True

# Sun has moved
bk_objects, sky_colour = render.bk_objects(meta['tick'], width)
bk_objects, sky_colour = render.bk_objects(meta['tick'], width, features.get('fancy_lights', True))
if not bk_objects == old_bk_objects:
old_bk_objects = bk_objects
redraw = True
Expand Down Expand Up @@ -132,7 +130,8 @@ def game(blocks, features, meta, map_, save):
sky_colour,
lights,
meta['tick'],
last_frame
last_frame,
features.get('fancy_lights', True)
)

crafting_grid = render.render_grid(
Expand Down
19 changes: 8 additions & 11 deletions render.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
max_light = max(map(lambda b: b.get('light', 0), blocks.values()))


FANCY_LIGHTING = 1


def in_circle(test_x, test_y, x, y, r):
return circle_dist(test_x, test_y, x, y, r) < 1

Expand All @@ -24,7 +21,7 @@ def circle_dist(test_x, test_y, x, y, r):
lit = lambda x, y, p: min(circle_dist(x, y, p['x'], p['y'], p['radius']), 1)


def render_map(map_, objects, blocks, bk_objects, sky_colour, lights, tick, last_frame):
def render_map(map_, objects, blocks, bk_objects, sky_colour, lights, tick, last_frame, fancy_lights):
"""
Prints out a frame of the game.
Expand Down Expand Up @@ -61,7 +58,7 @@ def render_map(map_, objects, blocks, bk_objects, sky_colour, lights, tick, last

for x, pixel in enumerate(row):

pixel_out = calc_pixel(x, y, pixel, objects, blocks, bk_objects, sky_colour, lights, tick)
pixel_out = calc_pixel(x, y, pixel, objects, blocks, bk_objects, sky_colour, lights, tick, fancy_lights)
this_frame[-1].append(pixel_out)

try:
Expand All @@ -88,7 +85,7 @@ def obj_pixel(x, y, objects, blocks):
return None, None


def calc_pixel(x, y, pixel_f, objects, blocks, bk_objects, sky_colour, lights, tick):
def calc_pixel(x, y, pixel_f, objects, blocks, bk_objects, sky_colour, lights, tick, fancy_lights):

# Add any objects
object_, obj_colour = obj_pixel(x, y, objects, blocks)
Expand All @@ -107,7 +104,7 @@ def calc_pixel(x, y, pixel_f, objects, blocks, bk_objects, sky_colour, lights, t
bg = blocks[pixel_b]['colours']['bg']
if bg is None:
# ...bg is sky
bg = sky(x, y, tick, bk_objects, sky_colour, lights)
bg = sky(x, y, tick, bk_objects, sky_colour, lights, fancy_lights)

# if there is no object, use the fg colour
fg = obj_colour
Expand All @@ -125,7 +122,7 @@ def calc_pixel(x, y, pixel_f, objects, blocks, bk_objects, sky_colour, lights, t
return blocks[pixel_f]['char']


def bk_objects(time, width):
def bk_objects(time, width, fancy_lights):
""" Returns objects for rendering to the background """

objects = []
Expand All @@ -147,7 +144,7 @@ def bk_objects(time, width):
'colour': world_gen['sun_colour'] if day else world_gen['moon_colour']
}

if FANCY_LIGHTING:
if fancy_lights:
shade = (cos(time) + 1) / 2

sky_colour = lerp_n(rgb_to_hsv(world_gen['night_colour']), shade, rgb_to_hsv(world_gen['day_colour']))
Expand All @@ -171,14 +168,14 @@ def bk_objects(time, width):
return objects, sky_colour


def sky(x, y, time, bk_objects, sky_colour, lights):
def sky(x, y, time, bk_objects, sky_colour, lights, fancy_lights):
""" Returns the sky colour. """

for obj in bk_objects:
if obj['x'] in range(x, x+obj['width']) and obj['y'] in range(y, y+obj['height']):
return rgb(*obj['colour'])

if FANCY_LIGHTING:
if fancy_lights:

# Get all lights which effect this pixel
pixel_lights = filter(lambda l: l[1] < 1, map(lambda l: (l['colour'], lit(x, y, l)), lights))
Expand Down

0 comments on commit ff1c552

Please sign in to comment.