Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Tile : MonoBehaviour
  6. {
  7. private Vector3 firstPosition;
  8. private Vector3 finalPosition;
  9. private float swipeAngle;
  10. private Vector3 tempPosition;
  11.  
  12. //Menampung data posisi tile
  13. public float xPosition;
  14. public float yPosition;
  15. public int column;
  16. public int row;
  17. private Grid grid;
  18. private GameObject otherTile;
  19.  
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. //Menentukan posisi dari tile
  24. grid = FindObjectOfType<Grid>();
  25. xPosition = transform.position.x;
  26. yPosition = transform.position.y;
  27. column = Mathf.RoundToInt((xPosition - grid.startPos.x) / grid.offset.x);
  28. row = Mathf.RoundToInt((yPosition - grid.startPos.y) / grid.offset.x);
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. xPosition = (column * grid.offset.x) + grid.startPos.x;
  35. yPosition = (row * grid.offset.y) + grid.startPos.y;
  36. SwipeTile();
  37. }
  38.  
  39. void OnMouseDown()
  40. {
  41. //Mendapatkan titik awal sentuhan jari
  42. firstPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  43. }
  44.  
  45. void OnMouseUp()
  46. {
  47. //Mendapatkan titik akhir sentuhan jari
  48. finalPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  49. CalculateAngle();
  50. }
  51.  
  52. void CalculateAngle()
  53. {
  54. //Menghitung sudut antara posisi awal dan posisi akhir
  55. swipeAngle = Mathf.Atan2(finalPosition.y - firstPosition.y, finalPosition.x - firstPosition.x) * 180 / Mathf.PI;
  56. MoveTile();
  57. }
  58.  
  59. void SwipeTile()
  60. {
  61. if (Mathf.Abs(xPosition - transform.position.x) > .1)
  62. {
  63. //Move towards the target
  64. tempPosition = new Vector2(xPosition, transform.position.y);
  65. transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
  66. }
  67. else
  68. {
  69. //Directly set the position
  70. tempPosition = new Vector2(xPosition, transform.position.y);
  71. transform.position = tempPosition;
  72. grid.tiles[column, row] = this.gameObject;
  73. }
  74.  
  75. if (Mathf.Abs(yPosition - transform.position.y) > .1)
  76. {
  77. //Move towards the target
  78. tempPosition = new Vector2(transform.position.x, yPosition);
  79. transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
  80. }
  81. else
  82. {
  83. //Directly set the position
  84. tempPosition = new Vector2(transform.position.x, yPosition);
  85. transform.position = tempPosition;
  86. grid.tiles[column, row] = this.gameObject;
  87. }
  88. }
  89.  
  90. void MoveTile()
  91. {
  92. if (swipeAngle > -45 && swipeAngle <= 45)
  93. {
  94. //Right swipe
  95. SwipeRightMove();
  96. }
  97. else if (swipeAngle > 45 && swipeAngle <= 135)
  98. {
  99. //Up swipe
  100. SwipeUpMove();
  101. }
  102. else if (swipeAngle > 135 || swipeAngle <= -135)
  103. {
  104. //Left swipe
  105. SwipeLeftMove();
  106. }
  107. else if (swipeAngle < -45 && swipeAngle >= -135)
  108. {
  109. //Down swipe
  110. SwipeDownMove();
  111. }
  112. }
  113.  
  114. void SwipeRightMove()
  115. {
  116. //Menukar posisi dengan sebelah kanan nya
  117. otherTile = grid.tiles[column + 1, row];
  118. otherTile.GetComponent<Tile>().column -= 1;
  119. column += 1;
  120. }
  121.  
  122. void SwipeUpMove()
  123. {
  124. //Menukar posisi dengan sebelah atasnya
  125. otherTile = grid.tiles[column, row + 1];
  126. otherTile.GetComponent<Tile>().row -= 1;
  127. row += 1;
  128. }
  129.  
  130. void SwipeLeftMove()
  131. {
  132. //Menukar posisi dengan sebelah kirinya
  133. otherTile = grid.tiles[column - 1, row];
  134. otherTile.GetComponent<Tile>().column += 1;
  135. column -= 1;
  136. }
  137.  
  138. void SwipeDownMove()
  139. {
  140. //Menukar posisi denhgan sebelah bawahnya
  141. otherTile = grid.tiles[column, row - 1];
  142. otherTile.GetComponent<Tile>().row += 1;
  143. row -= 1;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement