Advertisement
Guest User

Untitled

a guest
May 5th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HightLight : MonoBehaviour
  6. {
  7.  
  8. public GameObject HightLightPrefab;
  9. public List<HightLightHelper> MyHightlightsList;
  10. public Vector3 firstTilePoint = Vector3.zero;
  11.  
  12. private bool isxBackwards = false;
  13. private bool iszBackwards = false;
  14.  
  15.  
  16.  
  17. // Start is called before the first frame update
  18. void Awake()
  19. {
  20. MyHightlightsList = new List<HightLightHelper>();
  21. }
  22.  
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. int hightLightTileWidth = 0;
  27. int hightLightTileHeight = 0;
  28. Vector3 cuserTilePosition;
  29.  
  30. RaycastHit hit;
  31.  
  32.  
  33.  
  34. var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35.  
  36. if (Physics.Raycast(Ray, out hit, 1000))
  37. {
  38. Vector3 hitPoint = hit.point;
  39. cuserTilePosition = new Vector3(GetTilePointsX(hitPoint.x), 0, GetTilePointsZ(hit.point.z));
  40.  
  41. if (Input.GetMouseButtonDown(0))
  42. {
  43. firstTilePoint = cuserTilePosition;
  44. }
  45.  
  46. ClearTheHightLightList();
  47.  
  48. if (Input.GetMouseButton(0))
  49. {
  50.  
  51. hightLightTileWidth =( GetTilePointsX(hitPoint.x) - (int)firstTilePoint.x) + 1;
  52. hightLightTileHeight =( GetTilePointsZ(hitPoint.z) - (int)firstTilePoint.z) + 1;
  53.  
  54. if (hightLightTileHeight < 0)
  55. {
  56. iszBackwards = true;
  57. }
  58. else
  59. {
  60. iszBackwards = false;
  61. }
  62.  
  63. if (hightLightTileWidth < 0)
  64. {
  65. isxBackwards = true;
  66. }
  67. else
  68. {
  69. isxBackwards = false;
  70. }
  71.  
  72. hightLightTileHeight = Mathf.Abs(hightLightTileHeight);
  73. hightLightTileWidth = Mathf.Abs(hightLightTileWidth);
  74.  
  75. //Debug.Log(hightLightTileWidth + " " + hightLightTileHeight);
  76.  
  77. Debug.Log(isxBackwards + " " + iszBackwards);
  78.  
  79. Vector3 newPlacePosition = Vector3.zero;
  80.  
  81. for (int x = 0; hightLightTileWidth > x; x++)
  82. {
  83. for (int z = 0; hightLightTileHeight > z; z++)
  84. {
  85. if(isxBackwards == false && iszBackwards == false)
  86. {
  87. newPlacePosition = new Vector3(firstTilePoint.x + x, 0, firstTilePoint.z + z);
  88. }
  89. else if(isxBackwards == true && iszBackwards == true )
  90. {
  91. newPlacePosition = new Vector3(cuserTilePosition.x + x , 0, cuserTilePosition.z + z );
  92. }
  93. else
  94. if (isxBackwards == true && iszBackwards == false)
  95. {
  96. newPlacePosition = new Vector3(cuserTilePosition.x + x + 2 , 0, firstTilePoint.z + z);
  97. }
  98. else if(isxBackwards == false && iszBackwards == true)
  99. {
  100. newPlacePosition = new Vector3(firstTilePoint.x + x, 0, firstTilePoint.z + z);
  101. }
  102.  
  103. PlaceHightDown(newPlacePosition);
  104. }
  105. }
  106.  
  107. }
  108.  
  109. this.transform.localPosition = cuserTilePosition;
  110.  
  111. if (Input.GetKey(KeyCode.Escape))
  112. {
  113. ClearTheHightLightList();
  114. }
  115.  
  116. }
  117. }
  118.  
  119. private void ClearTheHightLightList()
  120. {
  121. for (int i = 0; i < MyHightlightsList.Count; i++)
  122. {
  123. HightLightHelper helperObject = MyHightlightsList[i];
  124. MyHightlightsList[i].Destroy();
  125. MyHightlightsList.Remove(helperObject);
  126. }
  127. }
  128.  
  129. private void PlaceHightDown(Vector3 tileSpace)
  130. {
  131. GameObject go = GameObject.Instantiate(HightLightPrefab);
  132. HightLightHelper h = go.GetComponent<HightLightHelper>();
  133. h.HightLight.transform.localPosition = tileSpace;
  134. MyHightlightsList.Add(h);
  135. }
  136.  
  137. private int GetTilePointsX(float worldPointx)
  138. {
  139. return (int)worldPointx;
  140. }
  141.  
  142. private int GetTilePointsZ(float worldPointz)
  143. {
  144. return (int)worldPointz;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement