Beispiel #1
0
    static public Vector3 DirectionToVector(OrthogonalDirection direction)
    {
        /*
         * Convert the given orthogonal direction to it's Vector equivalent
         */
        Vector3 directionVector = new Vector3(0, 0, 0);

        if (direction == OrthogonalDirection.Up)
        {
            directionVector = Vector3.up;
        }
        else if (direction == OrthogonalDirection.Right)
        {
            directionVector = Vector3.right;
        }
        else if (direction == OrthogonalDirection.Down)
        {
            directionVector = Vector3.down;
        }
        else if (direction == OrthogonalDirection.Left)
        {
            directionVector = Vector3.left;
        }

        return(directionVector);
    }
Beispiel #2
0
    private void UpdatePlayerPositions()
    {
        /*
         * Look at the inputs of each player and update their position relative
         * to the directions they are inputting.
         */

        for (int i = 0; i < players.Length; i++)
        {
            /*
             * Check the player's inputs and send a request to move the player
             */
            /* Get the amount of distance the player will travel */
            float travelDistance = players[i].defaultMovementSpeed * Time.deltaTime;

            /* Get the two directions the player has as inputs */
            OrthogonalDirection primaryDirection    = players[i].controls.GetPrimaryInput();
            OrthogonalDirection secondairyDirection = players[i].controls.GetSecondairyInput();
            /* The player has given a direction and a distance */
            if ((primaryDirection != OrthogonalDirection.NULL || secondairyDirection != OrthogonalDirection.NULL) && travelDistance > 0)
            {
                /* Request the player to commit to the given movement */
                players[i].MovePlayerRequest(primaryDirection, secondairyDirection, ref travelDistance);
                //Debug.Log("remaining distance: " + travelDistance);
            }
        }
    }
Beispiel #3
0
    public bool LinkLineStart(Line line, OrthogonalDirection direction)
    {
        /*
         * Link the given line's start corner to this corner along
         * the given direction. Return true if the connection was made.
         */
        bool connected = false;

        /* Make sure the line's start corner is on the same position as this corner */
        if (!position.Equals(line.start))
        {
            Debug.Log("WARNING: This corner does not connect to the given line's start corner");
            connected = false;
        }

        /* Set this corner's saved line to the given line */
        if (AddLine(line, direction))
        {
        }
        else
        {
            Debug.Log("WARNING: This corner already has a line linked to the given direction");
            connected = false;
        }

        return(connected);
    }
Beispiel #4
0
    static public OrthogonalDirection OppositeDirection(OrthogonalDirection direction)
    {
        /*
         * Return the opposite direction of the given direction
         */

        if (direction == OrthogonalDirection.Up)
        {
            direction = OrthogonalDirection.Down;
        }

        else if (direction == OrthogonalDirection.Right)
        {
            direction = OrthogonalDirection.Left;
        }

        else if (direction == OrthogonalDirection.Down)
        {
            direction = OrthogonalDirection.Up;
        }

        else if (direction == OrthogonalDirection.Left)
        {
            direction = OrthogonalDirection.Right;
        }

        return(direction);
    }
Beispiel #5
0
    public bool AddLine(Line newLine, OrthogonalDirection orthDir)
    {
        /*
         * Add the given line to this corner. Return true if it's added, false if not.
         */
        bool lineAdded = false;

        switch (orthDir)
        {
        case OrthogonalDirection.Up:
            lineAdded = SetLine(ref up, newLine);
            break;

        case OrthogonalDirection.Right:
            lineAdded = SetLine(ref right, newLine);
            break;

        case OrthogonalDirection.Down:
            lineAdded = SetLine(ref down, newLine);
            break;

        case OrthogonalDirection.Left:
            lineAdded = SetLine(ref left, newLine);
            break;

        default:
            lineAdded = false;
            break;
        }

        return(lineAdded);
    }
Beispiel #6
0
    public Vector3 GetCornerPositionInGivenDirection(OrthogonalDirection direction)
    {
        /*
         * Return the position where the line ends at in the given direction
         */
        Vector3 position = Vector3.zero;
        Vector3 newPosition = Vector3.zero;
        float   startPos, endPos;

        startPos = endPos = 0;

        /* If the given direction is a negative direction, flip the start and end values */
        bool flip = Corner.IsDirectionNegative(direction);

        /* Set the start and end values relative to their axis */
        startPos = Corner.GetVectorAxisValue(start, direction);
        endPos   = Corner.GetVectorAxisValue(end, direction);

        /* Do the comparasion */
        if (flip ^ (startPos > endPos))
        {
            newPosition = start;
        }
        else
        {
            newPosition = end;
        }

        return(newPosition);
    }
Beispiel #7
0
    static public OrthogonalDirection PreviousDirection(OrthogonalDirection direction)
    {
        /*
         * Return the "previous" direction in the sequence from the given direction.
         */

        if (direction == OrthogonalDirection.Up)
        {
            direction = OrthogonalDirection.Left;
        }

        else if (direction == OrthogonalDirection.Right)
        {
            direction = OrthogonalDirection.Up;
        }

        else if (direction == OrthogonalDirection.Down)
        {
            direction = OrthogonalDirection.Right;
        }

        else if (direction == OrthogonalDirection.Left)
        {
            direction = OrthogonalDirection.Down;
        }

        return(direction);
    }
Beispiel #8
0
    public Line AttachedLineAt(OrthogonalDirection direction)
    {
        /*
         * Return the line attached to this corner at the given direction
         */
        Line attachedLine = null;

        if (direction == OrthogonalDirection.Up)
        {
            attachedLine = up;
        }
        else if (direction == OrthogonalDirection.Right)
        {
            attachedLine = right;
        }
        else if (direction == OrthogonalDirection.Down)
        {
            attachedLine = down;
        }
        else if (direction == OrthogonalDirection.Left)
        {
            attachedLine = left;
        }

        return(attachedLine);
    }
Beispiel #9
0
    static public bool VertDirection(OrthogonalDirection direction)
    {
        /*
         * Return true if the given direction is vertical (up, down)
         */

        return(direction == OrthogonalDirection.Up || direction == OrthogonalDirection.Down);
    }
Beispiel #10
0
    static public bool HoriDirection(OrthogonalDirection direction)
    {
        /*
         * Return true if the given direction is horizontal (left, right)
         */

        return(direction == OrthogonalDirection.Left || direction == OrthogonalDirection.Right);
    }
Beispiel #11
0
    private void MovePlayer(OrthogonalDirection direction, float distance)
    {
        /*
         * Move the player's game position along the given direction for the given distance
         */

        gamePosition += Corner.DirectionToVector(direction) * distance;
    }
Beispiel #12
0
    public void AddWallNode(int x, int y, bool isCorner, MazeRoom room, OrthogonalDirection wallPosition)
    {
        MazeNode node = GetOrCreateNode(x, y);

        node.IsWall              = true;
        node.IsCornerWall        = isCorner;
        node.Room                = room;
        node.WallPosition        = wallPosition;
        node.Image               = CreateRectSprite(node.Rect, Color.white, FloorType.Room);
        node.Room.NumberOfNodes += 1;
    }
Beispiel #13
0
    static public bool IsDirectionsPerpendicular(OrthogonalDirection dir1, OrthogonalDirection dir2)
    {
        /*
         * Return true if dir2 is perpendicular to dir1
         */
        bool perpendicular = false;

        if (dir1.Equals(NextDirection(dir2)) || dir1.Equals(PreviousDirection(dir2)))
        {
            perpendicular = true;
        }

        return(perpendicular);
    }
Beispiel #14
0
    static public bool IsDirectionsOpposites(OrthogonalDirection dir1, OrthogonalDirection dir2)
    {
        /*
         * Return true if the two given directions are opposites
         */
        bool oppositeDirections = false;

        if (dir1.Equals(OppositeDirection(dir2)))
        {
            oppositeDirections = true;
        }

        return(oppositeDirections);
    }
Beispiel #15
0
    static public bool IsDirectionsParallel(OrthogonalDirection dir1, OrthogonalDirection dir2)
    {
        /*
         * Return true if dir2 is parallel to dir1
         */
        bool parallel = false;

        if (dir1.Equals(dir2) || dir1.Equals(OppositeDirection(dir2)))
        {
            parallel = true;
        }

        return(parallel);
    }
Beispiel #16
0
    public bool IsDirectionPerpendicular(OrthogonalDirection direction)
    {
        /*
         * Return whether the given direction is perpendicular (true) to this line
         */
        bool perpendicular = false;

        if (Corner.HoriDirection(direction) && IsVertical() ||
            Corner.VertDirection(direction) && IsHorizontal())
        {
            perpendicular = true;
        }

        return(perpendicular);
    }
Beispiel #17
0
    public bool IsDirectionParallel(OrthogonalDirection direction)
    {
        /*
         * Return whether the given direction is parallel to this line
         */
        bool parallel = false;

        if (Corner.HoriDirection(direction) && IsHorizontal() ||
            Corner.VertDirection(direction) && IsVertical())
        {
            parallel = true;
        }

        return(parallel);
    }
Beispiel #18
0
    public OrthogonalDirection LineSide(Line line)
    {
        /*
         * Return which side of this corner the given line is connected to.
         */
        Vector3             direction           = Vector3.zero;
        OrthogonalDirection orthogonalDirection = OrthogonalDirection.NULL;

        /* The line hits the corner in it's start point */
        if (position.Equals(line.start))
        {
            direction = line.end - line.start;
        }
        /* The line hits the corner in it's end point */
        else if (position.Equals(line.end))
        {
            direction = line.start - line.end;
        }
        /* The line's start/end points do not connect to this corner */
        else
        {
        }

        /* Get the orthoganal direction of the corner that the line is connected to */
        direction.Normalize();
        if (direction.Equals(Vector3.up))
        {
            orthogonalDirection = OrthogonalDirection.Up;
        }
        else if (direction.Equals(Vector3.right))
        {
            orthogonalDirection = OrthogonalDirection.Right;
        }
        else if (direction.Equals(Vector3.down))
        {
            orthogonalDirection = OrthogonalDirection.Down;
        }
        else if (direction.Equals(Vector3.left))
        {
            orthogonalDirection = OrthogonalDirection.Left;
        }
        else if (direction.Equals(Vector3.zero))
        {
            Debug.Log("WARNING: Trying to add a line with 0 length");
        }

        return(orthogonalDirection);
    }
Beispiel #19
0
    static public bool IsDirectionNegative(OrthogonalDirection direction)
    {
        /*
         * Return whether the given direction goes along the negative axis.
         * ie, up is along the positive Y axis, but down is along the negative Y axis.
         */
        bool negativeDirection = false;

        if (direction.Equals(OrthogonalDirection.Down) ||
            direction.Equals(OrthogonalDirection.Left))
        {
            negativeDirection = true;
        }

        return(negativeDirection);
    }
Beispiel #20
0
    public bool LineCollide(Line line1, Line line2)
    {
        /*
         * Return true if the two given lines collide with each other.
         * All lines travel across only 1 axis, so the collision detection
         * is assuming that each line only moves along one axis.
         *
         * Include the corners as a part of the lines.
         */
        bool collide = false;
        /* Get the directions of both lines */
        OrthogonalDirection line1Dir = line1.StartToEndDirection();
        OrthogonalDirection line2Dir = line2.StartToEndDirection();
        /* Get the start to end ranges of both lines */
        float line1Min = Mathf.Min(Corner.GetVectorAxisValue(line1.start, line1Dir), Corner.GetVectorAxisValue(line1.end, line1Dir));
        float line1Max = Mathf.Max(Corner.GetVectorAxisValue(line1.start, line1Dir), Corner.GetVectorAxisValue(line1.end, line1Dir));
        float line2Min = Mathf.Min(Corner.GetVectorAxisValue(line2.start, line2Dir), Corner.GetVectorAxisValue(line2.end, line2Dir));
        float line2Max = Mathf.Max(Corner.GetVectorAxisValue(line2.start, line2Dir), Corner.GetVectorAxisValue(line2.end, line2Dir));
        /* Get the flat axis value of each line */
        float line1Flat = Corner.GetVectorAxisValue(line1.start, Corner.NextDirection(line1Dir));
        float line2Flat = Corner.GetVectorAxisValue(line2.start, Corner.NextDirection(line2Dir));

        /* If the lines are parallel, they intercept if... */
        if (Corner.IsDirectionsParallel(line1Dir, line2Dir))
        {
            /* Both lines have the same flat value */
            if (line1Flat == line2Flat)
            {
                /* and both line's max > the other's min and vice versa */
                if (line1Min <= line2Max && line1Max >= line2Min)
                {
                    collide = true;
                }
            }
        }

        /* If they are not on the same axis, check if both's flats are within both's ranges */
        else
        {
            if ((line1Flat >= line2Min && line1Flat <= line2Max) && (line2Flat >= line1Min && line2Flat <= line1Max))
            {
                collide = true;
            }
        }

        return(collide);
    }
Beispiel #21
0
    public float DistanceToClosestGrid(Vector3 position, OrthogonalDirection direction, float gridSize, bool includeCorders)
    {
        /*
         * Given a position and direction on this line, return the distance to the closest grid mark.
         * The given boolean controls whether we should include nearby corners.
         */
        float pos = 0;
        float dir = 0;

        /* Get the values we want from the positions and direction depending on the line's orientation */
        if (IsHorizontal())
        {
            pos = position.x;
            dir = Corner.DirectionToVector(direction).x;
        }
        else if (IsVertical())
        {
            pos = position.y;
            dir = Corner.DirectionToVector(direction).y;
        }
        float remainder = pos % (gridSize);

        /* Ensure the given direction and position are actually on the line */
        if (IsDirectionPerpendicular(direction))
        {
            Debug.Log("WARNING: Given direction is perpendicular to this line");
        }
        if (!PointOnLine(position, true))
        {
            Debug.Log("WARNING: Given position is not on this line");
        }

        /* Change the nearest grid mark to match the orientation of the direction */
        if (dir > 0)
        {
            remainder = gridSize - remainder;
        }

        /* Check if the nearest corner is encountered before the grid mark */
        if (includeCorders)
        {
            remainder = Mathf.Min(remainder, DistanceToCorner(position, direction));
        }

        return(remainder);
    }
Beispiel #22
0
    public bool AddLine(Line newLine)
    {
        /*
         * Add the given line to this corner.
         *
         * Get the OrthogonalDirection the line is to this corner and call
         * the AddLine(Line, OrthogonalDirection) function.
         */
        bool lineAdded = false;

        /* Get the orthogonal direction of the line to this corner */
        OrthogonalDirection orthogonalDirection = LineSide(newLine);

        /* Try to add the line to this corner */
        lineAdded = AddLine(newLine, orthogonalDirection);

        return(lineAdded);
    }
Beispiel #23
0
    Line GetClosestLineTowards(OrthogonalDirection lineDirection, OrthogonalDirection playerDirection)
    {
        /*
         * Given a direction of a desired line and the direction of the player along their current line,
         * return the first line encountered along the playerDirection that is traveling towards lineDirection.
         */
        Line  desiredLine     = null;
        float minSnapDistance = 1;

        /* Get the first corner encountered along the player direction direction */
        float  leftDistance = minSnapDistance;
        Corner leftCorner   = currentLine.GetCornerInGivenDirection(playerDirection);

        while (leftCorner != null && desiredLine == null)
        {
            /* Get the distance from the player to this corner. If it's too far from the player, stop tracking */
            leftDistance = (leftCorner.position - gamePosition).magnitude;
            if (leftDistance < minSnapDistance)
            {
                /* Track the first line encountered that goes along the desired direction */
                desiredLine = leftCorner.AttachedLineAt(lineDirection);

                /* Get the next corner along the direction */
                if (leftCorner.AttachedLineAt(playerDirection) != null)
                {
                    leftCorner = leftCorner.AttachedLineAt(playerDirection).GetCornerInGivenDirection(playerDirection);
                }

                /* This was the last corner, so set the corner to null to stop the while loop */
                else
                {
                    leftCorner = null;
                }
            }

            /* Stop searching for the line if the corner is too far from the player */
            else
            {
                leftCorner = null;
            }
        }

        return(desiredLine);
    }
Beispiel #24
0
    private OrthogonalDirection ScanForLineInDirection(OrthogonalDirection direction)
    {
        /*
         * Scan ahead along the player's current line's both direction.
         * Return the direction along the line that points towards the closest line.
         */

        /* Get the first lines from both the player's current line which are travelling along the given direction */
        Line dir1Line = GetClosestLineTowards(direction, Corner.NextDirection(direction));
        Line dir2Line = GetClosestLineTowards(direction, Corner.PreviousDirection(direction));

        /* If two lines were found, pick the direction towards the closest one */
        if (dir1Line != null && dir2Line != null)
        {
            /* Get the distance between each line */
            float tempDistance1 = (dir1Line.GetCornerInGivenDirection(Corner.OppositeDirection(direction)).position - gamePosition).magnitude;
            float tempDistance2 = (dir2Line.GetCornerInGivenDirection(Corner.OppositeDirection(direction)).position - gamePosition).magnitude;

            /* Set the direction to the closest corner */
            if (tempDistance1 < tempDistance2)
            {
                direction = Corner.NextDirection(direction);
            }
            else
            {
                direction = Corner.PreviousDirection(direction);
            }
        }

        /* Properly set the direction to reflect if one or none lines were close enough */
        else if (dir1Line != null || dir2Line != null)
        {
            if (dir1Line != null)
            {
                direction = Corner.NextDirection(direction);
            }
            else if (dir2Line != null)
            {
                direction = Corner.PreviousDirection(direction);
            }
        }

        return(direction);
    }
Beispiel #25
0
    public static OrthogonalDirection ReturnHorizontalDirection(OrthogonalDirection dir1, OrthogonalDirection dir2)
    {
        /*
         * Given two directions, return the direction that points in a horizontal direction.
         * Return the NULL direction if neither of them are horizontal.
         */
        OrthogonalDirection horiDir = OrthogonalDirection.NULL;

        if (Corner.HoriDirection(dir1))
        {
            horiDir = dir1;
        }
        else if (Corner.HoriDirection(dir2))
        {
            horiDir = dir2;
        }

        return(horiDir);
    }
Beispiel #26
0
    public OrthogonalDirection GetSecondairyInput()
    {
        /*
         * Return the OrthogonalDirection of the second longest held directional
         * key that is not parallel to the primary inputted key.
         */
        OrthogonalDirection primaryInput    = GetPrimaryInput();
        OrthogonalDirection secondairyInput = OrthogonalDirection.NULL;

        /* Check whether the right or left inputs are pressed/which one for longer */
        if (primaryInput == OrthogonalDirection.Up || primaryInput == OrthogonalDirection.Down)
        {
            if (directionHoldTime[1] != 0 || directionHoldTime[3] != 0)
            {
                if (directionHoldTime[1] >= directionHoldTime[3])
                {
                    secondairyInput = OrthogonalDirection.Right;
                }
                else
                {
                    secondairyInput = OrthogonalDirection.Left;
                }
            }
        }

        /* Check whether the top or down inputs are pressed/which one for longer */
        else if (primaryInput == OrthogonalDirection.Right || primaryInput == OrthogonalDirection.Left)
        {
            if (directionHoldTime[0] != 0 || directionHoldTime[2] != 0)
            {
                if (directionHoldTime[0] >= directionHoldTime[2])
                {
                    secondairyInput = OrthogonalDirection.Up;
                }
                else
                {
                    secondairyInput = OrthogonalDirection.Down;
                }
            }
        }

        return(secondairyInput);
    }
Beispiel #27
0
    public static OrthogonalDirection ReturnVerticalDirection(OrthogonalDirection dir1, OrthogonalDirection dir2)
    {
        /*
         * Given two directions, return the direction that points in a vertical direction.
         * Return the NULL direction if neither of them are vertical.
         */
        OrthogonalDirection vertDir = OrthogonalDirection.NULL;

        if (Corner.VertDirection(dir1))
        {
            vertDir = dir1;
        }
        else if (Corner.VertDirection(dir2))
        {
            vertDir = dir2;
        }

        return(vertDir);
    }
Beispiel #28
0
    static public float GetVectorAxisValue(Vector3 vector, OrthogonalDirection axis)
    {
        /*
         * Given a vector and an axis represented as an orthogonal direction,
         * return the value of vector's given axis.
         * i.e.: a vector of (0, 1, 2) and a given axis of UP or DOWN would return 1.
         */
        float axisValue = 0;

        if (HoriDirection(axis))
        {
            axisValue = vector.x;
        }
        else if (VertDirection(axis))
        {
            axisValue = vector.y;
        }

        return(axisValue);
    }
Beispiel #29
0
    public void NewDrawingLine(OrthogonalDirection direction, bool addCorner)
    {
        /*
         * Put the player onto a new drawing line from their given position. The given
         * boolean determines whether we need to create a corner at the player's position.
         */

        /* Add a corner to the end of the current line if needed */
        if (addCorner)
        {
            if (currentLine.end.Equals(gamePosition))
            {
                /* Create the line and attach it to the corner */
                Corner newCorner = Corner.NewCorner(gamePosition);
                newCorner.AddLine(currentLine);

                /* Add the corner to the gameController so it can be rendered */
                gameController.AddCorner(newCorner);
            }
            else
            {
                Debug.Log("WARNING: Trying to add a corner not on the end of a line");
            }
        }

        /* Create a new line that the player will use to draw. Place the end point of
         * the line slightly forward to the line's a direction and not a point */
        Line drawLine = Line.NewLine(gamePosition, gamePosition);

        /* Once the line is added to the drawing list, change it's end point to move forward
         * so that it's no longer a point but a line. This will keep it's visuals unchanged
         * so that we cannot see the line but it's treated as such. */
        gameController.AddLine(drawLine);
        drawLine.end += Corner.DirectionToVector(direction);

        /* Link the new line's start corner to the corner's direction side */
        GetCorner().LinkLineStart(drawLine, direction);

        /* Move the player to the new line */
        ChangeCurrentLine(drawLine);
    }
Beispiel #30
0
    public Corner GetCornerInGivenDirection(OrthogonalDirection direction)
    {
        /*
         * Return the corner that the given direction points to.
         * If both corners are on the same position, return the start corner.
         */
        Corner  corner         = null;
        Vector3 cornerPosition = GetCornerPositionInGivenDirection(direction);

        /* Use GetCornerPositionInGivenDirection and compare the position of the corner */
        if (start.Equals(cornerPosition))
        {
            corner = startCorner;
        }
        else
        {
            corner = endCorner;
        }

        return(corner);
    }