def create_building(world, scenario_state, sound_system):
    up_door = world.entity()
    up_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_t.png"),
            3
        ),
        TilePositionable("wall", (8, 1), 1),
        Activable(False)
    )

    left_door = world.entity()
    left_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_l.png"),
            3
        ),
        TilePositionable("wall", (0, 6), 3),
        Activable(False)
    )

    down_door = world.entity()
    down_door.add_components(
        Positionable(0, 0, 50, 100),
        Renderable(
            lambda brush: brush.draw_image("door2_b.png"),
            3
        ),
        TilePositionable("wall", (8, 10), 3),
        Activable(False)
    )

    right_door = world.entity()
    right_door.add_components(
        Positionable(0, 0, 100, 50),
        Renderable(
            lambda brush: brush.draw_image("door2_r.png"),
            3
        ),
        TilePositionable("wall", (12, 6), 3),
        Activable(False)
    )

    up_window = world.entity()
    up_window.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("window_t.png"),
            1
        ),
        TilePositionable("wall", (7, 1), 1),
        Activable(False)
    )

    left_window = world.entity()
    left_window.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("window_l.png"),
            1
        ),
        TilePositionable("wall", (0, 3), 1),
        Activable(False)
    )

    down_window_renderable = Renderable(
        lambda brush: brush.draw_image("window_b.png"),
        1
    )

    down_window_toggled = [False]

    def toggle_down_window():
        down_window_renderable.render_image(
            "window_b.png" if down_window_toggled[0] else "window_semiopen_b.png",
            (0, 0) if down_window_toggled[0] else (-24, 0)
        )
        down_window_toggled[0] = not down_window_toggled[0]
        sound_system.play("window")

    down_window = world.entity()
    down_window.add_components(
        Positionable(0, 0, 100, 100),
        down_window_renderable,
        TilePositionable("wall", (4, 10), 1),
        Clickable(
            toggle_down_window,
            Button.LEFT
        ),
        Activable(False)
    )
    add_cursor_change_hoverable(down_window)

    def is_activated(entity):
        return entity.get_component(Activable).activated

    scenario_state["has_window"] = lambda: is_activated(up_window)
    scenario_state["has_down_door"] = lambda: is_activated(down_door)
    scenario_state["has_up_door"] = lambda: is_activated(up_door)
    scenario_state["has_right_door"] = lambda: is_activated(right_door)

    scenario_state["window"] = up_window

    building = Building(
        [
            Room((0, 0), [left_door, right_door, down_door, left_window]),
            Room((0, 30), [up_door, left_window]),
            Room((30, 0), [left_door, down_door, up_window]),
            Room((30, 30), [up_door, down_window])
        ],
        (30, 30)
    )

    scenario_state["minimap"] = create_minimap(world, (700, 50), building)

    for i in range(2, 5):
        bookshelf = world.entity()
        bookshelf.add_components(
            Positionable(0, 0, 50, 100),
            Renderable(
                lambda brush: brush.draw_image("bookshelf.png"),
                1
            ),
            TilePositionable("ground", (i, 1), 1)
        )

        if i == 2:
            def bookshelf_move(animable, scenario_state, direction, duration):
                def move():
                    animable.add_animation(
                        TileMoveAnimation(direction, duration)
                    )
                    scenario_state["bookshelf_moved"] =\
                        not scenario_state["bookshelf_moved"]
                    sound_system.play("furniture")

                return move

            animable = Animable()
            scenario_state["bookshelf_moved"] = False
            scenario_state["bookshelf_move_left"] =\
                bookshelf_move(animable, scenario_state, (-2, 0), 1)
            scenario_state["bookshelf_move_right"] =\
                bookshelf_move(animable, scenario_state, (2, 0), 1)
            scenario_state["bookshelf_can_move"] = True

            def toggle_bookshelf(bookshelf):
                def toggle():
                    if scenario_state["bookshelf_can_move"]:
                        if scenario_state["bookshelf_moved"]:
                            scenario_state["bookshelf_move_right"]()
                        else:
                            scenario_state["bookshelf_move_left"]()

                return toggle

            bookshelf.add_components(
                animable,
                Clickable(
                    toggle_bookshelf(bookshelf),
                    Button.LEFT
                ),
                Frightening()
            )

            add_cursor_change_hoverable(bookshelf)

    fireplace_anim = Animable()
    fireplace_anim.add_animation(
        FlickerAnimation(6, 0.3)
    )

    fireplace = world.entity()
    fireplace.add_components(
        Positionable(0, 0, 100, 100),
        Renderable(
            lambda brush: brush.draw_image("fireplace.png"),
            1
        ),
        TilePositionable("ground", (8, 1), 1),
        Lightable(
            Positionable(-130, 60, 360, 120),
            Positionable(-230, 30, 560, 200),
            (205, 155, 29, 64)
        ),
        fireplace_anim
    )

    scenario_state["fireplace"] = fireplace
예제 #2
0
def create_room(
        world,
        sound_system,
        outer_positionable=Positionable(0, 50, 600, 500),
        inner_positionable=Positionable(100, 100, 500, 400),
        outer_resolution=(50, 50),
        inner_resolution=(50, 50),
        ground_sprite = "basic_ground_tile.png",
        wall_sprite = "wall_tile",
        corner_sprite = "corner_tile"
):
    """ Create the entities of the room
    outer_positionable: describes the area of the room, walls included
    inner_positionable: describes the area of the ground
    outer_resolution: tile resolution of the walls
    inner_resolution: tile resolution of the ground
    """

    # ground
    tile_ground = world.entity()
    tile_ground.add_components(
        inner_positionable,
        TileSpace("ground", inner_resolution)
    )

    w, h = inner_resolution

    for i in range(inner_positionable.width / w):
        for j in range(inner_positionable.height / h):
            e = world.entity()
            e.add_components(
                Positionable(0, 0, w, h),
                Renderable(
                    lambda brush: brush.draw_image(ground_sprite, (0, 50)),
                    0
                ),
                TilePositionable("ground", (i, j), 0)
            )

    # wall
    tile_wall = world.entity()
    tile_wall.add_components(
        outer_positionable,
        TileSpace("wall", outer_resolution)
    )
    w, h = outer_resolution
    w_max = outer_positionable.width / w
    h_max = outer_positionable.height / h
    # corners
    tl = world.entity()
    tl.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_tl.png"),
            0
        ),
        TilePositionable("wall", (0, 1), -10)
    )
    tr = world.entity()
    tr.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_tr.png"),
            0
        ),
        TilePositionable("wall", (w_max, 1), -10)
    )
    bl = world.entity()
    bl.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_bl.png"),
            0
        ),
        TilePositionable("wall", (0, h_max), -10)
    )
    br = world.entity()
    br.add_components(
        Positionable(0, 0, 2*w, 2*h),
        Renderable(
            lambda brush: brush.draw_image(corner_sprite+"_br.png"),
            0
        ),
        TilePositionable("wall", (w_max, h_max), -10)
    )

    # walls
    for i in range(2, w_max):
        t = world.entity()
        t.add_components(
            Positionable(0, 0, w, 2*h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_t.png"),
                0
            ),
            TilePositionable("wall", (i, 1), -10)
        )
        b = world.entity()
        b.add_components(
            Positionable(0, 0, w, 2*h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_b.png"),
                0
            ),
            TilePositionable("wall", (i, h_max), -10)
        )

    for j in range(2, h_max):
        l = world.entity()
        l.add_components(
            Positionable(0, 0, 2*w, h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_l.png"),
                0
            ),
            TilePositionable("wall", (0, j), -10)
        )
        r = world.entity()
        r.add_components(
            Positionable(0, 0, 2*w, h),
            Renderable(
                lambda brush: brush.draw_image(wall_sprite+"_r.png"),
                0
            ),
            TilePositionable("wall", (w_max, j), -10)
        )

    # furniture
    table = world.entity()
    table.add_components(
        Positionable(0, 0, 150, 100),
        Renderable(
            lambda brush: brush.draw_image("table_textured.png"),
            1
        ),
        TilePositionable("ground", (1, 5), 1)
    )

    stool_animable = Animable()
    stool_toggled = [False]

    def toggle_stool():
        if not stool_animable.animations:
            stool_animable.add_animation(
                TileMoveAnimation(
                    (0, 0.5) if stool_toggled[0] else (0, -0.5),
                    0.2
                )
            )
            stool_toggled[0] = not stool_toggled[0]
            sound_system.play("furniture-short")

    stool = world.entity()
    stool.add_components(
        Positionable(0, 0, 40, 40),
        Renderable(
            lambda brush: brush.draw_image("stool.png"),
            1
        ),
        TilePositionable("ground", (2, 6), 1),
        stool_animable,
        Clickable(
            toggle_stool,
            Button.LEFT
        )
    )
    add_cursor_change_hoverable(stool)