Beispiel #1
0
    /// <summary>
    /// Tried to implement a 'jump' function which does not (yet) work properly without glitching out
    /// </summary>
    /// <param name="dir"></param>
    private void Jump(Utility.EDirection4 dir)
    {
        // Check if jumping the distance does the job
        var endPos = (Vector2)transform.position + dir.ToVector2() * JumpDistance;

        foreach (var connection in LevelInfo.NodeConnections)
        {
            if (connection.IsIntersectingWith(new LineSegment2D(endPos - Vector2.one * .01f, endPos + Vector2.one * .01f)))
            {
                _jumpPos          = endPos;
                _animator.enabled = true;
                _animator.Play("PlayerTeleport");
                return;
            }
        }

        // Jump over a wall
        LineSegment2D        jumpLine;
        List <LineSegment2D> validLines;
        Vector3 newPos;

        switch (dir)
        {
        case Utility.EDirection4.Up:
            // The line to move over for the jump
            jumpLine = new LineSegment2D(transform.position, new Vector2(transform.position.x, LevelInfo.LevelSize.max.y));
            // Get all lines intersecting with it, sorted according to y-value
            validLines =
                LevelInfo.NodeConnections.Where(
                    line => (Line2D)line != (Line2D)jumpLine && line.IsIntersectingWith(jumpLine) &&
                    // ReSharper disable PossibleInvalidOperationException
                    line.GetIntersectionWith(jumpLine).Value.y > transform.position.y)
                .OrderBy(line => line.GetIntersectionWith(jumpLine).Value.y)
                // ReSharper restore PossibleInvalidOperationException
                .ToList();

            // Jump to new position
            newPos = new Vector3(transform.position.x, validLines.First().Start.y, transform.position.z);
            break;

        case Utility.EDirection4.Right:
            // The line to move over for the jump
            jumpLine = new LineSegment2D(transform.position, new Vector2(LevelInfo.LevelSize.max.x, transform.position.y));
            // Get all lines intersecting with it, sorted according to y-value
            validLines =
                LevelInfo.NodeConnections.Where(
                    line => (Line2D)line != (Line2D)jumpLine && line.IsIntersectingWith(jumpLine) &&
                    // ReSharper disable PossibleInvalidOperationException
                    line.GetIntersectionWith(jumpLine).Value.x > transform.position.x)
                .OrderBy(line => line.GetIntersectionWith(jumpLine).Value.x)
                // ReSharper restore PossibleInvalidOperationException
                .ToList();

            // Jump to new position
            newPos = new Vector3(validLines.First().Start.x, transform.position.y, transform.position.z);
            break;

        case Utility.EDirection4.Down:
            // The line to move over for the jump
            jumpLine = new LineSegment2D(transform.position, new Vector2(transform.position.x, LevelInfo.LevelSize.min.y));
            // Get all lines intersecting with it, sorted according to y-value
            validLines =
                LevelInfo.NodeConnections.Where(
                    line => (Line2D)line != (Line2D)jumpLine && line.IsIntersectingWith(jumpLine) &&
                    // ReSharper disable PossibleInvalidOperationException
                    line.GetIntersectionWith(jumpLine).Value.y < transform.position.y)
                .OrderByDescending(line => line.GetIntersectionWith(jumpLine).Value.y)
                // ReSharper restore PossibleInvalidOperationException
                .ToList();

            // Jump to new position
            newPos = new Vector3(transform.position.x, validLines.First().Start.y, transform.position.z);
            break;

        case Utility.EDirection4.Left:
            // The line to move over for the jump
            jumpLine = new LineSegment2D(transform.position, new Vector2(LevelInfo.LevelSize.min.x, transform.position.y));
            // Get all lines intersecting with it, sorted according to y-value
            validLines =
                LevelInfo.NodeConnections.Where(
                    line => (Line2D)line != (Line2D)jumpLine && line.IsIntersectingWith(jumpLine) &&
                    // ReSharper disable PossibleInvalidOperationException
                    line.GetIntersectionWith(jumpLine).Value.x < transform.position.x)
                .OrderByDescending(line => line.GetIntersectionWith(jumpLine).Value.x)
                // ReSharper restore PossibleInvalidOperationException
                .ToList();

            // Jump to new position
            newPos = new Vector3(validLines.First().Start.x, transform.position.y, transform.position.z);
            break;

        default:
            throw new ArgumentOutOfRangeException("dir", dir, null);
        }
        _jumpPos          = newPos;
        _animator.enabled = true;
        _animator.Play("PlayerTeleport");
    }