Beispiel #1
0
    // Called when the player inputs movement.
    private void OnMovement(Vector2 rawInputVector)
    {
        // Check for input
        Vector2 direction = rawInputVector.normalized;

        if (direction.magnitude == 0.0f)
        {
            moveDir = QuadDirection2D.none;
            return;
        }
        targetAngle = Mathf.Atan2(-direction.x, direction.y) * Mathf.Rad2Deg;
        // Rotate the eye to fit the new player input.
        if (!eyeCoroutineActive)
        {
            StartCoroutine(MoveEyes());
        }

        // Movement
        // Adjust speed
        float moveSpeed = isSlowWalking ? slowWalkSpeed : speed;

        moveSpeed       = isSprinting ? sprintSpeed : moveSpeed;
        gridMover.speed = moveSpeed;
        // Try to move in the right direction
        Direction2D horDir  = new Vector2(direction.x, 0).ToDirection2D();
        Direction2D vertDir = new Vector2(0, direction.y).ToDirection2D();

        moveDir = horDir.Add(vertDir);
    }
Beispiel #2
0
 public GridHit(Vector2 hitPos, QuadDirection2D fromDir, float hitSpeed, GameObject moverObject)
 {
     hitPosition = hitPos;
     direction   = fromDir;
     speed       = hitSpeed;
     moverObj    = moverObject;
 }
Beispiel #3
0
    public override bool Hit(GridHit hit)
    {
        // Determine which side the hitter is closer on, vertical or horizontal
        Vector2 diff   = hit.hitPosition - (Vector2)transform.position;
        bool    isHori = Mathf.Abs(diff.x) > Mathf.Abs(diff.y);

        slideDirection = hit.direction.ToDirection2D(isHori).ToQuadDirection2D();
        return(false);
    }
Beispiel #4
0
    public static Direction2D ToDirection2D(this QuadDirection2D curDir, bool prioritizeHorizontal = true)
    {
        if (curDir == QuadDirection2D.left)
        {
            return(Direction2D.left);
        }
        else if (curDir == QuadDirection2D.right)
        {
            return(Direction2D.right);
        }
        else if (curDir == QuadDirection2D.up)
        {
            return(Direction2D.up);
        }
        else if (curDir == QuadDirection2D.down)
        {
            return(Direction2D.down);
        }
        else if (curDir == QuadDirection2D.downLeft)
        {
            if (prioritizeHorizontal)
            {
                return(Direction2D.left);
            }
            return(Direction2D.down);
        }
        else if (curDir == QuadDirection2D.downRight)
        {
            if (prioritizeHorizontal)
            {
                return(Direction2D.right);
            }
            return(Direction2D.down);
        }
        else if (curDir == QuadDirection2D.upLeft)
        {
            if (prioritizeHorizontal)
            {
                return(Direction2D.left);
            }
            return(Direction2D.up);
        }
        else if (curDir == QuadDirection2D.upRight)
        {
            if (prioritizeHorizontal)
            {
                return(Direction2D.right);
            }
            return(Direction2D.up);
        }


        return(Direction2D.none);
    }
Beispiel #5
0
    public override bool Hit(GridHit hit)
    {
        Vector2 diff = hit.hitPosition - (Vector2)transform.position;
        // Scale the difference by the mover's size. The bigger magnitude of the result (x or y) is closer
        Vector2 distComparisonV = diff / transform.lossyScale;
        bool    isHori          = Mathf.Abs(distComparisonV.x) > Mathf.Abs(distComparisonV.y);

        gridMover.speed = hit.speed;
        QuadDirection2D dir = hit.direction.ToDirection2D(isHori).ToQuadDirection2D();

        return(gridMover.Move(dir));
    }
Beispiel #6
0
 /// <summary>Lets the player move if given true. Keeps the player from moving if given false.
 /// Subscribes and unsubscribes the movement function from the Movement Input Event</summary>
 public void AllowMovement(bool shouldAllow)
 {
     if (shouldAllow)
     {
         if (allowMove == false)
         {
             InputEvents.MovementEvent += OnMovement;
             allowMove = true;
         }
     }
     else
     {
         if (allowMove)
         {
             InputEvents.MovementEvent -= OnMovement;
             moveDir   = QuadDirection2D.none;
             allowMove = false;
         }
     }
 }
Beispiel #7
0
    private void Update()
    {
        Direction2D horiDir = Direction2D.none;
        Direction2D vertDir = Direction2D.none;

        float horiInput = Input.GetAxisRaw("Horizontal");
        float vertInput = Input.GetAxisRaw("Vertical");

        if (Mathf.Abs(horiInput) == 1.0f)
        {
            horiDir = new Vector2(horiInput, 0).ToDirection2D();
        }
        if (Mathf.Abs(vertInput) == 1.0f)
        {
            vertDir = new Vector2(0, vertInput).ToDirection2D();
        }

        QuadDirection2D moveDir = horiDir.Add(vertDir);

        if (moveDir != QuadDirection2D.none)
        {
            gridMover.Move(moveDir);
        }
    }
Beispiel #8
0
 private void OnGridCollision(Collider2D col)
 {
     slideDirection = QuadDirection2D.none;
 }
Beispiel #9
0
 /// <summary>
 /// Returns true if the given direction is opposite to this direction.
 /// Left and right are opposites. Down and up are opposites.
 /// No direction is opposite to none.
 /// </summary>
 /// <param name="otherDirection">Direction to check if it is opposite to the current direction.</param>
 /// <returns>If the given direction is opposite to the current direction.</returns>
 public bool IsOppositeDirection(QuadDirection2D otherDirection)
 {
     return((int)(direction) + (int)(otherDirection.direction) == 0);
 }