PosCommunication.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using BestHTTP.JSON;
  6. using System;
  7. using System.Linq.Expressions;
  8. public class PosCommunication : MonoBehaviour
  9. {
  10. // Start is called before the first frame update
  11. public Data_PlayerAttributes dataPA;//生成多个
  12. public Data_PlayerAttributesOne dataPAOne;
  13. private int index;
  14. public string player_name_InputName;
  15. public string player_typeOfWork;
  16. public string player_ID;
  17. public string playerAction;
  18. public List<string> onlePlayerID = new List<string>();
  19. public List<string> makeTypeOfWorkID = new List<string>();
  20. private GameObject TypeOfWork;
  21. public List<GameObject> playerTypeOfWorksList = new List<GameObject>();//玩家自身
  22. public List<GameObject> playerMakeTypeOfWorksList = new List<GameObject>();//玩家创造的单位
  23. public List<Data_PlayerAttributes> dataPlayerAttributesList = new List<Data_PlayerAttributes>();
  24. public List<Data_PlayerAttributesOne> dataPlayerAttributesListOne = new List<Data_PlayerAttributesOne>();
  25. public MakeEnemyManager makeEnemyManager;
  26. public MakeEneutralManager makeEneutralManager;
  27. public bool isMakeTypeOfWork = false;//是否制造了单位
  28. public EnemyMessagerPanelManager enemyMessagerPanelManager_Scenes5;//跟随视野旋转 左右上下
  29. public EnemyMessagerPanelManager enemyMessagerPanelManager_2D;//固定位置界面
  30. public EnemyMessagerPanelManager enemyMessagerPanelManager_3D;//跟随视野旋转,左右 不上下
  31. private GameManager gameManager;
  32. float gpsX, gpsY, gpsZ;
  33. public static bool IsPlayerjoin = false;//判断玩家是否加入,false为未加入;
  34. private Vector3 thisGpsV3;
  35. private Vector3 thisSlamV3;
  36. public static float webMM = 0;
  37. void Start()
  38. {
  39. plugin = SvrPlugin.Instance;
  40. gpsNumType = 0;
  41. gameManager = this.GetComponent<GameManager>();
  42. WSHandler.init();
  43. WSHandler.User.OnLogin += AcceptMessage;
  44. TypeOfWork = Resources.Load("PlayerTypeOfWork") as GameObject;
  45. //player_name_InputName = "FriendlyForcesTypeOfWork1"+","+" ";
  46. //Debug.Log(makeEnemyManager.enemyList.Count);
  47. playerTypeOfWorksList.Clear();
  48. MakeThisBady();//创造自己
  49. }
  50. int iaaaa = 0;
  51. public static int currentMinMapFitIndex;
  52. private string strInitPoint;
  53. void AcceptMessage(JsonData data)
  54. {
  55. Debug.Log("wo zai jieshou");
  56. // Debug.Log(data.ToJson());
  57. dataPAOne = JsonMapper.ToObject<Data_PlayerAttributesOne>(data.ToJson());//没有创造单位
  58. dataPA = JsonMapper.ToObject<Data_PlayerAttributes>(data.ToJson());//创造单位
  59. if (dataPA.data.message.dataEnemy.Count <= 0)
  60. {
  61. IsPlayerjoin = true;
  62. Debug.Log("我在接收,没有人造单位");
  63. if (!onlePlayerID.Contains(dataPAOne.data.message.data2.id))
  64. {
  65. currentMinMapFitIndex += 1;
  66. Debug.Log((iaaaa += 1).ToString() + "yourenjinlaile");
  67. onlePlayerID.Add(dataPAOne.data.message.data2.id);//ID 单个人
  68. dataPlayerAttributesListOne.Add(dataPAOne);
  69. strInitPoint = dataPlayerAttributesListOne[0].data.message.data2.des;
  70. GameObject playerTypeOfWork = Instantiate(TypeOfWork, new Vector3(999, 999, 999), Quaternion.identity);//单个人
  71. //playerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform;
  72. playerTypeOfWorksList.Add(playerTypeOfWork);
  73. Debug.Log(playerTypeOfWorksList.Count + "nnnnnnnnnnnnnnnnnnnnnn");
  74. }
  75. for (int i = 0; i < dataPlayerAttributesListOne.Count; i++)
  76. {
  77. if (dataPlayerAttributesListOne[i].data.message.data2.id.Contains(dataPAOne.data.message.data2.id))
  78. {
  79. dataPlayerAttributesListOne[i] = dataPAOne;
  80. }
  81. // Debug.Log(dataPlayerAttributesList[i].data.message.data2.pos);
  82. }
  83. }
  84. else
  85. {
  86. // Debug.Log("有人创造了" + dataPA.data.message.dataEnemy.Count + "个造单位");
  87. for (int i = 0; i < dataPA.data.message.dataEnemy.Count; i++)
  88. {
  89. if (!makeTypeOfWorkID.Contains(dataPA.data.message.dataEnemy[i].id))
  90. {
  91. currentMinMapFitIndex = makeTypeOfWorkID.Count + 1;
  92. // Debug.Log("加入了一个创建的单位");
  93. // Debug.Log("用户" + dataPA.data.message.data2.id + "生成了" + dataPA.data.message.dataEnemy.Count + "个单位");
  94. makeTypeOfWorkID.Add(dataPA.data.message.dataEnemy[i].id);
  95. dataPlayerAttributesList.Add(dataPA);
  96. GameObject playerTypeOfWork = Instantiate(TypeOfWork, new Vector3(999, 999, 999), Quaternion.identity);//单个人
  97. // playerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform;
  98. playerTypeOfWork.name = "UserMakeTypeOfWork";
  99. playerMakeTypeOfWorksList.Add(playerTypeOfWork);
  100. playerTypeOfWork.GetComponent<MiniMapFit>().id = dataPA.data.message.dataEnemy[i].id;
  101. playerTypeOfWork.GetComponent<MiniMapFit>().action = dataPA.data.message.dataEnemy[i].action;
  102. playerTypeOfWork.GetComponent<MiniMapFit>().des = dataPA.data.message.dataEnemy[i].des;
  103. playerTypeOfWork.GetComponent<MiniMapFit>().pos = dataPA.data.message.dataEnemy[i].pos;
  104. playerTypeOfWork.GetComponent<MiniMapFit>().eul = dataPA.data.message.dataEnemy[i].eul;
  105. playerTypeOfWork.GetComponent<MiniMapFit>().GpsPlayerPosStr = dataPA.data.message.dataEnemy[i].gpsPos;
  106. playerTypeOfWork.GetComponent<MiniMapFit>().typeOfWork = dataPA.data.message.dataEnemy[i].typeOfWork;
  107. playerTypeOfWork.GetComponent<MiniMapFit>().thisCurrentMinMapFitIndex = currentMinMapFitIndex+MakeEneutralManager.MakeEenmyIndex;
  108. MakeEneutralManager.ChangeEnemyIndex(dataPA.data.message.dataEnemy[i].des);
  109. webMM = Time.timeSinceLevelLoad;
  110. Debug.Log("webMM "+ webMM);
  111. if (gameManager.scenesName == "scenes3")
  112. {
  113. enemyMessagerPanelManager_2D.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork);
  114. enemyMessagerPanelManager_2D.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos);
  115. enemyMessagerPanelManager_2D.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos);
  116. enemyMessagerPanelManager_2D.ShowOrHeidThisPanel("Show");
  117. }
  118. else if (gameManager.scenesName == "scenes4")
  119. {
  120. enemyMessagerPanelManager_3D.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork);
  121. enemyMessagerPanelManager_3D.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos);
  122. enemyMessagerPanelManager_3D.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos);
  123. enemyMessagerPanelManager_3D.ShowOrHeidThisPanel("Show");
  124. }
  125. else if (gameManager.scenesName == "scenes5")
  126. {
  127. enemyMessagerPanelManager_Scenes5.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork);
  128. enemyMessagerPanelManager_Scenes5.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos);
  129. enemyMessagerPanelManager_Scenes5.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos);
  130. enemyMessagerPanelManager_Scenes5.ShowOrHeidThisPanel("Show");
  131. }
  132. }
  133. }
  134. //for (int i = 0; i < dataPlayerAttributesList.Count; i++) //敌人位置同步
  135. //{
  136. // if (dataPlayerAttributesList[i].data.message.dataEnemy[i].id.Contains(dataPA.data.message.dataEnemy[i].id))
  137. // {
  138. // dataPlayerAttributesList[i] = dataPA;
  139. // }
  140. //}
  141. }
  142. }
  143. private SvrPlugin plugin = null;
  144. float[] gness = new float[3];
  145. double dt; //20米开外的值
  146. //float[] dtnull = new float[3];//20米开外的值(测试);
  147. public TextMesh textM1, textM2, textM3;
  148. private string initPoint;
  149. private bool isB;
  150. public static Vector3 shadowPosStatic;
  151. public static double gpsNumType = 0;//0为正常数据。20为前方20米数据
  152. public static Vector3 enemyGpsenemyPos;//敌人Gps数据
  153. // public static Vector3 playerGpsPos;
  154. public LineRenderer lineX;
  155. public LineRenderer lineZ;
  156. IEnumerator Initialize()
  157. {
  158. yield return new WaitUntil(() => plugin.IsInitialized() == true);
  159. // plugin.getGnss(ref gpsNumType, gness);
  160. Debug.Log(gness + "Gps = {" + gness[0] + "," + gness[1] + "," + gness[2] + "," + "}");
  161. gpsX = gness[0];
  162. gpsY = 0;
  163. gpsZ = gness[1];
  164. textM1.text = "gpsX:" + gness[0].ToString();
  165. textM2.text = "gpsY:" + gness[1].ToString();
  166. textM3.text = "gps高:" + gness[2].ToString();
  167. shadowPosStatic = new Vector3(gpsX, gpsY, gpsZ);
  168. if (isB)
  169. {
  170. initPoint = "." + gpsX + "," + gpsY + "," + gpsZ;
  171. isB = false;
  172. strInitPoint = initPoint;
  173. }
  174. }
  175. private void ShowLine()
  176. {
  177. lineX.gameObject.SetActive(true);
  178. lineZ.gameObject.SetActive(true);
  179. lineX.SetPosition(0, API_SVR.GetHead().transform.position + new Vector3(-20f, -1, 0));
  180. lineX.SetPosition(1, API_SVR.GetHead().transform.position + new Vector3(20f, -1, 0));
  181. lineZ.SetPosition(0, API_SVR.GetHead().transform.position + new Vector3(0, -1, -20f));
  182. lineZ.SetPosition(1, API_SVR.GetHead().transform.position + new Vector3(0, -1, 20f));
  183. }
  184. private GameObject thisPlayerTypeOfWork;
  185. private void MakeThisBady()//创造自己
  186. {
  187. Debug.Log("wo chan sheng le zi ji");
  188. thisPlayerTypeOfWork = Instantiate(TypeOfWork, new Vector3(10, 10, 10), Quaternion.identity);//单个人
  189. //thisPlayerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform;
  190. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().id = player_ID;
  191. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().action = "ThisPlayerUser";
  192. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().des = "MakeThisPlayerUser";
  193. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().typeOfWork = player_typeOfWork;
  194. }
  195. public void SendMyMessage()
  196. {
  197. WSHandler.User.testLogin(id: player_name_InputName);
  198. Vector3 shadowPos = SvrManager.Instance.head.position;
  199. shadowPosStatic = shadowPos;
  200. //Debug.Log("shadowPos: " + shadowPos);
  201. textM1.text = "X:" + shadowPos.x.ToString();
  202. textM2.text = "Z:" + shadowPos.z.ToString();
  203. textM3.text = "y:" + shadowPos.y.ToString();
  204. Vector3 shadowRot = new Vector3(SvrManager.Instance.head.rotation.x, SvrManager.Instance.head.rotation.y, SvrManager.Instance.head.rotation.z);
  205. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().pos = shadowPos.x + "_" + shadowPos.y + "_" + shadowPos.z;
  206. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().eul = SvrManager.Instance.head.rotation.x + "_" + SvrManager.Instance.head.rotation.y + "_" + SvrManager.Instance.head.rotation.z;
  207. thisPlayerTypeOfWork.GetComponent<MiniMapFit>().GpsPlayerPos = shadowPos;
  208. // thisPlayerTypeOfWork.GetComponent<MiniMapFit>().GpsPlayerPosStr = 11 + "_" +"0" + "_" + 12;
  209. //上一版本传输 传输 GPS坐标
  210. UpdateAttitude(shadowPos, shadowRot, player_ID, player_name_InputName, player_typeOfWork, makeEneutralManager.typeOfEnutralOrenemyList, playerAction, initPoint);
  211. // UpdateAttitude(new Vector3(SvrManager.Instance.head.transform.position.x , SvrManager.Instance.head.transform.position.y , SvrManager.Instance.head.transform.position.z), shadowRot, player_ID, player_name_InputName, player_typeOfWork, makeEneutralManager.typeOfEnutralOrenemyList, playerAction, initPoint);
  212. }
  213. float time;
  214. private bool isMakeAIaircraft = true;
  215. void Update()
  216. {
  217. // ShowLine();
  218. // StartCoroutine(Initialize());
  219. time = time + Time.deltaTime;
  220. if (time > 0.3f)
  221. {
  222. Debug.Log("我在发送");
  223. SendMyMessage();
  224. time = 0;
  225. }
  226. }
  227. public void UpdateAttitude(Vector3 pos, Vector3 eul, string id, string describe, string TypeOfWork, List<GameObject> games, string PlayerAction, string initStrPoint)
  228. {
  229. JsonData message = new JsonData();
  230. JsonData data = new JsonData();
  231. JsonData data2 = new JsonData();
  232. message["notification"] = true;
  233. message["method"] = "transmit";
  234. message["data"] = data;
  235. data["data2"] = data2;
  236. data2["action"] = PlayerAction;
  237. data2["id"] = id;
  238. data2["des"] = describe;
  239. data2["pos"] = pos.x + "_" + pos.y + "_" + pos.z;
  240. data2["eul"] = eul.x + "_" + eul.y + "_" + eul.z;
  241. data2["typeOfWork"] = TypeOfWork;
  242. if (games.Count > 0)
  243. {
  244. JsonData dataEnemy = new JsonData();
  245. data["dataEnemy"] = new JsonData();
  246. for (int i = 0; i < games.Count; i++)
  247. {
  248. JsonData dataEnemys = new JsonData();
  249. // data["dataEnemy" +i] = dataEnemys;
  250. dataEnemys["action"] = games[i].GetComponent<MiniMapFit>().action;
  251. dataEnemys["id"] = games[i].GetComponent<MiniMapFit>().id;
  252. dataEnemys["des"] = games[i].GetComponent<MiniMapFit>().des;
  253. dataEnemys["pos"] = games[i].GetComponent<MiniMapFit>().pos;
  254. dataEnemys["eul"] = games[i].GetComponent<MiniMapFit>().eul;
  255. dataEnemys["gpsPos"] = games[i].GetComponent<MiniMapFit>().GpsPlayerPosStr;
  256. dataEnemys["typeOfWork"] = games[i].GetComponent<MiniMapFit>().typeOfWork;
  257. data["dataEnemy"].Add(dataEnemys);
  258. }
  259. }
  260. if (makeTypeOfWorkID.Count <= 0 && isMakeAIaircraft)
  261. {
  262. //造一个无人机
  263. // makeEneutralManager.MakeAIAircraftEneutra("AIAircraft", gameManager.gpsPosInitx, 100, gameManager.gpsPosInitz);
  264. isMakeAIaircraft = false;
  265. }
  266. WSHandler.SendMessage(message);
  267. }
  268. }