Example #1
0
    /// <summary>
    /// Action that is invoked whenever the player changes its direction
    /// </summary>
    /// <param name="dir"></param>
    void OnDirectionChange(Utility.EDirection4 dir)
    {
        var animator = GetComponent <Animator>();

        switch (dir)
        {
        case Utility.EDirection4.None:
            animator.enabled = false;
            break;

        case Utility.EDirection4.Up:
            animator.enabled   = true;
            transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
            break;

        case Utility.EDirection4.Right:
            animator.enabled   = true;
            transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
            break;

        case Utility.EDirection4.Down:
            animator.enabled   = true;
            transform.rotation = Quaternion.AngleAxis(-90, Vector3.forward);
            break;

        case Utility.EDirection4.Left:
            animator.enabled   = true;
            transform.rotation = Quaternion.AngleAxis(180, Vector3.forward);
            break;

        default:
            throw new ArgumentOutOfRangeException("dir", dir, null);
        }
    }
Example #2
0
 public bool IsAllowed(Utility.EDirection4 dir)
 {
     if (dir == Utility.EDirection4.Up)
     {
         return(AllowUp);
     }
     if (dir == Utility.EDirection4.Right)
     {
         return(AllowRight);
     }
     if (dir == Utility.EDirection4.Down)
     {
         return(AllowDown);
     }
     if (dir == Utility.EDirection4.Left)
     {
         return(AllowLeft);
     }
     if (dir == Utility.EDirection4.None)
     {
         return(false);
     }
     throw new InvalidEnumArgumentException();
 }
Example #3
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");
    }