Advertisement
MaoChessy

Untitled

Nov 26th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Extension;
  4. using Plugins.HabObject;
  5. using Plugins.HabObject.GeneralProperty;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10.  
  11. namespace Gameplay.Builds.Data
  12. {
  13. [DisallowMultipleComponent]
  14. public class SizeOnMap : DataProperty
  15. {
  16. public Vector2Int Offset => _offset;
  17. public Vector2Int Size => _size;
  18.  
  19. public HabObject Hab => _hab != null ? _hab : _hab = transform.parent.GetComponent<HabObject>();
  20. [SerializeField]private HabObject _hab;
  21. [SerializeField] private Vector2Int _offset;
  22. [SerializeField] private Vector2Int _size;
  23.  
  24. private static AnimationCurve CurveX
  25. {
  26. get
  27. {
  28. if (_curveX != null)
  29. return _curveX;
  30. var keys = new Keyframe[5];
  31. int i = 0;
  32. keys[i] = new Keyframe(0,0);
  33. i++;
  34. keys[i] = new Keyframe(0.25f,0);
  35. i++;
  36. keys[i] = new Keyframe(0.5f,-1);
  37. i++;
  38. keys[i] = new Keyframe(0.75f,-1);
  39. i++;
  40. keys[i] = new Keyframe(1,0);
  41. _curveX = new AnimationCurve(keys);
  42. _curveX.postWrapMode = _curveX.preWrapMode = WrapMode.Loop;
  43. return _curveX;
  44. }
  45. }
  46. private static AnimationCurve CurveZ
  47. {
  48. get
  49. {
  50. if (_curveZ != null)
  51. return _curveZ;
  52. var keys = new Keyframe[5];
  53. int i = 0;
  54. keys[i] = new Keyframe(0,0);
  55. i++;
  56. keys[i] = new Keyframe(0.25f,-1);
  57. i++;
  58. keys[i] = new Keyframe(0.5f,-1);
  59. i++;
  60. keys[i] = new Keyframe(0.75f,0);
  61. i++;
  62. keys[i] = new Keyframe(1,0);
  63. _curveZ = new AnimationCurve(keys);
  64. _curveZ.postWrapMode = _curveZ.preWrapMode = WrapMode.Loop;
  65. return _curveZ;
  66. }
  67. }
  68.  
  69. private static AnimationCurve _curveX;
  70. private static AnimationCurve _curveZ;
  71.  
  72.  
  73. public static Vector3 GetModifacateXZ(float angelY)
  74. => new Vector3(CurveX.Evaluate(angelY / 360f), 0, (CurveZ.Evaluate(angelY / 360f)));
  75.  
  76. public static Vector3 SingModifacateXZ(float angelY)
  77. {
  78. var mod = GetModifacateXZ(angelY);
  79. return new Vector3(Mathf.Sign(mod.x), 0, Mathf.Sign(mod.z));
  80. }
  81.  
  82.  
  83. public List<Vector3> GetAllEmployedCell()
  84. {
  85. var result = new List<Vector3>();
  86.  
  87. MinPointAndMaxPoint(out var min, out var max);
  88.  
  89. var mod = GetModifacateXZ(Hab.transform.eulerAngles.y);
  90. CustomFor(min.x, max.x, 1, x =>
  91. {
  92. CustomFor(min.z,max.z, 1, z =>
  93. {
  94. var somePoint = new Vector3(x, 0, z)-mod;
  95. result.Add(somePoint);
  96. });
  97. });
  98. return result;
  99.  
  100. }
  101.  
  102. private Vector3 CeilVector(Vector3 posTocheck) =>
  103. new Vector3(Mathf.Ceil(posTocheck.x), Mathf.Ceil(posTocheck.y), Mathf.Ceil(posTocheck.z));
  104.  
  105. private void CustomFor(float x1, float x2, float step, Action<float> callback)
  106. {
  107. //x1 = Round2(x1);
  108. //x2 = Round2(x2);
  109. if(x1<x2)
  110. for (float i = x1; i < x2; i+=step)
  111. callback?.Invoke(i);
  112. else
  113. for (float i = x2; i < x1; i += step)
  114. callback?.Invoke(i);
  115. }
  116.  
  117.  
  118. private void MinPointAndMaxPoint(out Vector3 minPoint, out Vector3 maxPoint)
  119. {
  120. var posTocheck = Hab.transform.position;
  121. var prevVector = posTocheck;
  122. //posTocheck = CeilVector(posTocheck);
  123. Hab.transform.position = posTocheck;
  124.  
  125. minPoint = Hab.transform.position + (Hab.transform.forward * _offset.y + Hab.transform.right * _offset.x);
  126.  
  127. Vector2 sizeByAngel = SwapVector2Value(_size, Mathf.Abs(Mathf.Sin(Hab.transform.eulerAngles.y*Mathf.Deg2Rad)));
  128. maxPoint = minPoint
  129. + _size.x * Hab.transform.right
  130. + _size.y * Hab.transform.forward;
  131.  
  132.  
  133. maxPoint.x = Round(maxPoint.x);
  134. maxPoint.z = Round(maxPoint.z);
  135. minPoint.x = Round(minPoint.x);
  136. minPoint.z = Round(minPoint.z);
  137.  
  138. Hab.transform.position = prevVector;
  139. }
  140. #if UNITY_EDITOR
  141. [ContextMenu("Debug cells")]
  142. private void Debgub_GetAllEmployedCell()
  143. {
  144. string debugMes = "";
  145. int i = 1;
  146. GetAllEmployedCell().ForEach(x=>
  147. {
  148. debugMes += $"[{i}: x:{x.x} y:{x.y} z:{x.z}]\n";
  149. i++;
  150. });
  151.  
  152. Debug.Log(debugMes);
  153. }
  154.  
  155. private void OnDrawGizmos()
  156. {
  157. if(Application.isPlaying)
  158. return;
  159. DrawCvadrat();
  160. }
  161.  
  162. private void OnDrawGizmosSelected()
  163. {
  164. if(!Application.isPlaying)
  165. return;
  166. DrawCvadrat();
  167. }
  168.  
  169. private void DrawCvadrat()
  170. {
  171. MinPointAndMaxPoint(out var minPoint, out var maxPoint);
  172. var sing = SingModifacateXZ(Hab.transform.eulerAngles.y);
  173. minPoint -= sing / 2;
  174. maxPoint -= sing / 2;
  175. Handles.Label(minPoint, $"offset, start here x:{minPoint.x} y:{minPoint.y} z:{minPoint.z}");
  176. Handles.Label(maxPoint, $"size, end here x:{maxPoint.x} y:{maxPoint.y} z:{maxPoint.z}");
  177. Gizmos.DrawLine(minPoint, maxPoint);
  178. Gizmos.color = Color.red;
  179.  
  180. var p1 = new Vector3(minPoint.x, 0, maxPoint.z);
  181. var p2 = new Vector3(maxPoint.x, 0, minPoint.z);
  182.  
  183. Gizmos.DrawLine(maxPoint, p1);
  184. Gizmos.DrawLine(maxPoint, p2);
  185. Gizmos.DrawLine(minPoint, p1);
  186. Gizmos.DrawLine(minPoint, p2);
  187. }
  188. #endif
  189.  
  190. private float Round(float x)
  191. {
  192. return (float) Math.Round(x, 1);
  193.  
  194. }
  195.  
  196. private float Round2(float x)
  197. {
  198. var ostatok = x % 0.5f;
  199. if(ostatok>0)
  200. {
  201. if (ostatok < 0.25f)
  202. return x - ostatok;
  203. else
  204. return x + 0.5f - ostatok;
  205. }
  206. else
  207. {
  208. if (ostatok > -0.25f)
  209. return x - ostatok;
  210. else
  211. return x + 0.5f - ostatok;
  212. }
  213. }
  214.  
  215. public List<Vector3> GetAllEmployedCell(Vector3 position, Vector3 rotate)
  216. {
  217. var prevPos = Hab.transform.position;
  218. var prevRotate = Hab.transform.eulerAngles;
  219.  
  220. Hab.transform.position = position;
  221. Hab.transform.eulerAngles = rotate;
  222.  
  223. var result = GetAllEmployedCell();
  224.  
  225. Hab.transform.position = prevPos;
  226. Hab.transform.eulerAngles = prevRotate;
  227.  
  228. return result;
  229. }
  230.  
  231.  
  232.  
  233.  
  234. private Vector2 SwapVector2Value(Vector2 size, float normalProcent)
  235. {
  236. float allValue = size.x + size.y;
  237. float newX = (size.y * normalProcent) + (size.x * (1 - normalProcent));
  238. float newY = (size.x * normalProcent) + (size.y * (1 - normalProcent));
  239. return new Vector2(newX, newY);
  240. }
  241.  
  242. private void OnValidate()
  243. {
  244. if (_size.x <= 0)
  245. _size.x =1;
  246. if (_size.y <= 0)
  247. _size.y = 1;
  248. if (_hab == null)
  249. _hab = transform.parent.GetComponent<HabObject>();
  250. }
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement