Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Tile : MonoBehaviour
- {
- private Vector3 firstPosition;
- private Vector3 finalPosition;
- private float swipeAngle;
- private Vector3 tempPosition;
- //Menampung data posisi tile
- public float xPosition;
- public float yPosition;
- public int column;
- public int row;
- private Grid grid;
- private GameObject otherTile;
- // Start is called before the first frame update
- void Start()
- {
- //Menentukan posisi dari tile
- grid = FindObjectOfType<Grid>();
- xPosition = transform.position.x;
- yPosition = transform.position.y;
- column = Mathf.RoundToInt((xPosition - grid.startPos.x) / grid.offset.x);
- row = Mathf.RoundToInt((yPosition - grid.startPos.y) / grid.offset.x);
- }
- // Update is called once per frame
- void Update()
- {
- xPosition = (column * grid.offset.x) + grid.startPos.x;
- yPosition = (row * grid.offset.y) + grid.startPos.y;
- SwipeTile();
- }
- void OnMouseDown()
- {
- //Mendapatkan titik awal sentuhan jari
- firstPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- }
- void OnMouseUp()
- {
- //Mendapatkan titik akhir sentuhan jari
- finalPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- CalculateAngle();
- }
- void CalculateAngle()
- {
- //Menghitung sudut antara posisi awal dan posisi akhir
- swipeAngle = Mathf.Atan2(finalPosition.y - firstPosition.y, finalPosition.x - firstPosition.x) * 180 / Mathf.PI;
- MoveTile();
- }
- void SwipeTile()
- {
- if (Mathf.Abs(xPosition - transform.position.x) > .1)
- {
- //Move towards the target
- tempPosition = new Vector2(xPosition, transform.position.y);
- transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
- }
- else
- {
- //Directly set the position
- tempPosition = new Vector2(xPosition, transform.position.y);
- transform.position = tempPosition;
- grid.tiles[column, row] = this.gameObject;
- }
- if (Mathf.Abs(yPosition - transform.position.y) > .1)
- {
- //Move towards the target
- tempPosition = new Vector2(transform.position.x, yPosition);
- transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
- }
- else
- {
- //Directly set the position
- tempPosition = new Vector2(transform.position.x, yPosition);
- transform.position = tempPosition;
- grid.tiles[column, row] = this.gameObject;
- }
- }
- void MoveTile()
- {
- if (swipeAngle > -45 && swipeAngle <= 45)
- {
- //Right swipe
- SwipeRightMove();
- }
- else if (swipeAngle > 45 && swipeAngle <= 135)
- {
- //Up swipe
- SwipeUpMove();
- }
- else if (swipeAngle > 135 || swipeAngle <= -135)
- {
- //Left swipe
- SwipeLeftMove();
- }
- else if (swipeAngle < -45 && swipeAngle >= -135)
- {
- //Down swipe
- SwipeDownMove();
- }
- }
- void SwipeRightMove()
- {
- //Menukar posisi dengan sebelah kanan nya
- otherTile = grid.tiles[column + 1, row];
- otherTile.GetComponent<Tile>().column -= 1;
- column += 1;
- }
- void SwipeUpMove()
- {
- //Menukar posisi dengan sebelah atasnya
- otherTile = grid.tiles[column, row + 1];
- otherTile.GetComponent<Tile>().row -= 1;
- row += 1;
- }
- void SwipeLeftMove()
- {
- //Menukar posisi dengan sebelah kirinya
- otherTile = grid.tiles[column - 1, row];
- otherTile.GetComponent<Tile>().column += 1;
- column -= 1;
- }
- void SwipeDownMove()
- {
- //Menukar posisi denhgan sebelah bawahnya
- otherTile = grid.tiles[column, row - 1];
- otherTile.GetComponent<Tile>().row += 1;
- row -= 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement