| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityCommon;
- using UnityEngine;
- using Mirror;
- namespace DTYJ
- {
- public class TrainCtrl : NetworkBehaviour
- {
- public static Dictionary<string, TrainCtrl> TrainCtrlDic = new Dictionary<string, TrainCtrl>();
- public static string trainStop_xiaxing, trainStop_shangxing;
- private Dictionary<string, Transform> dicBlock;
- private readonly List<string> blockReverseNames = new List<string>() { "T0105", "T0107", "T0203", "T0205", "T0301" };
- private Animation animDoor;
- private void Awake()
- {
- InitBlocks();
- animDoor = GetComponentInChildren<Animation>();
- }
- public override void OnStartClient()
- {
- base.OnStartClient();
- }
- //是否停在站台上
- public static bool IsStopOnPlantform(string trainNo)
- {
- return trainNo == trainStop_xiaxing || trainNo == trainStop_shangxing;
- }
- [ClientRpc]
- public void RpcSetName(string name)
- {
- gameObject.name = name;
- TrainCtrlDic[name] = this;
- }
- public override void OnStartServer()
- {
- base.OnStartServer();
- TrainCtrlDic[name] = this;
- //生成固定在车上的乘客
- Utility.DelayExe(1, () =>
- {
- OnlineCtrl.Instance.chengKeCtrl.GenerateInTrain(name);
- });
- }
- public List<Transform> GetPoints()
- {
- List<Transform> points = new List<Transform>();
- var root = transform.Find("ChengKePoints");
- int count = root.childCount;
- for (int i = 0; i < count; i++)
- {
- Transform t = root.GetChild(i);
- points.Add(t);
- }
- return points;
- }
- private void InitBlocks()
- {
- dicBlock = new Dictionary<string, Transform>();
- var goBlockRoot = GameObject.Find("Blocks");
- if (goBlockRoot != null)
- {
- int count = goBlockRoot.transform.childCount;
- for (int i = 0; i < count; i++)
- {
- Transform t = goBlockRoot.transform.GetChild(i);
- dicBlock[t.name] = t;
- }
- }
- }
- Quaternion q_forward = Quaternion.identity;
- Quaternion q_back = Quaternion.Euler(new Vector3(0, 180, 0));
- public string myTrainNo
- {
- get
- {
- if (trainInfo != null)
- return trainInfo.trainNo;
- else
- return "";
- }
- }
- TrainInfo trainInfo;
- public void MsgHandle(TrainInfo trainInfo)
- {
- if (this.trainInfo == null)
- {
- Main.Instance?.uiMainMenu?.AddTrainJumpPosition(trainInfo.trainNo);
- }
- this.trainInfo = trainInfo;
- var p = GetPosition(trainInfo);
- if (p == null) return;
- transform.position = (Vector3)p;
- if (blockReverseNames.Contains(trainInfo.blockNo))
- {
- transform.localRotation = q_back;
- }
- else
- {
- transform.localRotation = q_forward;
- }
- if (!string.IsNullOrEmpty(trainStop_xiaxing))
- {
- if (trainStop_xiaxing == trainInfo.trainNo)
- {
- if (trainInfo.speed != 0)
- trainStop_xiaxing = null;
- }
- }
- if (!string.IsNullOrEmpty(trainStop_shangxing))
- {
- if (trainStop_shangxing == trainInfo.trainNo)
- {
- if (trainInfo.speed != 0)
- trainStop_shangxing = null;
- }
- }
- if (trainInfo.speed == 0)
- {
- if (trainInfo.blockNo == "T0202" && Mathf.Abs((float)trainInfo.offset - 140.63f) < 0.3f)
- {
- trainStop_xiaxing = trainInfo.trainNo;
- }
- if (trainInfo.blockNo == "T0201" && Mathf.Abs((float)trainInfo.offset - 5) < 0.3f)
- {
- trainStop_shangxing = trainInfo.trainNo;
- }
- OnlineCtrl.Instance.chengKeCtrl.TrainRunning(trainInfo.trainNo, false);
- }
- else
- OnlineCtrl.Instance.chengKeCtrl.TrainRunning(trainInfo.trainNo, true);
- if (string.IsNullOrEmpty(trainStop_xiaxing) && string.IsNullOrEmpty(trainStop_shangxing))
- {//无车停站,更新位置
- OnlineCtrl.Instance.chengKeCtrl.UpdateCKPosition();
- }
- }
- public void TrainDoor(int doorState)
- {
- animDoor[animDoor.clip.name].speed = doorState == 1 ? 1 : -1;
- animDoor[animDoor.clip.name].time = doorState == 1 ? 0 : animDoor[animDoor.clip.name].length;
- animDoor.CrossFade(animDoor.clip.name);
- }
- private Transform GetBlock(string blockName)
- {
- if (dicBlock.ContainsKey(blockName))
- return dicBlock[blockName];
- return null;
- }
- private readonly Vector3 pFaraway = Vector3.down * 10000;
- private Vector3? GetPosition(TrainInfo trainInfo)
- {//计算位置
- var tfBlock = GetBlock(trainInfo.blockNo);
- if (tfBlock != null)
- return tfBlock.position + (tfBlock.forward * (float)trainInfo.offset);
- else
- {
- Debug.LogWarning("无该区段定点:" + trainInfo.blockNo);
- //return null;
- int nIndex;
- if (int.TryParse(trainInfo.trainNo, out nIndex))
- return Vector3.down * 1000 * nIndex;
- else
- return pFaraway;
- }
- }
- private void OnEnable()
- {
- }
- private void OnDestroy()
- {
- Main.Instance?.uiMainMenu?.DeleteTrainJumpBnt(trainInfo.trainNo);
- }
- }
- }
|