GameManage.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class GameManage : MonoSingleton<GameManage>
  6. {
  7. /// <summary>
  8. /// 显示物体
  9. /// </summary>
  10. public List<Transform> list_Obj;
  11. /// <summary>
  12. /// 物体触发范围 和 list_Obj 按下标对应
  13. /// </summary>
  14. public List<Transform> list_Box;
  15. /// <summary>
  16. /// 水晶点
  17. /// </summary>
  18. public List<Transform> list_Crystal;
  19. /// <summary>
  20. /// MeshRoute
  21. /// </summary>
  22. public GameObject meshRoute;
  23. public static string result;
  24. public static bool state = true;
  25. public bool isSlam = false;
  26. /// <summary>
  27. /// Json
  28. /// </summary>
  29. public Root data;
  30. [HideInInspector]
  31. public List<GameObject> list_route;
  32. public TeshMap teshMap;
  33. bool isInit = true;
  34. int index = 0;
  35. private void Start()
  36. {
  37. // BlueUdp.logMBSRD += AnalysisJson;
  38. list_route = new List<GameObject>();
  39. if (!isSlam)
  40. HttpSocket.Instance.SendIpToInitialize();
  41. else
  42. StartCoroutine(Test());
  43. }
  44. /// <summary>
  45. /// 处理Json 数据
  46. /// </summary>
  47. /// <param name="message"></param>
  48. public void AnalysisJson(string message)
  49. {
  50. if (!state)
  51. return;
  52. Debug.Log("Message aaa " + message);
  53. if (message == "" || message == "Message")
  54. {
  55. Debug.LogError("未接受到json 数据");
  56. //udpClient.SendIpToBluetooth();
  57. HttpSocket.Instance.SendIpToInitialize();
  58. return;
  59. }
  60. data = JsonControl.Instance.ToJsonData(message);
  61. // Debug.Log(data.navLines.Count);
  62. if (data == null)
  63. {
  64. //udpClient.SendIpToBluetooth();
  65. HttpSocket.Instance.SendIpToInitialize();
  66. return;
  67. }
  68. if (!isInit)
  69. return;
  70. state = false;
  71. isInit = false;
  72. // Debug.Log(data.projectId);
  73. for (int i = 0; i < data.navLines.Count; i++)
  74. {
  75. //for (int j = 0; j < data.navLines[i].points.Count; j++)
  76. //{
  77. // Debug.Log(data.navLines[i].points[j].MN);
  78. //}
  79. SettingLuJing(data.navLines[i], i);
  80. }
  81. SettingObj(data);
  82. //SettingLuJing(data);
  83. SettingLuJing(0);
  84. teshMap.SettingMapPoint(list_Crystal);
  85. teshMap.SettingMapRoute();
  86. }
  87. /// <summary>
  88. /// 设置路径
  89. /// </summary>
  90. private void SettingLuJing(Root data)
  91. {
  92. for (int i = 0; i < data.navLines.Count; i++)
  93. {
  94. for (int j = 0; j < data.navLines[i].points.Count - 1; j++)
  95. {
  96. GameObject route = GameObject.Instantiate(meshRoute, meshRoute.transform.parent);
  97. Vector3 endPos = new Vector3((float)data.navLines[i].points[j].X, 0, (float)data.navLines[i].points[j].Y);
  98. Vector3 exitPos = new Vector3((float)data.navLines[i].points[j + 1].X, 0, (float)data.navLines[i].points[j + 1].Y);
  99. Debug.Log(endPos + " " + exitPos);
  100. route.GetComponent<TestRoute>().SetRoute(endPos, exitPos);
  101. route.SetActive(true);
  102. }
  103. }
  104. }
  105. private void SettingLuJing(NavLinesItem data, int Index)
  106. {
  107. GameObject routes = new GameObject("Route" + Index.ToString());
  108. routes.transform.parent = meshRoute.transform.parent;
  109. for (int j = 0; j < data.points.Count - 1; j++)
  110. {
  111. GameObject route = GameObject.Instantiate(meshRoute, routes.transform);
  112. Vector3 endPos = new Vector3((float)data.points[j].X, 0, (float)data.points[j].Y);
  113. Vector3 exitPos = new Vector3((float)data.points[j + 1].X, 0, (float)data.points[j + 1].Y);
  114. // Debug.Log(endPos + " " + exitPos);
  115. route.GetComponent<TestRoute>().SetRoute(endPos, exitPos, Index);
  116. route.SetActive(true);
  117. // 地图显示路径
  118. teshMap.ADDMapRoutePoint(endPos, Index);
  119. }
  120. teshMap.ADDMapRoutePoint(new Vector3((float)data.points[data.points.Count - 1].X, 0, (float)data.points[data.points.Count - 1].Y), Index);
  121. // teshMap.SettingMapRoute();
  122. list_route.Add(routes);
  123. }
  124. public void SettingLuJing(int Index)
  125. {
  126. if (list_route == null || list_route.Count < Index)
  127. {
  128. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  129. return;
  130. }
  131. for (int i = 0; i < list_route.Count; i++)
  132. {
  133. list_route[i].SetActive(false);
  134. }
  135. list_route[Index].SetActive(true);
  136. }
  137. private void Update()
  138. {
  139. //if (state && UdpClient.logMBSRD != null)
  140. //{
  141. // Debug.Log(" UdpClient.logMBSRD");
  142. // UdpClient.logMBSRD(result);
  143. // // UdpClient.logMBSRD -= AnalysisJson;
  144. // //state = false;
  145. //}
  146. //times += Time.deltaTime;
  147. //if(times>0.5f && API_SVR.GetHead()!=null)
  148. //{
  149. // times = 0;
  150. // Vector3 pos = new Vector3(API_SVR.GetHead().position.x, API_SVR.GetHead().position.z, 0);
  151. // string Ts = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
  152. // udpClient.SendIpToPointCloud(pos, Ts);
  153. //}
  154. }
  155. /// <summary>
  156. /// 设置物体位置 和 触发范围
  157. /// </summary>
  158. /// <param name="data"></param>
  159. private void SettingObj(Root data)
  160. {
  161. for (int i = 0; i < data.navLines.Count; i++)
  162. {
  163. for (int j = 0; j < data.navLines[i].points.Count; j++)
  164. {
  165. if (data.navLines[i].points[j].T == 1)
  166. {
  167. switch (data.navLines[i].points[j].MN)
  168. {
  169. case "Party":
  170. QueryBox("Party", data.navLines[i].points[j]);
  171. break;
  172. case "Gongye":
  173. QueryBox("Gongye", data.navLines[i].points[j]);
  174. break;
  175. case "Subway":
  176. QueryBox("Subway", data.navLines[i].points[j]);
  177. break;
  178. case "Chemical":
  179. QueryBox("Chemical", data.navLines[i].points[j]);
  180. break;
  181. case "Library":
  182. //// 博物馆 前4个触发器改为一个触发器
  183. //int index = 0;
  184. //SettingBox(list_Box[list_Box.Count - 1], new Vector3((float)data.navLines[i].points[j].X, (float)data.navLines[i].points[j].Z, (float)data.navLines[i].points[j].Y));
  185. QueryBox("Museum", data.navLines[i].points[j]);
  186. //foreach (MPItem item in data.navLines[i].points[j].MP)
  187. //{
  188. // SettingBox(list_Obj[index++], item);
  189. //}
  190. break;
  191. case "YiLiao":
  192. QueryBox("YiLiao", data.navLines[i].points[j]);
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 查询
  203. /// </summary>
  204. /// <param name="Name"></param>
  205. /// <param name="points"></param>
  206. private void QueryBox(string Name, PointsItem points)
  207. {
  208. for (int i = 0; i < list_Obj.Count; i++)
  209. {
  210. if (list_Obj[i].name == Name)
  211. {
  212. SettingBox(list_Box[i], list_Obj[i], list_Crystal[i], points);
  213. Debug.Log("AAAAAAAA+ " + list_Box[i].name + list_Crystal[i].name + list_Obj[i].name);
  214. break;
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// 设置物体位置 和触发范围
  220. /// </summary>
  221. /// <param name="box">触发器</param>
  222. /// <param name="obj">显示物体</param>
  223. /// <param name="crystal">水晶点</param>
  224. /// <param name="points"></param>
  225. private void SettingBox(Transform box, Transform obj, Transform crystal, PointsItem points)
  226. {
  227. box.position = new Vector3((float)points.X, -1f, (float)points.Y);
  228. crystal.position = new Vector3(box.position.x, -3f, box.position.z);
  229. obj.position = new Vector3((float)points.MP[0].X, points.MP[0].Z == -1 ? obj.position.y : (float)points.MP[0].Z, (float)points.MP[0].Y);
  230. Debug.Log(obj.name + " " + obj.position);
  231. }
  232. private void SettingBox(Transform obj, MPItem mpItem)
  233. {
  234. obj.position = new Vector3((float)mpItem.X, mpItem.Z == -1 ? obj.position.y : (float)mpItem.Z, (float)mpItem.Y);
  235. }
  236. private void SettingBox(Transform obj, Vector3 mpItem)
  237. {
  238. //Debug.Log(obj.name);
  239. obj.position = new Vector3((float)mpItem.x, obj.position.y, (float)mpItem.y);
  240. list_Crystal[0].position = new Vector3(obj.position.x, -2.5f, obj.position.z);
  241. }
  242. IEnumerator Test()
  243. {
  244. WWW www = new WWW(Application.streamingAssetsPath + "/projectInfo.json");
  245. yield return www;
  246. string message = www.text;
  247. Debug.Log(message);
  248. AnalysisJson(message);
  249. }
  250. }