TeshMap.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 计算人物在地图上的位置
  7. /// </summary>
  8. public class TeshMap : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 定位场景模型的2个点
  12. /// </summary>
  13. public Transform mapPointA;
  14. public Transform mapPointB;
  15. /// <summary>
  16. /// 角色
  17. /// </summary>
  18. public Transform player;
  19. /// <summary>
  20. /// 圆形地图
  21. /// </summary>
  22. public TestMapShow testMinMapShow;
  23. /// <summary>
  24. /// 长方形地图
  25. /// </summary>
  26. public TestMapShow testMaxMapShow;
  27. // public UdpClient udpClient;
  28. public Vector2 mapSize;
  29. /// <summary>
  30. /// 场景尺寸 比例
  31. /// </summary>
  32. public float disZ;
  33. public float disX;
  34. /// <summary>
  35. /// 场景所有水晶点
  36. /// </summary>
  37. // public List<Transform> listPoint;
  38. public LineRenderer line;
  39. List<Vector3> listPointPos;
  40. List<Vector3> listMapRoutePos;
  41. Dictionary<int, List<Vector3>> mapRoutePos;
  42. List<List<Vector3>> listmapRoute;
  43. Vector3 oldPos;
  44. float times;
  45. int Index = -1;
  46. private void Start()
  47. {
  48. listmapRoute = new List<List<Vector3>>();
  49. mapRoutePos = new Dictionary<int, List<Vector3>>();
  50. listMapRoutePos = new List<Vector3>();
  51. Debug.Log(mapPointA.eulerAngles + " mapPointA " + mapPointA.localEulerAngles);
  52. disX = Mathf.Abs(mapPointB.localPosition.x - mapPointA.localPosition.x);
  53. disZ = Mathf.Abs(mapPointB.localPosition.z - mapPointA.localPosition.z);
  54. }
  55. private void LateUpdate()
  56. {
  57. if (oldPos != player.localPosition)
  58. {
  59. oldPos = player.localPosition;
  60. MapPosRot();
  61. }
  62. times += Time.deltaTime;
  63. if (times > 0.5f && API_SVR.IsSvrInitialized())
  64. {
  65. times = 0;
  66. Vector2 mapPos = MapPos();
  67. Vector3 pos = new Vector3(mapSize.x * mapPos.x, mapSize.y * mapPos.y, 0.0f);
  68. string Ts = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
  69. // udpClient.SendIpToPointCloud(pos, Ts);
  70. HttpSocket.Instance.SendIpToPointPos(pos, Ts);
  71. }
  72. }
  73. /// <summary>
  74. /// 计算位置 ( 坐标和旋转)
  75. /// </summary>
  76. private void MapPosRot()
  77. {
  78. float x = Mathf.Abs(oldPos.x - mapPointA.localPosition.x)/disX;
  79. float z = (Mathf.Abs(oldPos.z - mapPointA.localPosition.z))/disZ;
  80. SettingMapPosRot(z, x);
  81. }
  82. private Vector2 MapPos()
  83. {
  84. float x = Mathf.Abs(oldPos.x - mapPointA.localPosition.x) / disX;
  85. float z = (Mathf.Abs(oldPos.z - mapPointA.localPosition.z)) / disZ;
  86. Vector2 pos = new Vector2(z, x);
  87. return pos;
  88. }
  89. private void SettingMapPosRot( float z, float x)
  90. {
  91. testMaxMapShow.ShowPlayer(z, x, player.transform.localEulerAngles.y + 179);
  92. testMinMapShow.MapRot(z, x, player.transform.localEulerAngles.y - 80);
  93. }
  94. public void SettingMapPoint(List<Transform> list_Pos)
  95. {
  96. //for (int i = 0; i < listPoint.Count; i++)
  97. //{
  98. // //listPoint[i].position = list_Pos[i + 3].position;
  99. //}
  100. // listPoint = list_Pos;
  101. MapPoint( list_Pos);
  102. }
  103. public void ADDMapRoutePoint(Vector3 pos , int Index)
  104. {
  105. //Debug.Log(pos);
  106. //if (this.Index == -1)
  107. // this.Index = Index;
  108. //if (Index != this.Index)
  109. //{
  110. // listMapRoutePos.Clear();
  111. // this.Index = Index;
  112. // // listmapRoute[Index] = new List<Vector3>();
  113. //}
  114. //if (Index == this.Index)
  115. // listMapRoutePos.Add(pos);
  116. if (listmapRoute.Count == 0 || listmapRoute.Count < Index + 1)
  117. listmapRoute.Add(new List<Vector3>());
  118. listmapRoute[Index].Add(pos);
  119. //if (!mapRoutePos.ContainsKey(Index))
  120. //{
  121. // listMapRoutePos = new List<Vector3>();
  122. // listMapRoutePos.Clear();
  123. // listMapRoutePos.Add(pos);
  124. // mapRoutePos.Add(Index, listMapRoutePos);
  125. //}
  126. // mapRoutePos[Index] = listMapRoutePos;
  127. }
  128. public void SettingMapRoute()
  129. {
  130. //for (int i = 0; i < listmapRoute.Count; i++)
  131. //{
  132. // for (int j = 0; j < listmapRoute[i].Count; j++)
  133. // {
  134. // Debug.Log(i+" "+listMapRoutePos[i] + " " + listmapRoute[i][j] );
  135. // }
  136. //}
  137. listMapRoutePos = listmapRoute[0];
  138. for (int i = 0; i < listMapRoutePos.Count; i++)
  139. {
  140. float x = Mathf.Abs(listMapRoutePos[i].x - mapPointA.localPosition.x) / disX;
  141. float z = Mathf.Abs(listMapRoutePos[i].z - mapPointA.localPosition.z) / disZ;
  142. listMapRoutePos[i] = new Vector3(z, x, 0);
  143. //Debug.Log(listMapRoutePos[i]);
  144. }
  145. testMaxMapShow.SettingMapRoute(listMapRoutePos);
  146. }
  147. /// <summary>
  148. /// 计算场景水晶点位置
  149. /// </summary>
  150. private void MapPoint(List<Transform> listPoint)
  151. {
  152. if (listPoint.Count <= 0)
  153. return;
  154. listPointPos = new List<Vector3>();
  155. for (int i = 0; i < listPoint.Count; i++)
  156. {
  157. float x = Mathf.Abs(listPoint[i].localPosition.x - mapPointA.localPosition.x) / disX;
  158. float z = Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) / disZ;
  159. // Debug.Log(Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) + " "+ disX);
  160. listPointPos.Add(new Vector3(z, x, 0));
  161. }
  162. testMaxMapShow.ShowPoint(listPointPos);
  163. testMinMapShow.LucencyMap();
  164. testMinMapShow.ShowPoint(listPointPos);
  165. testMinMapShow.LucencyMap();
  166. }
  167. }