ControllerPanel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Mirror;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using FLOW;
  6. using GFMain;
  7. using UnityCommon;
  8. using UnityEngine.UI;
  9. namespace DTYJ
  10. {
  11. public class ControllerPanel : NetworkBehaviour
  12. {
  13. public static ControllerPanel instance;
  14. private void Awake()
  15. {
  16. instance = this;
  17. ShowPanel(false);
  18. InitDeng();
  19. InitShuiZhai();
  20. }
  21. public GameObject goPanelRoot;
  22. private void Update()
  23. {
  24. if (Input.GetKeyUp(KeyCode.P))
  25. {
  26. if (!isServer) return;
  27. SwitchShowPanel();
  28. }
  29. if (!OnlineCtrl.Instance.xietongServerIsConnected)
  30. UpdateTrain();
  31. }
  32. private void SwitchShowPanel()
  33. {
  34. ShowPanel(!goPanelRoot.activeSelf);
  35. }
  36. public void ShowPanel(bool show)
  37. {
  38. goPanelRoot.SetActive(show);
  39. }
  40. #region 水灾
  41. Dictionary<string, GameObject> dicGos = new Dictionary<string, GameObject>();
  42. public void InitShuiZhai()
  43. {
  44. dicGos.Clear();
  45. var tfRoot = GameObject.Find("快速定位").transform;
  46. int count = tfRoot.childCount;
  47. for (int i = 0; i < count; i++)
  48. {
  49. var child = tfRoot.GetChild(i);
  50. dicGos[child.name] = child.GetChild(0).gameObject;
  51. }
  52. }
  53. GameObject goShuiZai;
  54. ShuiZaiCtrl shuizaiCtrl;
  55. public void OnToggleShuiZai(bool v)
  56. {
  57. CmdSetShuiZai(v, 0);
  58. }
  59. public void OnSliderShuiZai(float v)
  60. {
  61. CmdSetShuiZai(true, v);
  62. }
  63. public Dictionary<string, float> dicSW = new Dictionary<string, float>();
  64. [Command(requiresAuthority = false)]
  65. public void CmdOnLevel(string name, float level)
  66. {
  67. dicSW[name] = level;
  68. foreach (var kvp in dicSW)
  69. {
  70. Debug.Log($"{kvp.Key} 水位 {kvp.Value}");
  71. }
  72. var cmdObj = new ShuiWei_opt();
  73. cmdObj.kvp = new ShuiWei_opt.Info[dicSW.Count];
  74. int index = 0;
  75. foreach (var kvp in dicSW)
  76. {
  77. cmdObj.kvp[index++] = new ShuiWei_opt.Info() { postion = kvp.Key, value = kvp.Value };
  78. }
  79. NetworkAnimPlayer.instance.SendCmdToServer(20601, cmdObj);
  80. }
  81. int nTaskSetShuiWei = -1;
  82. [Command(requiresAuthority = false)]
  83. public void CmdSetShuiZai(bool isOpen, float value)
  84. {
  85. Debug.Log($"CmdSetShuiZai isOpen-{isOpen} value-{value}");
  86. RpcSetShuiZai(isOpen, value);
  87. if (isOpen)
  88. {
  89. nTaskSetShuiWei = Utility.Update(5, () =>
  90. {
  91. foreach (var kvp in dicSW)
  92. {
  93. RpcSetShuiWei(kvp.Key, kvp.Value);
  94. }
  95. });
  96. }
  97. else
  98. {
  99. Utility.StopCoroutineExe(nTaskSetShuiWei);
  100. }
  101. }
  102. [ClientRpc]
  103. public void RpcSetShuiWei(string shuiweiname, float shuiwei)
  104. {
  105. dicGos[shuiweiname].GetComponentInChildren<TextMesh>().text = $"{shuiweiname}水位:{shuiwei.ToString("#0.000")} 米";
  106. }
  107. private void ShowLevelTag(bool show)
  108. {
  109. foreach (var kvp in dicGos)
  110. {
  111. kvp.Value.SetActive(show);
  112. }
  113. }
  114. [ClientRpc]
  115. public void RpcSetShuiZai(bool isOpen, float value)
  116. {
  117. ShowLevelTag(isOpen);
  118. if (isOpen)
  119. {
  120. if (goShuiZai == null)
  121. {
  122. MainEntry.Resource.LoadAsset("Assets/_DTYJ/Prefabs/水灾/水灾Flow.prefab", new GameFramework.Resource.LoadAssetCallbacks((assetName, asset, duration, userData) =>
  123. {
  124. goShuiZai = Instantiate((GameObject)asset);
  125. shuizaiCtrl = goShuiZai.GetComponent<ShuiZaiCtrl>();
  126. shuizaiCtrl?.SetFlowValue_up(value);
  127. }));
  128. }
  129. else
  130. {
  131. shuizaiCtrl?.SetFlowValue_up(value);
  132. }
  133. }
  134. else
  135. {
  136. if (goShuiZai != null)
  137. {
  138. DestroyImmediate(goShuiZai);
  139. goShuiZai = null;
  140. shuizaiCtrl = null;
  141. }
  142. }
  143. }
  144. [Command(requiresAuthority = false)]
  145. public void CmdSetShuiZai_down(float value)
  146. {
  147. var conn = OnlineCtrl.Instance.GetConnByRoleName(RoleName.站务员_站台);
  148. if (conn != null)
  149. RpcSetShuiZai_down(conn, value);
  150. }
  151. [TargetRpc]
  152. public void RpcSetShuiZai_down(NetworkConnectionToClient target, float value)
  153. {
  154. shuizaiCtrl.SetFlowValue_down(Mathf.Abs(value));
  155. }
  156. private GameObject goShaDai;
  157. [Command(requiresAuthority = false)]
  158. public void CmdPlaceShaDai()
  159. {
  160. if (goShaDai == null)
  161. goShaDai = OnlineCtrl.Instance.GenerateNetworkObj(13);
  162. }
  163. [Command(requiresAuthority = false)]
  164. public void CmdRemoveShaDai()
  165. {
  166. if (goShaDai != null)
  167. NetworkServer.Destroy(goShaDai);
  168. }
  169. private GameObject goTishiPai;
  170. [Command(requiresAuthority = false)]
  171. public void CmdPlaceTishiPai()
  172. {
  173. if (goTishiPai == null)
  174. goTishiPai = OnlineCtrl.Instance.GenerateNetworkObj(17);
  175. }
  176. [Command(requiresAuthority = false)]
  177. public void CmdRemoveTishiPai()
  178. {
  179. if (goTishiPai != null)
  180. NetworkServer.Destroy(goTishiPai);
  181. }
  182. private GameObject goGaoShi;
  183. [Command(requiresAuthority = false)]
  184. public void CmdPlaceGaoShi()
  185. {
  186. if (goGaoShi == null)
  187. goGaoShi = OnlineCtrl.Instance.GenerateNetworkObj(18);
  188. }
  189. [Command(requiresAuthority = false)]
  190. public void CmdRemoveGaoShi()
  191. {
  192. if (goGaoShi != null)
  193. NetworkServer.Destroy(goGaoShi);
  194. }
  195. private GameObject goZhiHuiBu;
  196. [Command(requiresAuthority = false)]
  197. public void CmdPlaceZhiHuiBu()
  198. {
  199. if (goZhiHuiBu == null)
  200. goZhiHuiBu = OnlineCtrl.Instance.GenerateNetworkObj(19);
  201. }
  202. [Command(requiresAuthority = false)]
  203. public void CmdRemoveZhiHuiBu()
  204. {
  205. if (goZhiHuiBu != null)
  206. NetworkServer.Destroy(goZhiHuiBu);
  207. }
  208. private GameObject goTZFW_Pai;
  209. [Command(requiresAuthority = false)]
  210. public void CmdPlaceTZFW()
  211. {
  212. if (goTZFW_Pai == null)
  213. goTZFW_Pai = OnlineCtrl.Instance.GenerateNetworkObj(20);
  214. }
  215. [Command(requiresAuthority = false)]
  216. public void CmdRemoveTZFW()
  217. {
  218. if (goTZFW_Pai != null)
  219. NetworkServer.Destroy(goTZFW_Pai);
  220. }
  221. private GameObject goShuiBeng_1, goShuiBeng_2;
  222. [Command(requiresAuthority = false)]
  223. public void CmdPlaceShuiBeng(string id)
  224. {
  225. if (id == "1")
  226. {
  227. if (goShuiBeng_1 == null)
  228. goShuiBeng_1 = OnlineCtrl.Instance.GenerateNetworkObj(14);
  229. }
  230. else if (id == "2")
  231. {
  232. if (goShuiBeng_2 == null)
  233. goShuiBeng_2 = OnlineCtrl.Instance.GenerateNetworkObj(16);
  234. }
  235. }
  236. [Command(requiresAuthority = false)]
  237. public void CmdRemoveShuiBeng(string id)
  238. {
  239. if (id == "1")
  240. {
  241. if (goShuiBeng_1 != null)
  242. NetworkServer.Destroy(goShuiBeng_1);
  243. }
  244. else if (id == "2")
  245. {
  246. if (goShuiBeng_2 != null)
  247. NetworkServer.Destroy(goShuiBeng_2);
  248. }
  249. }
  250. #endregion
  251. #region Train
  252. public InputField input_trainNo, input_blockNo;
  253. private float trainSpeed = 0;
  254. public void OnSlider_TrainSpeed(float value)
  255. {
  256. trainSpeed = value;
  257. }
  258. public void OnClickSetTrain()
  259. {
  260. RunTrain(input_trainNo.text, input_blockNo.text);
  261. }
  262. Dictionary<string, TrainInfo> dicTrain = new Dictionary<string, TrainInfo>();
  263. public void RunTrain(string trainNo, string block)
  264. {
  265. TrainInfo trainInfo;
  266. if (dicTrain.ContainsKey(trainNo))
  267. {
  268. trainInfo = dicTrain[trainNo];
  269. }
  270. else
  271. {
  272. trainInfo = new TrainInfo();
  273. dicTrain.Add(trainNo, trainInfo);
  274. }
  275. trainInfo.trainNo = trainNo;
  276. if (trainInfo.blockNo != block)
  277. {
  278. trainInfo.blockNo = block;
  279. trainInfo.offset = 0;
  280. }
  281. trainInfo.speed = trainSpeed;
  282. }
  283. CMDData cmdData10101_train = new CMDData() { cmd = 10101 };
  284. readonly List<string> listTrainStop = new List<string>();
  285. private void UpdateTrain()
  286. {
  287. foreach (var kvp in dicTrain)
  288. {
  289. kvp.Value.offset += kvp.Value.speed * Time.deltaTime;
  290. var trainInfo = kvp.Value;
  291. if (kvp.Value.blockNo == "T0202")
  292. {
  293. if (kvp.Value.offset > 140.6f && kvp.Value.offset < 140.73f)
  294. {
  295. if (!listTrainStop.Contains(kvp.Key))
  296. {
  297. listTrainStop.Add(kvp.Key);
  298. trainInfo.speed = 0;
  299. }
  300. }
  301. else
  302. {
  303. if (listTrainStop.Contains(kvp.Key))
  304. {
  305. listTrainStop.Remove(kvp.Key);
  306. }
  307. }
  308. }
  309. else
  310. {
  311. if (listTrainStop.Contains(kvp.Key))
  312. listTrainStop.Remove(kvp.Key);
  313. }
  314. cmdData10101_train.data = trainInfo;
  315. ControllerPanel.instance.CmdSendMessageMoNI(Newtonsoft.Json.JsonConvert.SerializeObject(cmdData10101_train));
  316. }
  317. }
  318. public void SetTrainDoor(string trainNo, int state)
  319. {
  320. TrainDoorState doorState = new TrainDoorState();
  321. doorState.trainNo = trainNo;
  322. doorState.state = state;
  323. var dataCmd = new { cmd = 10105, data = doorState };
  324. ControllerPanel.instance.CmdSendMessageMoNI(Newtonsoft.Json.JsonConvert.SerializeObject(dataCmd));
  325. }
  326. public InputField input_trainNo_door;
  327. int trainDoorState = 0;
  328. public void OnDropdownTrainDoor(int v)
  329. {
  330. trainDoorState = v;
  331. }
  332. public void OnClickSetTrainDoor()
  333. {
  334. SetTrainDoor(input_trainNo_door.text, trainDoorState);
  335. }
  336. #endregion
  337. #region CK
  338. public void OnSliderJinZhanSpeed(float value)
  339. {
  340. CmdSetJinZhanSpeed((int)value);
  341. }
  342. int countGenerate = 50;
  343. public Text addCKCount;
  344. public void OnSliderCKCount(float value)
  345. {
  346. countGenerate = (int)value;
  347. addCKCount.text = $"进站速度({countGenerate}人/分钟)";
  348. }
  349. [Command(requiresAuthority = false)]
  350. public void CmdSetJinZhanSpeed(int speed)
  351. {
  352. OnlineCtrl.Instance.chengKeCtrl.SetInSpeed(speed);
  353. }
  354. public void OnClickClearAll()
  355. {
  356. CmdClearAllCK();
  357. }
  358. [Command(requiresAuthority = false)]
  359. public void CmdClearAllCK()
  360. {
  361. OnlineCtrl.Instance.chengKeCtrl.RemoveAllChengKe();
  362. }
  363. public void OnClickGenerateCK_Immidiately()
  364. {
  365. CmdGenerateCK_Immidiately(countGenerate);
  366. }
  367. [Command(requiresAuthority = false)]
  368. public void CmdGenerateCK_Immidiately(int count)
  369. {
  370. OnlineCtrl.Instance.chengKeCtrl.GenerateImmidiately(count);
  371. }
  372. public void OntoggleChuZhan(bool v)
  373. {
  374. CmdChuZhanIsOn(v);
  375. }
  376. [Command(requiresAuthority = false)]
  377. public void CmdChuZhanIsOn(bool value)
  378. {
  379. OnlineCtrl.Instance.chengKeCtrl.CountPerMin_ChuZhan = value ? 10 : 0;
  380. }
  381. private GameObject goWeilan;
  382. [Command(requiresAuthority = false)]
  383. public void CmdPlaceWeilan()
  384. {
  385. if (goWeilan == null)
  386. goWeilan = OnlineCtrl.Instance.GenerateNetworkObj(15);
  387. }
  388. [Command(requiresAuthority = false)]
  389. public void CmdRemoveWeilan()
  390. {
  391. if (goWeilan != null)
  392. NetworkServer.Destroy(goWeilan);
  393. }
  394. #endregion
  395. #region 设备控制
  396. [Command(requiresAuthority = false)]
  397. public void CmdSendMessageMoNI(string cMDDataJson)
  398. {//模拟来协同服务器的消息
  399. OnlineCtrl.Instance.OnWebSocketMsgReceiveMoNi(cMDDataJson);
  400. }
  401. #endregion
  402. #region 灯
  403. List<MeshRenderer> meshRenderers;
  404. Color oldEmissionColor;
  405. Dictionary<string, GameObject> dicDeng;
  406. private void InitDeng()
  407. {
  408. meshRenderers = new List<MeshRenderer>();
  409. meshRenderers.Add(GameObject.Find("SceneStatic/Chezhan_fangti_static/Chezhan_fangti_static/polySurface4884").GetComponent<MeshRenderer>());
  410. var root = GameObject.Find("SceneStatic/Chezhan_light_static/Chezhan_light_static/zhantai");
  411. int count = root.transform.childCount;
  412. for (int i = 0; i < count; i++)
  413. {
  414. MeshRenderer renderer = root.transform.GetChild(i).GetComponent<MeshRenderer>();
  415. meshRenderers.Add(renderer);
  416. }
  417. oldEmissionColor = meshRenderers[0].material.GetColor("_EmissionColor");
  418. dicDeng = new Dictionary<string, GameObject>();
  419. var rootDeng = GameObject.Find($"灯控制");
  420. for (int i = 0; i < rootDeng.transform.childCount; i++)
  421. {
  422. var child = rootDeng.transform.GetChild(i);
  423. dicDeng[child.name] = child.gameObject;
  424. child.gameObject.SetActive(false);
  425. }
  426. }
  427. [Command(requiresAuthority = false)]
  428. public void CmdDeng(string position, bool v)
  429. {
  430. RpcDeng(position, v);
  431. }
  432. [ClientRpc]
  433. public void RpcDeng(string position, bool v)
  434. {
  435. dicDeng[position].SetActive(!v);
  436. for (int i = 0; i < meshRenderers.Count; i++)
  437. {
  438. meshRenderers[0].material.SetColor("_EmissionColor", v ? oldEmissionColor : Color.black);
  439. }
  440. }
  441. #endregion
  442. }
  443. }