TrainCtrl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityCommon;
  4. using UnityEngine;
  5. using Mirror;
  6. namespace DTYJ
  7. {
  8. public class TrainCtrl : NetworkBehaviour
  9. {
  10. public static Dictionary<string, TrainCtrl> TrainCtrlDic = new Dictionary<string, TrainCtrl>();
  11. public static string trainStop_xiaxing, trainStop_shangxing;
  12. private Dictionary<string, Transform> dicBlock;
  13. private readonly List<string> blockReverseNames = new List<string>() { "T0105", "T0107", "T0203", "T0205", "T0301" };
  14. private Animation animDoor;
  15. private void Awake()
  16. {
  17. InitBlocks();
  18. animDoor = GetComponentInChildren<Animation>();
  19. }
  20. public override void OnStartClient()
  21. {
  22. base.OnStartClient();
  23. }
  24. //是否停在站台上
  25. public static bool IsStopOnPlantform(string trainNo)
  26. {
  27. return trainNo == trainStop_xiaxing || trainNo == trainStop_shangxing;
  28. }
  29. [ClientRpc]
  30. public void RpcSetName(string name)
  31. {
  32. gameObject.name = name;
  33. TrainCtrlDic[name] = this;
  34. }
  35. public override void OnStartServer()
  36. {
  37. base.OnStartServer();
  38. TrainCtrlDic[name] = this;
  39. //生成固定在车上的乘客
  40. Utility.DelayExe(1, () =>
  41. {
  42. OnlineCtrl.Instance.chengKeCtrl.GenerateInTrain(name);
  43. });
  44. }
  45. public List<Transform> GetPoints()
  46. {
  47. List<Transform> points = new List<Transform>();
  48. var root = transform.Find("ChengKePoints");
  49. int count = root.childCount;
  50. for (int i = 0; i < count; i++)
  51. {
  52. Transform t = root.GetChild(i);
  53. points.Add(t);
  54. }
  55. return points;
  56. }
  57. private void InitBlocks()
  58. {
  59. dicBlock = new Dictionary<string, Transform>();
  60. var goBlockRoot = GameObject.Find("Blocks");
  61. if (goBlockRoot != null)
  62. {
  63. int count = goBlockRoot.transform.childCount;
  64. for (int i = 0; i < count; i++)
  65. {
  66. Transform t = goBlockRoot.transform.GetChild(i);
  67. dicBlock[t.name] = t;
  68. }
  69. }
  70. }
  71. Quaternion q_forward = Quaternion.identity;
  72. Quaternion q_back = Quaternion.Euler(new Vector3(0, 180, 0));
  73. public string myTrainNo
  74. {
  75. get
  76. {
  77. if (trainInfo != null)
  78. return trainInfo.trainNo;
  79. else
  80. return "";
  81. }
  82. }
  83. TrainInfo trainInfo;
  84. public void MsgHandle(TrainInfo trainInfo)
  85. {
  86. if (this.trainInfo == null)
  87. {
  88. Main.Instance?.uiMainMenu?.AddTrainJumpPosition(trainInfo.trainNo);
  89. }
  90. this.trainInfo = trainInfo;
  91. var p = GetPosition(trainInfo);
  92. if (p == null) return;
  93. transform.position = (Vector3)p;
  94. if (blockReverseNames.Contains(trainInfo.blockNo))
  95. {
  96. transform.localRotation = q_back;
  97. }
  98. else
  99. {
  100. transform.localRotation = q_forward;
  101. }
  102. if (!string.IsNullOrEmpty(trainStop_xiaxing))
  103. {
  104. if (trainStop_xiaxing == trainInfo.trainNo)
  105. {
  106. if (trainInfo.speed != 0)
  107. trainStop_xiaxing = null;
  108. }
  109. }
  110. if (!string.IsNullOrEmpty(trainStop_shangxing))
  111. {
  112. if (trainStop_shangxing == trainInfo.trainNo)
  113. {
  114. if (trainInfo.speed != 0)
  115. trainStop_shangxing = null;
  116. }
  117. }
  118. if (trainInfo.speed == 0)
  119. {
  120. if (trainInfo.blockNo == "T0202" && Mathf.Abs((float)trainInfo.offset - 140.63f) < 0.3f)
  121. {
  122. trainStop_xiaxing = trainInfo.trainNo;
  123. }
  124. if (trainInfo.blockNo == "T0201" && Mathf.Abs((float)trainInfo.offset - 5) < 0.3f)
  125. {
  126. trainStop_shangxing = trainInfo.trainNo;
  127. }
  128. OnlineCtrl.Instance.chengKeCtrl.TrainRunning(trainInfo.trainNo, false);
  129. }
  130. else
  131. OnlineCtrl.Instance.chengKeCtrl.TrainRunning(trainInfo.trainNo, true);
  132. if (string.IsNullOrEmpty(trainStop_xiaxing) && string.IsNullOrEmpty(trainStop_shangxing))
  133. {//无车停站,更新位置
  134. OnlineCtrl.Instance.chengKeCtrl.UpdateCKPosition();
  135. }
  136. }
  137. public void TrainDoor(int doorState)
  138. {
  139. animDoor[animDoor.clip.name].speed = doorState == 1 ? 1 : -1;
  140. animDoor[animDoor.clip.name].time = doorState == 1 ? 0 : animDoor[animDoor.clip.name].length;
  141. animDoor.CrossFade(animDoor.clip.name);
  142. }
  143. private Transform GetBlock(string blockName)
  144. {
  145. if (dicBlock.ContainsKey(blockName))
  146. return dicBlock[blockName];
  147. return null;
  148. }
  149. private readonly Vector3 pFaraway = Vector3.down * 10000;
  150. private Vector3? GetPosition(TrainInfo trainInfo)
  151. {//计算位置
  152. var tfBlock = GetBlock(trainInfo.blockNo);
  153. if (tfBlock != null)
  154. return tfBlock.position + (tfBlock.forward * (float)trainInfo.offset);
  155. else
  156. {
  157. Debug.LogWarning("无该区段定点:" + trainInfo.blockNo);
  158. //return null;
  159. int nIndex;
  160. if (int.TryParse(trainInfo.trainNo, out nIndex))
  161. return Vector3.down * 1000 * nIndex;
  162. else
  163. return pFaraway;
  164. }
  165. }
  166. private void OnEnable()
  167. {
  168. }
  169. private void OnDestroy()
  170. {
  171. Main.Instance?.uiMainMenu?.DeleteTrainJumpBnt(trainInfo.trainNo);
  172. }
  173. }
  174. }