GearPart

Used for swiping and calculations
 avatar
unknown
plain_text
2 years ago
3.7 kB
14
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GearPart : MonoBehaviour
{
    private GameObject otherGearPart;

    private GridSetup grid;
    
    public int column;
    public int row;
   
    public int targetX;
    public int targetY;

    private Vector2 firstTouchPosition;
    private Vector2 finalTouchPosition;
    private Vector2 tempPosition;
    
    public float swipeAngle = 0;
    
    
    void Start()
    {
        grid = FindObjectOfType<GridSetup>();
        targetX = (int)transform.position.x;
        targetY = (int)transform.position.y;
        row = targetY;
        column = targetX;
    }

  
    void Update()
    {
        targetX = column;
        targetY = row;

        if(swipeAngle > 0)
        {
            if (Mathf.Abs(targetX - transform.position.x) > 0.1)
            {
                //Move Towards target 
                tempPosition = new Vector2(targetX, transform.position.y);
                transform.position = Vector2.Lerp(transform.position, tempPosition, 0.4f);
            }
            else
            {
                //Directly set the position
                tempPosition = new Vector2(targetX, transform.position.y);
                transform.position = tempPosition;
                grid.allGearParts[column, row] = this.gameObject;
            }

            if (Mathf.Abs(targetY - transform.position.y) > 0.1)
            {
                //Move Towards target 
                tempPosition = new Vector2(transform.position.x, targetY);
                transform.position = Vector2.Lerp(transform.position, tempPosition, 0.4f);
            }
            else
            {
                //Directly set the position
                tempPosition = new Vector2(transform.position.x, targetY);
                transform.position = tempPosition;
                grid.allGearParts[column, row] = this.gameObject;
            }
        }
       
    }

    private void OnMouseDown()
    {
        firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        
    }

    private void OnMouseUp()
    {
        finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        CalculateAngle();
    }

    void CalculateAngle()
    {
        swipeAngle = Mathf.Atan2(finalTouchPosition.y - firstTouchPosition.y, finalTouchPosition.x - firstTouchPosition.x) * 180/Mathf.PI;
        Debug.Log(swipeAngle);
        MovePieces();
    }

    void MovePieces()
    {
        if(swipeAngle > -45 && swipeAngle <= 45 && column < grid.width)
        {
            //right swipe
            otherGearPart = grid.allGearParts[column + 1, row];
            otherGearPart.GetComponent<GearPart>().column -= 1;
            column += 1;
        }

        else if (swipeAngle > 45 && swipeAngle <= 135 && row < grid.height)
        {
            //up swipe
            otherGearPart = grid.allGearParts[column, row + 1];
            otherGearPart.GetComponent<GearPart>().row -= 1;
            row += 1;
        }

        else if ((swipeAngle > 135 || swipeAngle <= -135) &&  column > 0)
        {
            //left swipe
            otherGearPart = grid.allGearParts[column - 1, row];
            otherGearPart.GetComponent<GearPart>().column += 1;
            column -= 1;
        }

        else if (swipeAngle < -45 && swipeAngle >= -135 && row > 0)
        {
            //down swipe
            otherGearPart = grid.allGearParts[column, row - 1];
            otherGearPart.GetComponent<GearPart>().row += 1;
            row -= 1;
        }
    }
}
Editor is loading...
Leave a Comment