TeshMap.cs 5.6 KB

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