using AX.MessageSystem; using AX.NetworkSystem; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using AX.Network.Protocols; using System; using AX.Serialization; public enum FireRobotSkill { 待命, 出水, 出泡沫, 铺设水带, 收起水带, 装备选择, 背包 } public class FireRobotSkillData { public long SenderId; public long GameObjId; public FireRobotSkill WorkType; } public class FireRobotController : MonoBehaviour { //旋转 public Transform HRotate; public Transform VRotate; private float V; public float MaxVrotate = 39f; public float MinVrotate = -39f; //技能同步 public FireRobotSkill workType; public FireRobotSkill WorkType { get { return workType; } set { workType = value; //机器人技能同步 //FireRobotSkillData worksync = new FireRobotSkillData(); //worksync.SenderId = CurrentUserInfo.mySelf.Id; //worksync.WorkType = value; //worksync.GameObjId = basegameinfo.gameObjID; //NetworkManager.Default.SendAsync( "FIREROBOT_WORKTYPECHANGE_SYNC", worksync); } } //流量 public float flow; public float Flow { get { return flow; } set { flow = value; //if (GetComponent().WaterLineConnent) //{ // GetComponent().WaterLineConnent.GetComponent().HasWaterCross = value > 0 ? true : false; //} //流量修改时重新分流 if (GetComponent()) { if (GetComponent().ConnentSource) { if (GetComponent().ConnentSource.GetComponent()) {//直接水源是车辆 float remain = CheckWaterRemain(GetComponent().WaterSourceLine); if (remain > 0 || remain == -100) {//水源有水 if (GetComponent().ConnentSource.GetComponent()) {//直接水源为水源车辆 // GetComponent().ConnentSource.GetComponent().Flow += ; GetComponent().ConnentSource.GetComponent().TotalFlowChange(basegameinfo.gameObjID, flow); } } else { Debug.Log("水源没水了"); } } else {//直接连接的消防设施 // GetComponent().ConnentSource.GetComponent().TotalFlow += (value - prveflow); GetComponent().ConnentSource.GetComponent().TotalFlowChange(basegameinfo.gameObjID, flow); } } } //流量同步 FlowChangeData arg = new FlowChangeData { SenderId = CurrentUserInfo.mySelf.Id, GameObjId = basegameinfo.gameObjID, Flow = value, }; NetworkManager.Default.SendAsync("FLOW_CHANGE_SYNC", arg); } } /// /// 剩余时间 /// public int RemainTime; /// /// 剩余水量 /// public float RemainWater; public SprayMode spraymode = SprayMode.WaterGun19; /// /// 水量用完 /// public bool waterrunoutof = false; private BaseGameObjInfo basegameinfo; public SprayParticleType SelectParticleType; //粒子大小 public float ParticleSize = 0.2f; public float MaxSize = 0.4f; public float MinSize = 0.0f; //出水粒子 private Transform Hose; private Transform frothpart; private Transform straightpart; private FireRobotWaterHoseManage hosemanage; //计时器 private float timer; void Start() { basegameinfo = GetComponent(); hosemanage = GetComponent(); Hose = transform.Find("Pao1/Pao2/Hose"); frothpart = Hose.Find("Froth"); straightpart = Hose.Find("WaterStraight"); MessageDispatcher.AddListener("SPRAY_WATER", Changeflow); MessageDispatcher.AddListener("RUN_OUR_OF_WATER", CloseSpray); NetworkMessageDispatcher.AddListener("FIREROBOT_ROTATE_SYNC", RotationSync); GetRealtimeConsume.getAllRealtimeConsume += addMyRealtimeConsume; //统计实时流量 } void OnDisable() { MessageDispatcher.RemoveListener("SPRAY_WATER", Changeflow); MessageDispatcher.RemoveListener("RUN_OUR_OF_WATER", CloseSpray); NetworkMessageDispatcher.AddListener("FIREROBOT_ROTATE_SYNC", RotationSync); } void OnDestroy() { MessageDispatcher.RemoveListener("SPRAY_WATER", Changeflow); MessageDispatcher.RemoveListener("RUN_OUR_OF_WATER", CloseSpray); NetworkMessageDispatcher.AddListener("FIREROBOT_ROTATE_SYNC", RotationSync); GetRealtimeConsume.getAllRealtimeConsume -= addMyRealtimeConsume; } private FloatData addMyRealtimeConsume(FloatData data) { data.value += Flow; return data; } void Update() { if (Flow > 0) { timer -= Time.deltaTime; if (timer <= 0) { var watertimepair = GetWaterAndTimeRemain(hosemanage.WaterSourceLine); RemainTime = watertimepair.Key; RemainWater = watertimepair.Value; if (watertimepair.Key != -100) { if (watertimepair.Key <= 0 || watertimepair.Value <= 0) {//没水了 stopSpray(); } } if (WorkType == FireRobotSkill.出泡沫) { float remainfoam = CheckFoamRemain(); if (remainfoam == -100 || remainfoam > 0) { GetComponent().ConnentSource.GetComponent().UserFoam((Flow / 0.97f) * 0.03f); } else { stopSpray(); } } timer = 1f; } } } private void Changeflow(IMessage obj) { var info = (flowchangeinfo)obj.Data; if (info.ChangObjId == basegameinfo.gameObjID) { GameObject watersource = GetComponent().ConnentSource; if (watersource != null) { watersource.GetComponent().SetTotalFlow(new KeyValuePair(gameObject, Flow), info.IsAdd); } } } /// /// 连接的水源没有水了 /// /// private void CloseSpray(IMessage obj) { // Debug.Log("over22"); if (WorkType == FireRobotSkill.出水 || WorkType == FireRobotSkill.出泡沫) { long watersourceId = (long)obj.Data; long thiswaterid = -1; if (GetComponent().ConnentSource != null) { thiswaterid = GetComponent().ConnentSource.GetComponent().gameObjID; } // Debug.Log(thiswaterid); if (watersourceId == thiswaterid) { waterrunoutof = true; WorkType = FireRobotSkill.待命; Flow = 0; if (FireRobotSkillPanelController.Instance) { if (FireRobotSkillPanelController.Instance.SelectRobot == gameObject) { FireRobotSkillPanelController.Instance.SprayWater.GetComponent().isOn = false; FireRobotSkillPanelController.Instance.SprayFroth.GetComponent().isOn = false; } } DestoryPartical(); } } } /// /// 机器人出水/泡沫 /// /// public void SetParticleType(SprayParticleType type) { if (type == SprayParticleType.Froth) { if (straightpart.gameObject.activeInHierarchy) { straightpart.gameObject.SetActive(false); } frothpart.gameObject.SetActive(true); frothpart.GetComponentInChildren().SetScaleValue(0.95f); ParticleSize = 0.95f; } if (type == SprayParticleType.WaterStraight) { if (frothpart.gameObject.activeInHierarchy) { frothpart.gameObject.SetActive(false); } straightpart.gameObject.SetActive(true); straightpart.GetComponentInChildren().SetScaleValue(0.2f); ParticleSize = 0.2f; } } /// /// 停止出水、出泡沫并同步 /// public void DestoryPartical() { for (int i = 0; i < Hose.childCount; i++) { if (Hose.GetChild(i).gameObject.activeInHierarchy) { Hose.GetChild(i).gameObject.SetActive(false); } } SpraySyncData spraysync = new SpraySyncData(); spraysync.SendUserID = CurrentUserInfo.mySelf.Id; spraysync.IsOn = false; spraysync.gameObjID = basegameinfo.gameObjID; NetworkManager.Default.SendAsync("SPRAY_WATER_SYNC", spraysync); } public void Up() { float V = VRotate.localRotation.eulerAngles.y; if (checkV(V) > MinVrotate) { V -= 5f; VRotate.localRotation = Quaternion.Euler(VRotate.localRotation.eulerAngles.x, V, VRotate.localRotation.eulerAngles.z); } else { V = MinVrotate; VRotate.localRotation = Quaternion.Euler(VRotate.localRotation.eulerAngles.x, V, VRotate.localRotation.eulerAngles.z); } RotationSync(); } public void Down() { float V = VRotate.localRotation.eulerAngles.y; if (checkV(V) < MaxVrotate) { V += 5f; VRotate.localRotation = Quaternion.Euler(VRotate.localRotation.eulerAngles.x, V, VRotate.localRotation.eulerAngles.z); } else { V = MaxVrotate; VRotate.localRotation = Quaternion.Euler(VRotate.localRotation.eulerAngles.x, V, VRotate.localRotation.eulerAngles.z); } RotationSync(); } public void Left() { float H = HRotate.localRotation.eulerAngles.y; H -= 10f; HRotate.localRotation = Quaternion.Euler(HRotate.localRotation.eulerAngles.x, H, HRotate.localRotation.eulerAngles.z); RotationSync(); } public void Right() { float H = HRotate.localRotation.eulerAngles.y; H += 10f; HRotate.localRotation = Quaternion.Euler(HRotate.localRotation.eulerAngles.x, H, HRotate.localRotation.eulerAngles.z); RotationSync(); } void RotationSync() { SprayRotateSyncData arg = new SprayRotateSyncData(); arg.SendUserID = CurrentUserInfo.mySelf.Id; arg.gameObjID = basegameinfo.gameObjID; arg.Vroatet = VRotate.localEulerAngles.y; arg.Hrotate = HRotate.localEulerAngles.y; NetworkManager.Default.SendAsync("FIREROBOT_ROTATE_SYNC", arg); } void RotateSync() { SprayRotateSyncData arg = new SprayRotateSyncData(); arg.SendUserID = CurrentUserInfo.mySelf.Id; arg.gameObjID = basegameinfo.gameObjID; arg.Vroatet = VRotate.localRotation.eulerAngles.y; arg.Hrotate = HRotate.localRotation.eulerAngles.y; NetworkManager.Default.SendAsync("FIREROBOT_ROTATE_SYNC", arg); } private void RotationSync(BinaryMessage obj) { var info = obj.Body.Deserialize(); if (info.SendUserID != CurrentUserInfo.mySelf.Id) { if (info.gameObjID == basegameinfo.gameObjID) { VRotate.localEulerAngles = new Vector3(VRotate.transform.localEulerAngles.x, info.Vroatet, VRotate.localEulerAngles.z); HRotate.localEulerAngles = new Vector3(HRotate.transform.localEulerAngles.x, info.Hrotate, HRotate.transform.localEulerAngles.z); } } } float checkV(float value) { if (value > 180) { return value -= 360; } else { return value; } } /// /// 连接水源时造成的水源信息修改 /// /// 直接水源上的两条线路信息总和 /// 直接水源id /// 连接的哪条线路 public void ChangeWaterSourceLineInfo(long directionId, int linenum) { List> go = CloneBySerialize.Clone(GetDirectWaterSourceInfo(directionId)); go.Insert(0, new List { directionId }); if (linenum == 0) { hosemanage.WaterSourceLine = go; } } /// /// 获取直接水源上的所有水源信息 /// /// 直接水源ID /// public List> GetDirectWaterSourceInfo(long directtionId) { List> go = new List>(); GameObject dirobj = EntitiesManager.Instance.GetEntityByID(directtionId); if (dirobj.GetComponent()) { TruckBindWaterSource dirTBC = dirobj.GetComponent(); List> line1 = CloneBySerialize.Clone(dirTBC.WaterSourceLine1); List> line2 = CloneBySerialize.Clone(dirTBC.WaterSourceLine2); line1.Reverse(); line2.Reverse(); int count = line1.Count >= line2.Count ? line1.Count : line2.Count; //List> go = new List>(count); for (int i = 0; i < count; i++) { List depth = new List(); if (i < line1.Count) { for (int j = 0; j < line1[i].Count; j++) { depth.Add(line1[i][j]); } } if (i < line2.Count) { for (int j = 0; j < line2[i].Count; j++) { depth.Add(line2[i][j]); } } go.Add(depth); } go.Reverse(); } else {//消防设施没有进水口 } return go; } public float CheckWaterRemain(List> watersouce) { float allremain = 0; for (int i = 0; i < watersouce.Count; i++) { for (int j = 0; j < watersouce[i].Count; j++) { WaterSource ws = EntitiesManager.Instance.GetEntityByID(watersouce[i][j]).GetComponent(); if (ws.TotalWater != -100) { allremain += (ws.TotalWater - ws.AllUserWater); } else { allremain = -100; break; } } } return allremain; } public KeyValuePair GetWaterAndTimeRemain(List> waterline) { KeyValuePair remain = new KeyValuePair(); //float allflow = 0; float remainwater = 0; List allwatersource = new List(); for (int i = 0; i < waterline.Count; i++) { for (int j = 0; j < waterline[i].Count; j++) { if (!allwatersource.Contains(waterline[i][j])) { allwatersource.Add(waterline[i][j]); } } } for (int i = 0; i < allwatersource.Count; i++) { WaterSource ws = EntitiesManager.Instance.GetEntityByID(allwatersource[i]).GetComponent(); if (ws.TotalWater != -100) { remainwater += (ws.TotalWater - ws.AllUserWater); //if (ws.GetComponent()) //{ // allflow += ws.GetComponent().Flow; //} } else { remainwater = -100; break; } } int remaintime = 0; //allflow += Flow; if (remainwater == -100) { remaintime = -100; } else { float realewater = 0;//实际分到的水量=自己的流量/直接水源的总流量*直接水源总流量 float dirTotalFlow = GetComponent().ConnentSource.GetComponent().TotalFlow; realewater = (Flow / dirTotalFlow) * remainwater; if (Flow != 0) remaintime = Mathf.CeilToInt(realewater / Flow); else remaintime = 0; } remain = new KeyValuePair(remaintime, remainwater); return remain; } void stopSpray() { Flow = 0; WorkType = FireRobotSkill.待命; RemainTime = 0; RemainWater = 0; for (int i = 0; i < Hose.childCount; i++) { if (Hose.GetChild(i).gameObject.activeInHierarchy) { Hose.GetChild(i).gameObject.SetActive(false); } } if (FireRobotSkillPanelController.Instance && FireRobotSkillPanelController.Instance.SelectRobot == gameObject) { FireRobotSkillPanelController.Instance.SprayWater.GetComponent().isOn = false; FireRobotSkillPanelController.Instance.SprayFroth.GetComponent().isOn = false; } SpraySyncData spraysync = new SpraySyncData(); spraysync.SendUserID = CurrentUserInfo.mySelf.Id; spraysync.spraytype = SprayParticleType.WaterStraight; spraysync.IsOn = false; spraysync.gameObjID = basegameinfo.gameObjID; NetworkManager.Default.SendAsync("SPRAY_WATER_SYNC", spraysync); } public float CheckFoamRemain() { float remain = 0; WaterSource ws = GetComponent().ConnentSource.GetComponent(); if (ws.foam) { if (ws.TotalFoam != -100) { remain = ws.TotalFoam - ws.AllUserFoam; } else { remain = -100; } } return remain; } }