Ejemplo n.º 1
0
def grid_editor_loop():

    while True:
        events = yield from listen.any(
            enter=listen.on_cursor_enter(globs.window),
            pos=listen.on_cursor_pos(globs.window),
            scroll=listen.on_scroll(globs.window),
            button=listen.on_mouse_button(globs.window))

        if globs.mode != "grid_edit": continue

        if "button" in events:
            _, button, action, mods = events["button"]
            # left click
            if (button == glfw.MOUSE_BUTTON_LEFT and\
                    action == glfw.PRESS):
                x, y = glfw.get_cursor_pos(globs.window)
                i, j, isUpper = position_to_grid_index(x, y)
                p1, p2, p3 = position_to_triangle_points(x, y)
                #print(p1, p2, p3)
                #print(i, j, isUpper)
                # click on a grid tile: make that tile the cursor tile
                if p1.x + p1.y > 0:
                    poly_i = get_polygon_index(p1, p2, p3, isUpper)
                    #print("clicked polygon at index=", poly_i)
                    #poly_p1, poly_p2, poly_p3 = globs.polygons[poly_i].get_points()
                    #print("polygon at index=", poly_i, " at points ", poly_p1, poly_p2, poly_p3)
                    if poly_i > -1:
                        globs.numberClicksThisLevel += 1  # related to score
                        globs.tri["updatedNeighbors"] = [
                        ]  # reset color change tally
                        change_polygon_color_cascade(poly_i)
                        increment_color_index()
Ejemplo n.º 2
0
def hud_loop():
    # look for clicks that fall in the areas covered by HUD elements
    while True:
        events = yield from listen.any(pos=listen.on_cursor_pos(globs.window),
                                       button=listen.on_mouse_button(
                                           globs.window))

        # editing the terrain on cursor movement or click
        if "button" in events or "pos" in events:
            if "button" in events:
                x, y = glfw.get_cursor_pos(globs.window)
            else:
                _, x, y = events["pos"]
            hud_element = mouse_to_hud(
                x, y)  # identify which HUD element the mouse is over

            if glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_LEFT
                                     ) == glfw.PRESS and hud_element != "":
                # left click might select a UI button
                if hud_element == "test":
                    print("Click received. Run test function for HUD element")
                    # TODO add meaningful functions for HUD elements being clicked
                elif hud_element == "inventory":
                    print(
                        "Click received. Run inventory function for HUD element"
                    )
Ejemplo n.º 3
0
def editor_camera_loop():
    # keep track of keys in this dictionary
    keys = {
        glfw.KEY_W: False,
        glfw.KEY_A: False,
        glfw.KEY_S: False,
        glfw.KEY_D: False,
        glfw.KEY_LEFT_SHIFT: False,
        glfw.KEY_SPACE: False,
        glfw.KEY_P: False
    }
    while True:
        events = yield from listen.any(
                key=listen.on_key(globs.window),
                frame=listen.event("on_frame"))

        # update dictionary
        if "key" in events:
            _, key, _, action, _ = events["key"]
            if key in keys:
                if action == glfw.PRESS: keys[key] = True
                if action == glfw.RELEASE: keys[key] = False

        # must be in grid_edit mode
        if globs.mode != "grid_edit": continue

        # adjust camera position based on key state
        if "frame" in events:
            _, dt = events["frame"]

            v = -10

            dx = 0
            if keys[glfw.KEY_D]: dx += v
            if keys[glfw.KEY_A]: dx -= v
            globs.cam["pos"].x += dt*dx

            dy = 0
            if keys[glfw.KEY_W]: dy += v
            if keys[glfw.KEY_S]: dy -= v
            globs.cam["pos"].y += dt*dy
Ejemplo n.º 4
0
def grid_editor_loop():

    # currently selected asset
    cursor_asset = 0 # an index in globs.grid.assets

    # a quadarray for the cursor
    cursor_array = QuadArray(globs.assets[globs.grid.assets[cursor_asset]])
    cursor_array.update()
    globs.quadarrays.append(cursor_array)

    # convert mouse coordinates to grid coordinates with rounding
    def mouse_to_grid(x,y):
        coords = mouse_coords(x,y)
        x0,y0 = np.floor(coords.x),np.floor(coords.y)
        while x0 > x: x0 -= 1
        while y0 > y: y0 -= 1
        return x0,y0

    while True:
        events = yield from listen.any(
                enter=listen.on_cursor_enter(globs.window),
                pos=listen.on_cursor_pos(globs.window),
                scroll=listen.on_scroll(globs.window),
                button=listen.on_mouse_button(globs.window)
                )

        if globs.mode != "grid_edit": continue

        if "button" in events:
            _, button, action, mods = events["button"]

            # releasing any mouse button saves
            if action == glfw.RELEASE:
                globs.grid.save()

            # middle click
            if (button == glfw.MOUSE_BUTTON_MIDDLE and\
                    action == glfw.PRESS):
                x,y = glfw.get_cursor_pos(globs.window)
                x,y = mouse_to_grid(x,y)
                if (x,y) in globs.grid:
                    # click on a grid tile: make that tile the cursor tile
                    asset = globs.grid[x,y]
                    cursor_asset = globs.grid.assets.index(asset)

                    cursor_array.asset.clear()
                    cursor_array.asset.enqueue(globs.assets[globs.grid.assets[cursor_asset]],loop=True)
                    cursor_array.update()
                else:
                    # click on empty space with middle mouse: place the player there and go into play mode
                    place_player(x,y)
                    center_camera(x,y)
                    # delete the cursor quad if we have it
                    if len(cursor_array.quads) > 0:
                        del cursor_array.quads[0]
                        cursor_array.update()
                    globs.mode = "play"

        # editing the terrain on cursor movement or click
        if "button" in events or "pos" in events:
            if "button" in events: x,y = glfw.get_cursor_pos(globs.window)
            else: _,x,y = events["pos"]
            x,y = mouse_to_grid(x,y)

            if glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS:
                # left click places the current asset
                asset = globs.grid.assets[cursor_asset]
                globs.grid[x,y] = asset
            elif glfw.get_mouse_button(globs.window, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS:
                # right click deletes
                del globs.grid[x,y]


        # scrolling changes the cursor asset
        if "scroll" in events:
            _, _, dy = events["scroll"]

            if dy > 0:
                cursor_asset += 1
                if cursor_asset == len(globs.grid.assets):
                    cursor_asset = 0

            if dy < 0:
                cursor_asset -= 1
                if cursor_asset == -1:
                    cursor_asset = len(globs.grid.assets)-1

            cursor_array.asset.clear()
            cursor_array.asset.enqueue(globs.assets[globs.grid.assets[cursor_asset]],loop=True)
            cursor_array.update()

        # remove the cursor quad when the mouse leaves the window
        if "enter" in events:
            _, entered = events["enter"]
            if not entered:
                if len(cursor_array.quads) > 0:
                    del cursor_array.quads[0]
                cursor_array.update()

        # move the cursor quad to the mouse position
        if "pos" in events:
            _,x,y = events["pos"]

            x,y = mouse_to_grid(x,y)

            if len(cursor_array.quads) == 0:
                cursor_array.quads.append(Vec(0,0,0.5))

            cursor_array.quads[0].x = x
            cursor_array.quads[0].y = y
            cursor_array.update()
Ejemplo n.º 5
0
def play_loop():
    velocities = [Vec(0, 0), Vec(0, 0)]
    dpad = Vec(0, 0)
    listen.launch(dpad_loop(dpad))
    direction = "right"

    collision_radius = 3

    while True:
        events = yield from listen.any(frame=listen.event("on_frame"), )

        if globs.mode != "play": continue
        _, dt = events["frame"]

        for pn in range(len(globs.playerarray.quads)):

            playerpos = globs.playerarray.quads[pn]
            velocity = velocities[pn]

            # is the player touching a wall?
            touch = {
                "bot": Vec(0, -1, 0),
                "left": Vec(-1, 0, 0),
                "right": Vec(1, 0, 0)
            }
            for key in touch:
                found = False
                for (x, y) in grid_positions_nearby(playerpos,
                                                    collision_radius):
                    if squares_intersect(Vec(x, y),
                                         playerpos + touch[key] / 16):
                        if globs.grid[(x, y)] == "terrain-top":
                            found = True
                            break
                touch[key] = found  # replace dict entry with boolean
            print(touch)

            if touch["bot"]:
                # if upstream of obstacle, make the vertical velocity 0
                velocity.y = 0

            else:
                # otherwise apply downstream current
                if velocity.y <= 1:
                    velocity.y -= 5 * dt

            # horizontal movement
            velocity.x = dpad.x * 3

            for (x, y) in grid_positions_nearby(playerpos, collision_radius):
                if rect_in_rect(Vec(x, y), playerpos, Vec(1, 1), Vec(1, 1)):
                    if globs.grid[(x, y)] == "terrain-side-right":
                        if velocity.x <= 5:
                            velocity.x += 1 * dt
                    if globs.grid[(x, y)] == "terrain-side-left":
                        if velocity.x >= -5:
                            velocity.x -= 1 * dt

            # bonk on sides
            if touch["right"] and velocity.x > 0: velocity.x = 0
            if touch["left"] and velocity.x < 0: velocity.x = 0

            # compute the delta, and apply it with collision detection
            delta = velocity * dt

            # move the player. split the delta into lots of tiny nudges
            # and then apply as many as you can before we collide
            for nudge in split_delta(delta, 1 / 16):
                any_collisions = False
                for (x, y) in globs.grid:
                    if globs.grid[x, y] == "terrain-top": continue
                    if squares_intersect(Vec(x, y), playerpos + nudge.xy0):
                        any_collisions = True
                        break

                if any_collisions: break
                playerpos += nudge.xy0

        # finalize playerpos
        globs.playerarray.update()

        # adjust the player sprite animation
        # compute the new label
        newlabel = "idle"
        if touch["bot"]:
            if velocity.x != 0: newlabel = "sneak"
            else: newlabel = "idle"
        else: newlabel = "move"

        # compute the direction the player is facing
        if velocity.x != 0:
            if velocity.x > 0: direction = "right"
            else: direction = "left"
        newlabel += "-" + direction

        # update the animator
        if globs.playerarray.asset.label != newlabel:
            globs.playerarray.asset.clear()
            globs.playerarray.asset.enqueue(globs.assets["dino-" + newlabel],
                                            loop=True,
                                            label=newlabel)
Ejemplo n.º 6
0
def play_loop():
    velocity = Vec(0, 0)
    dpad = Vec(0, 0)
    listen.launch(dpad_loop(dpad))
    direction = "right"

    while True:
        events = yield from listen.any(frame=listen.event("on_frame"), )
        # only allow play mode
        if globs.mode != "play": continue
        _, dt = events["frame"]

        playerpos = globs.playerarray.quads[0]

        center_camera(playerpos.x, playerpos.y)

        # is the player touching something?
        touch = {
            "top": Vec(0, 1, 0),
            "bot": Vec(0, -1, 0),
            "left": Vec(-1, 0, 0),
            "right": Vec(1, 0, 0)
        }
        for key in touch:
            found = False
            for (x, y) in globs.grid:
                if squares_intersect(Vec(x, y), playerpos + touch[key] / 16):
                    found = True
                    break
            touch[key] = found  # replace dict entry with boolean

        if touch["bot"]:
            # if grounded, make the vertical velocity 0
            velocity.y = 0

            # if the player is pressing up, make them jump
            if dpad.y == 1:
                velocity.y = 5
        else:
            # otherwise apply gravity
            velocity.y -= 5 * dt
            if (velocity.y < 0):  # after peak jump, fall faster
                velocity.y -= 5 * dt

        # head bonk
        if touch["top"] and velocity.y > 0:
            velocity.y = 0

        # horizontal movement
        velocity.x = dpad.x * 3

        # bonk on sides
        if touch["right"] and velocity.x > 0: velocity.x = 0
        if touch["left"] and velocity.x < 0: velocity.x = 0

        # compute the delta, and apply it with collision detection
        delta = velocity * dt

        # move the player. split the delta into lots of tiny nudges
        # and then apply as many as you can before we collide
        for nudge in split_delta(delta, 1 / 16):
            any_collisions = False
            for (x, y) in globs.grid:
                if squares_intersect(Vec(x, y), playerpos + nudge.xy0):
                    any_collisions = True
                    break

            if any_collisions: break
            playerpos += nudge.xy0

        # finalize playerpos
        globs.playerarray.update()

        # adjust the player sprite animation
        # compute the new label
        newlabel = "idle"
        if touch["bot"]:
            if velocity.x != 0: newlabel = "sneak"
            else: newlabel = "idle"
        else: newlabel = "move"

        # compute the direction the player is facing
        if velocity.x != 0:
            if velocity.x > 0: direction = "right"
            else: direction = "left"
        newlabel += "-" + direction

        # update the animator
        if globs.playerarray.asset.label != newlabel:
            globs.playerarray.asset.clear()
            globs.playerarray.asset.enqueue(globs.assets["dino-" + newlabel],
                                            loop=True,
                                            label=newlabel)
Ejemplo n.º 7
0
def play_loop():
    velocity = Vec(0, 0)
    dpad = Vec(0, 0)
    listen.launch(dpad_loop(dpad))
    direction = "right"

    collision_prec = 0.01
    collision_radius = 2

    playersize = globs.playerarray.asset["size"]
    gridsize = Vec(1, 1)

    recent_portal = None

    while True:
        events = yield from listen.any(frame=listen.event("on_frame"), )

        if globs.mode != "play": continue
        _, dt = events["frame"]

        playerpos = globs.playerarray.quads[0]

        # is the player touching something?
        touch = {
            "top": Vec(0, 1, 0),
            "bot": Vec(0, -1, 0),
            "left": Vec(-1, 0, 0),
            "right": Vec(1, 0, 0)
        }
        for key in touch:
            found = False
            for (x, y) in grid_positions_nearby(playerpos, collision_radius):
                if "dark" not in globs.grid[x, y]: continue
                if rects_intersect(Vec(x, y),
                                   playerpos + touch[key] * collision_prec,
                                   gridsize, playersize):
                    found = True
                    break
            touch[key] = found  # replace dict entry with boolean

        if touch["bot"]:
            # if grounded, make the vertical velocity 0
            velocity.y = 0

            # if the player is pressing up, make them jump
            if dpad.y == 1:
                velocity.y = 3
        else:
            # otherwise apply gravity
            velocity.y -= 3 * dt

        # horizontal movement
        velocity.x = dpad.x * 3

        # head bonk
        if touch["top"] and velocity.y > 0:
            velocity.y = 0

        # bonk on sides
        if touch["right"] and velocity.x > 0: velocity.x = 0
        if touch["left"] and velocity.x < 0: velocity.x = 0

        # compute the delta, and apply it with collision detection
        delta = velocity * dt

        # move the player. split the delta into lots of tiny nudges
        # and then apply as many as you can before we collide
        for nudge in split_delta(delta, collision_prec):
            any_collisions = False
            for (x, y) in grid_positions_nearby(playerpos, collision_radius):
                if "dark" not in globs.grid[x, y]: continue
                if rects_intersect(
                        Vec(x, y),
                        playerpos + nudge.xy0,
                        gridsize,
                        playersize,
                ):
                    any_collisions = True
                    break

            if any_collisions: break
            playerpos += nudge.xy0

        # finalize playerpos
        globs.playerarray.update()
        globs.circlearray.quads[0] = Vec(0, 0, 0) + playerpos
        globs.circlearray.quads[0] -= globs.circlearray.asset["size"].xy0 / 2
        globs.circlearray.quads[0] += globs.playerarray.asset["size"].xy0 / 2
        globs.circlearray.update()

        for (x, y) in grid_positions_nearby(playerpos, collision_radius):
            if globs.grid[x, y] != "portal": continue
            if not rect_in_rect(playerpos, Vec(x, y), playersize, gridsize):
                continue
            if (x, y) == recent_portal: continue
            mode_switch()
            velocity = Vec(0, 0)
            recent_portal = (x, y)

        if recent_portal is not None:
            if not rects_intersect(playerpos, Vec(*recent_portal), playersize,
                                   gridsize):
                recent_portal = None

        # adjust the camera if we are not falling
        if touch["bot"]:
            globs.cam["target_pos"] = -playerpos.xy + globs.cam["dims"] / 2

        # adjust the player sprite animation
        # compute the new label
        newlabel = "idle"
        if touch["bot"]:
            if velocity.x != 0: newlabel = "sneak"
            else: newlabel = "idle"
        else: newlabel = "move"

        # compute the direction the player is facing
        if velocity.x != 0:
            if velocity.x > 0: direction = "right"
            else: direction = "left"
        newlabel += "-" + direction

        # update the animator
        if globs.playerarray.asset.label != newlabel:
            globs.playerarray.asset.clear()
            globs.playerarray.asset.enqueue(globs.assets["dino-" + newlabel],
                                            loop=True,
                                            label=newlabel)