ChengKeCtrl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using GFMain;
  2. using Mirror;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityCommon;
  8. using UnityEngine;
  9. using UnityEngine.AI;
  10. namespace DTYJ
  11. {
  12. public class ChengKeCtrl
  13. {
  14. [Server]
  15. public void RemoveAllChengKe()
  16. {
  17. listChengKeJinZhan.ForEach((ck) =>
  18. {
  19. NetworkServer.Destroy(ck);
  20. });
  21. listChengKeJinZhan.Clear();
  22. for (int i = 0; i < listQueue.Count; i++)
  23. {
  24. listQueue[i].Clear();
  25. }
  26. listChengKeChuZhan.ForEach((ck) =>
  27. {
  28. NetworkServer.Destroy(ck);
  29. });
  30. listChengKeChuZhan.Clear();
  31. maxCount_ChuZhan = 0;
  32. RenShuTrigger.dicCount["站台"] = 0;
  33. RenShuTrigger.dicCount["站厅"] = 0;
  34. personTotalCount = 0;
  35. }
  36. [Server]
  37. public void GenerateImmidiately(int count)
  38. {
  39. GenereatePerson_immidiately(count);
  40. }
  41. public void RemoveCK(GameObject go)
  42. {
  43. if (listChengKeChuZhan.Contains(go))
  44. {
  45. listChengKeChuZhan.Remove(go);
  46. }
  47. else if (listChengKeJinZhan.Contains(go))
  48. {
  49. listChengKeJinZhan.Remove(go);
  50. for (int i = 0; i < listQueue.Count; ++i)
  51. {
  52. if (listQueue[i].Contains(go))
  53. {
  54. listQueue[i].Remove(go);
  55. }
  56. }
  57. }
  58. NetworkServer.Destroy(go);
  59. personTotalCount--;
  60. if (personTotalCount < 0)
  61. {
  62. personTotalCount = 0;
  63. }
  64. }
  65. Dictionary<string, List<GameObject>> dicCkInTrain = new Dictionary<string, List<GameObject>>();
  66. public void SetCKInTrain(string trainNo, GameObject goCk)
  67. {
  68. if (!dicCkInTrain.ContainsKey(trainNo))
  69. {
  70. dicCkInTrain.Add(trainNo, new List<GameObject>());
  71. }
  72. dicCkInTrain[trainNo].Add(goCk);
  73. if (listChengKeJinZhan.Contains(goCk))
  74. {
  75. listChengKeJinZhan.Remove(goCk);
  76. }
  77. for (int i = 0; i < listQueue.Count; ++i)
  78. {
  79. if (listQueue[i].Contains(goCk))
  80. {
  81. listQueue[i].Remove(goCk);
  82. }
  83. }
  84. //UpdateCKPosition();
  85. }
  86. /// <summary>
  87. /// 设置进站未上车的乘客出站
  88. /// </summary>
  89. public void SetCKChuZhan(int exitPort)
  90. {
  91. foreach (var item in listChengKeJinZhan)
  92. {
  93. var targetP = exitPort == 1 ? GetSpawnPoint_ChuZhan_target_1() : GetSpawnPoint_ChuZhan_target_2();
  94. item.GetComponent<NavMeshAgent>().SetDestination(targetP.position);
  95. }
  96. for (int i = 0; i < listQueue.Count; ++i)
  97. {
  98. listQueue[i].Clear();
  99. }
  100. cursor = -1;
  101. }
  102. public void GenerateInTrain(string trainNo)
  103. {
  104. if (TrainCtrl.TrainCtrlDic.ContainsKey(trainNo))
  105. {
  106. var trainctrl = TrainCtrl.TrainCtrlDic[trainNo];
  107. var points = trainctrl.GetPoints();
  108. for (int i = 0; i < points.Count; ++i)
  109. {
  110. var tfOrg = points[i];
  111. var goNew = OnlineCtrl.Instance.GenerateChengKe(GetRolePrefab(), tfOrg, $"列车角色{i}");
  112. goNew.name = "Train" + trainNo;
  113. var agent = goNew.GetComponent<NavMeshAgent>();
  114. agent.enabled = false;
  115. ++personTotalCount;
  116. }
  117. }
  118. }
  119. public void SetCkOutTrain(string trainNo, GameObject goCk)
  120. {
  121. if (dicCkInTrain.ContainsKey(trainNo))
  122. {
  123. dicCkInTrain[trainNo].Remove(goCk);
  124. }
  125. }
  126. public void ClearCkInTrain()
  127. {
  128. foreach (var k in dicCkInTrain.Values)
  129. {
  130. foreach (var v in k)
  131. {
  132. RemoveCK(v);
  133. }
  134. }
  135. dicCkInTrain.Clear();
  136. }
  137. /// <summary>
  138. /// 上车
  139. /// </summary>
  140. /// <param name="xingbie">1上行 2下行</param>
  141. [Server]
  142. public void ShangChe(int xingbie)
  143. {
  144. Utility.DelayExe(2, () =>
  145. {//延时两秒 等车门打开
  146. if (xingbie == 2)
  147. {
  148. for (int i = 0; i < 57; ++i)
  149. {
  150. if (listQueue[i].Count > 0)
  151. {
  152. var goCkFirst = listQueue[i][0];
  153. var tfFirst = listPoints_zhantai[i].transform;
  154. var targetP = tfFirst.transform.position + (tfFirst.transform.forward * 2.3f);
  155. goCkFirst.GetComponent<NavMeshAgent>().SetDestination(targetP);
  156. }
  157. }
  158. }
  159. else if (xingbie == 1)
  160. {
  161. for (int i = 57; i < listQueue.Count; ++i)
  162. {
  163. if (listQueue[i].Count > 0)
  164. {
  165. var goCkFirst = listQueue[i][0];
  166. var tfFirst = listPoints_zhantai[i].transform;
  167. var targetP = tfFirst.transform.position + (tfFirst.transform.forward * 2.3f);
  168. goCkFirst.GetComponent<NavMeshAgent>().SetDestination(targetP);
  169. }
  170. }
  171. }
  172. });
  173. }
  174. public void XiaChe(string trainNo)
  175. {
  176. if (dicCkInTrain.ContainsKey(trainNo))
  177. {
  178. Utility.DelayExe(2, () =>
  179. {//延时两秒 等车门打开
  180. var xiacheCount = dicCkInTrain[trainNo].Count * 0.6f;
  181. var targetP = GetSpawnPoint_ChuZhan_target_1();
  182. for (int i = 0; i < xiacheCount; ++i)
  183. {
  184. dicCkInTrain[trainNo][i].GetComponent<NavMeshAgent>().SetDestination(targetP.position);
  185. }
  186. });
  187. }
  188. }
  189. private const float interval = 0.5f;//队列间距
  190. private Transform tfSpawnPointRoot;
  191. private Transform tfSpawnPointRoot_chuzhan;//出站出生点
  192. private Transform tfSpawnPointRoot_chuzhan_target;//出站目标点
  193. private Transform tfSpawnPointRoot_chuzhan_target_2;//出站目标点
  194. private Transform tfSpawnPointRoot_zhanting;//站厅
  195. public int maxCount = 50000;
  196. public int maxCount_ChuZhan = 50;
  197. public ChengKeCtrl()
  198. {
  199. var tfRoot = GameObject.Find("角色默认位置/乘客位置/站台");
  200. tfSpawnPointRoot = GameObject.Find("角色默认位置/乘客位置/进站出生点/2号口").transform;
  201. tfSpawnPointRoot_chuzhan = GameObject.Find("角色默认位置/乘客位置/出站出生点").transform;
  202. tfSpawnPointRoot_chuzhan_target = GameObject.Find("角色默认位置/乘客位置/出站目标点").transform;
  203. tfSpawnPointRoot_chuzhan_target_2 = GameObject.Find("角色默认位置/乘客位置/出站目标点_2").transform;
  204. tfSpawnPointRoot_zhanting = GameObject.Find("角色默认位置/乘客位置/站厅").transform;
  205. var points = tfRoot.GetComponentsInChildren<Transform>();
  206. listPoints_zhantai.AddRange(points.Skip(1).ToArray());
  207. for (int i = 0; i < listPoints_zhantai.Count; i++)
  208. {
  209. listQueue.Add(new List<GameObject>());
  210. }
  211. }
  212. List<List<GameObject>> listQueue = new List<List<GameObject>>();
  213. public void UpdateCKPosition()
  214. {
  215. for (int i = 0; i < listQueue.Count; i++)
  216. {
  217. for (int j = 0; j < listQueue[i].Count; j++)
  218. {
  219. var ck = listQueue[i][j];
  220. var tfFirst = listPoints_zhantai[i].transform;
  221. var targetP = tfFirst.transform.position + (-tfFirst.transform.forward * interval * j);
  222. ck.GetComponent<NavMeshAgent>().SetDestination(targetP);
  223. }
  224. }
  225. }
  226. public void TrainRunning(string trainNo, bool v)
  227. {
  228. if (dicCkInTrain.ContainsKey(trainNo))
  229. {
  230. for (int i = 0; i < dicCkInTrain[trainNo].Count; i++)
  231. {
  232. dicCkInTrain[trainNo][i].GetComponent<NavMeshAgent>().enabled = !v && TrainCtrl.IsStopOnPlantform(trainNo);
  233. }
  234. }
  235. }
  236. private GameObject GetRolePrefab()
  237. {
  238. return OnlineNetworkManager.singleton.spawnPrefabs[UnityEngine.Random.Range(5, 12)];
  239. }
  240. private Transform GetSpawnPoint()
  241. {
  242. return tfSpawnPointRoot.GetChild(UnityEngine.Random.Range(0, tfSpawnPointRoot.childCount));
  243. }
  244. private Transform GetSpawnPoint_ChuZhan()
  245. {
  246. return tfSpawnPointRoot_chuzhan.GetChild(UnityEngine.Random.Range(0, tfSpawnPointRoot_chuzhan.childCount));
  247. }
  248. private Transform GetSpawnPoint_ChuZhan_target_1()
  249. {
  250. return tfSpawnPointRoot_chuzhan_target.GetChild(UnityEngine.Random.Range(0, tfSpawnPointRoot_chuzhan_target.childCount));
  251. }
  252. private Transform GetSpawnPoint_ChuZhan_target_2()
  253. {
  254. return tfSpawnPointRoot_chuzhan_target_2.GetChild(UnityEngine.Random.Range(0, tfSpawnPointRoot_chuzhan_target_2.childCount));
  255. }
  256. public void StartProgress()
  257. {
  258. Utility.Update(1, Update);
  259. }
  260. private readonly List<Transform> listPoints_zhantai = new List<Transform>();
  261. private void Update()
  262. {
  263. //进站
  264. var countPerSec = CountPerMin / 60f;//每秒生成人数
  265. int count = (int)countPerSec;
  266. GenereatePerson(count);
  267. countPerSec -= count;//小数部分
  268. if (UnityEngine.Random.Range(0, 1f) < countPerSec)
  269. GenereatePerson(1);
  270. //出站
  271. var countPerSec_chuzhan = CountPerMin_ChuZhan / 60f;//每秒生成人数
  272. int count_chuzhan = (int)countPerSec_chuzhan;
  273. GenereatePerson_ChuZhan(count_chuzhan);
  274. countPerSec_chuzhan -= count_chuzhan;//小数部分
  275. if (UnityEngine.Random.Range(0, 1f) < countPerSec_chuzhan)
  276. GenereatePerson_ChuZhan(1);
  277. float soundValue = personTotalCount / 300f;
  278. soundValue = Mathf.Clamp(soundValue, 0f, 1f);
  279. ControllerPanel.instance.RpcSetSoundValue(Mathf.Clamp(soundValue, 0f, 1f));
  280. }
  281. private int personTotalCount = 0;
  282. private int personTotalCount_chuzhan = 0;
  283. int cursor = -1;
  284. int cursorMoveNext
  285. {
  286. get
  287. {
  288. cursor++;
  289. if (cursor >= listPoints_zhantai.Count)
  290. cursor = 0;
  291. return cursor;
  292. }
  293. }
  294. private readonly List<GameObject> listChengKeChuZhan = new List<GameObject>();
  295. private readonly List<GameObject> listChengKeJinZhan = new List<GameObject>();
  296. private void GenereatePerson(int count)
  297. {
  298. if (count <= 0 || personTotalCount >= maxCount) return;
  299. for (int i = 0; i < count; i++)
  300. {
  301. var tfOrg = GetSpawnPoint();
  302. var goNew = OnlineCtrl.Instance.GenerateChengKe(GetRolePrefab(), tfOrg, $"进站角色{i}");
  303. //goNew.name = "GenereatePerson";
  304. var nowCursor = cursorMoveNext;
  305. listQueue[nowCursor].Add(goNew);
  306. var agent = goNew.GetComponent<NavMeshAgent>();
  307. agent.Warp(tfOrg.position);
  308. var tfFirst = listPoints_zhantai[nowCursor].transform;
  309. var targetP = tfFirst.transform.position + (-tfFirst.transform.forward * interval * listQueue[nowCursor].Count);
  310. agent.SetDestination(targetP);
  311. ++personTotalCount;
  312. listChengKeJinZhan.Add(goNew);
  313. }
  314. Debug.Log($"总进站人数:{personTotalCount} 新增:{count}");
  315. }
  316. private void GenereatePerson_immidiately(int count)
  317. {
  318. if (count <= 0 || personTotalCount >= maxCount) return;
  319. count = tfSpawnPointRoot_zhanting.childCount;
  320. for (int i = 0; i < count; i++)
  321. {
  322. var tfOrg = tfSpawnPointRoot_zhanting.GetChild(i);// GetSpawnPoint();
  323. var goNew = OnlineCtrl.Instance.GenerateChengKe(GetRolePrefab(), tfOrg, $"立即生成角色{i}");
  324. goNew.name = "GenereatePerson_immidiately";
  325. var nowCursor = cursorMoveNext;
  326. listQueue[nowCursor].Add(goNew);
  327. var agent = goNew.GetComponent<NavMeshAgent>();
  328. agent.Warp(tfOrg.position);
  329. var tfFirst = listPoints_zhantai[nowCursor].transform;
  330. var targetP = tfFirst.transform.position + (-tfFirst.transform.forward * interval * listQueue[nowCursor].Count);
  331. agent.SetDestination(targetP);
  332. ++personTotalCount;
  333. listChengKeJinZhan.Add(goNew);
  334. }
  335. Debug.Log($"总进站人数:{personTotalCount} 新增:{count}");
  336. }
  337. private void GenereatePerson_ChuZhan(int count)
  338. {
  339. if (count <= 0 || personTotalCount_chuzhan >= maxCount_ChuZhan) return;
  340. for (int i = 0; i < count; i++)
  341. {
  342. var tfOrg = GetSpawnPoint_ChuZhan();
  343. var goNew = OnlineCtrl.Instance.GenerateChengKe(GetRolePrefab(), tfOrg, $"出站角色{i}");
  344. goNew.name = "GenereatePerson_ChuZhan";
  345. var agent = goNew.GetComponent<NavMeshAgent>();
  346. agent.Warp(tfOrg.position);
  347. var trigger = goNew.AddComponentOnlyOne<ChuZhanTrigger>();
  348. trigger.onTriggerEnter = (go) =>
  349. {
  350. RemoveCK_chuzhan(go);
  351. };
  352. listChengKeChuZhan.Add(goNew);
  353. var targetP = GetSpawnPoint_ChuZhan_target_1();
  354. agent.SetDestination(targetP.position);
  355. ++personTotalCount_chuzhan;
  356. }
  357. Debug.Log($"总出站人数:{personTotalCount_chuzhan} 新增:{count}");
  358. }
  359. private void RemoveCK_chuzhan(GameObject go)
  360. {
  361. if (listChengKeChuZhan.Contains(go))
  362. {
  363. listChengKeChuZhan.Remove(go);
  364. NetworkServer.Destroy(go);
  365. --personTotalCount_chuzhan;
  366. }
  367. }
  368. private float countPerMin = 0;
  369. /// <summary>
  370. /// 每分钟进站人数
  371. /// </summary>
  372. public float CountPerMin
  373. {
  374. set
  375. {
  376. countPerMin = value;
  377. }
  378. get
  379. {
  380. return countPerMin;
  381. }
  382. }
  383. /// <summary>
  384. /// 每分钟出站人数
  385. /// </summary>
  386. public float CountPerMin_ChuZhan = 0;
  387. /// <summary>
  388. /// 进站人数速率
  389. /// </summary>
  390. /// <param name="countPerMin">每分钟多少人</param>
  391. public void SetInSpeed(int countPerMin)
  392. {
  393. CountPerMin = countPerMin;
  394. }
  395. }
  396. }