Example #1
0
def main():
    version = (0, 4, 1)

    print(f"""Light Test {'.'.join(str(v) for v in version)}
Keys:
u - update all chunks and shadows
t - open chat
l - toggle light of cursor
""")

    pg.init()

    size = (CELL_WIDTH * GRID_WIDTH, CELL_HEIGHT * GRID_HEIGHT)

    screen = pg.display.set_mode(size)

    pg.display.set_caption("Light Test")

    timer = pg.time.Clock()

    session_conf = SessionConf.new()

    world = World.new()

    # Blocks
    world.add_block(0, light_source=255)  # Air

    world.add_block(1, color=(125, 20, 20))  # Dirt maybe?

    world.add_block(2, color=(50, 10, 10))  # Dirt wall

    world.update_chunks()

    chat = ChatInputWindow(10, 10)

    objects = []

    cursor_light = None

    mouse_pressed = {
        'left': False,
        'right': False,
    }

    bg = pg.Surface(size)
    bg.fill(pg.Color(20, 125, 125))

    last_update_time = pg.time.get_ticks()

    while True:
        timer.tick(session_conf.max_fps)

        t = pg.time.get_ticks()

        dtime = (t - last_update_time) / 1000.0

        last_update_time = t

        events = pg.event.get()

        for event in events:
            if event.type == pg.QUIT:
                print("Quit event received")
                pg.quit()
                return 0
            elif event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_pressed['left'] = True

                elif event.button == 3:
                    mouse_pressed['right'] = True
            elif event.type == pg.MOUSEBUTTONUP:
                if event.button == 1:
                    mouse_pressed['left'] = False

                elif event.button == 3:
                    mouse_pressed['right'] = False
            elif event.type == pg.KEYDOWN and not chat.visible:
                if event.key == pg.K_u:
                    world.update_chunks()
                if event.key == pg.K_l:
                    if cursor_light is None:
                        cursor_light = DynamicLight(
                            world=world,
                            getposf=lambda: cursor2world(*pg.mouse.get_pos()),
                            update_time=1 / 60)
                        objects.append(cursor_light)
                    else:
                        objects.remove(cursor_light)
                        cursor_light.clear()
                        cursor_light = None

        for obj in objects:
            obj.update(dtime)

        chat.update(events)

        x, y = cursor2world(*pg.mouse.get_pos())

        if mouse_pressed['left']:
            world.setblock(x, y, 0)

        elif mouse_pressed['right']:
            world.setblock(x, y, session_conf.selected_block)

        Shadow.redraw_updated()

        screen.blit(bg, (0, 0))

        world.draw(screen)

        chat.draw(screen)

        pg.display.flip()

        pg.display.set_caption(f"Light Test ({int(timer.get_fps())} fps)")