using AX.MessageSystem; using AX.NetworkSystem; using System.Collections; using System.Collections.Generic; using UnityEngine; using AX.Network.Protocols; using System; using AX.Serialization; using UnityEngine.UI; public enum SnowCannonSkill { None, 喷水, 喷泡沫, } public class SonwWaterCannonSkillData { public long SenderId; public long GameObjId; public SnowCannonSkill skill; } public class SnowConnonController : MonoBehaviour { public List> WaterSourceLine = new List>();//进水口线路 public bool IsConnentWaterLine; public bool hasWaterConnect; public bool hasFoamConnect; public bool IsConnentWaterSource; public GameObject ConnentSource; public GameObject WaterLineConnent; public SnowCannonSkill skill = SnowCannonSkill.None; private Transform Hose; public GameObject FrothPrefabs; public GameObject WaterStraightPrefabs; /// /// 粒子大小 /// public float ParticleSize = 1; //暴风雪炮纵向旋转 public float VMinRotate = 0f; public float VMaxRotate = 20f; private Transform paotong; public SprayMode spraymode = SprayMode.PS30; public bool waterrunoutof; private bool IsSpray = false;//是否在出水/泡沫 /// /// 流量 /// public float flow; /// /// 剩余时间 /// public int RemainTime; /// /// 剩余水量 /// public float RemainWater; private float timer = 1f; private GameObject Waterstraight; private GameObject Froth; private BaseGameObjInfo basegameInfo; public float Flow { get { return flow; } set { flow = value; //if (WaterLineConnent) //{//修改管线是否有水 // WaterLineConnent.GetComponent().HasWaterCross = value > 0 ? true : false; //} if (WaterSourceLine.Count > 0) {//fenliu if (ConnentSource.GetComponent()) {//直接水源是车辆 float remain = CheckWaterRemain(WaterSourceLine); if (remain > 0 || remain == -100) {//水源有水 if (ConnentSource.GetComponent()) {//直接水源为水源车辆 ConnentSource.GetComponent().TotalFlowChange(basegameInfo.gameObjID, flow); } } else { Debug.Log("水源没水了"); } } else {//直接连接的消防设施 // GetComponent().ConnentSource.GetComponent().TotalFlow += (value - prveflow); ConnentSource.GetComponent().TotalFlowChange(basegameInfo.gameObjID, flow); } } //流量同步 FlowChangeData arg = new FlowChangeData { SenderId = CurrentUserInfo.mySelf.Id, GameObjId = GetComponent().gameObjID, Flow = value, }; NetworkManager.Default.SendAsync("FLOW_CHANGE_SYNC", arg); } } public SnowCannonSkill Skill { get { return skill; } set { skill = value; //SonwWaterCannonSkillData arg = new SonwWaterCannonSkillData //{ // SenderId = CurrentUserInfo.mySelf.Id, // GameObjId = GetComponent().gameObjID, // skill = value //}; //NetworkManager.Default.SendAsync("MOVE_WATERCANNON_SKILL_SYNC", arg); } } // Use this for initialization void Start() { paotong = transform.Find("XuePao_tong"); basegameInfo = GetComponent(); Hose = TransformHelper.FindChild(transform, "Hose"); //if (FrothPrefabs == null) //{ // FrothPrefabs = Resources.Load("Particle/Froth"); //} //if (WaterStraightPrefabs == null) // WaterStraightPrefabs = Resources.Load("Particle/WaterStraight"); Waterstraight = Hose.transform.Find("WaterStraight").gameObject; Froth = Hose.transform.Find("Froth").gameObject; MessageDispatcher.AddListener("SPRAY_WATER", Changeflow); MessageDispatcher.AddListener("RUN_OUR_OF_WATER", CloseSpray); MessageDispatcher.AddListener("HOSE_CONNECT_OR_DELECT", ConnectOrDelectControl); NetworkMessageDispatcher.AddListener("SNOWCANNON_ROTATE_SYNC", RotationSync); //MessageDispatcher.AddListener("RUN_OUR_OF_WATER", CloseSpray); //MessageDispatcher.AddListener("SPRAY_WATER", Changeflow); GetRealtimeConsume.getAllRealtimeConsume += addMyRealtimeConsume; //统计实时流量 } public void OnEnable() { } public void OnDisable() { MessageDispatcher.RemoveListener("SPRAY_WATER", Changeflow); MessageDispatcher.RemoveListener("RUN_OUR_OF_WATER", CloseSpray); MessageDispatcher.RemoveListener("HOSE_CONNECT_OR_DELECT", ConnectOrDelectControl); NetworkMessageDispatcher.RemoveListener("SNOWCANNON_ROTATE_SYNC", RotationSync); } public void OnDestroy() { MessageDispatcher.RemoveListener("SPRAY_WATER", Changeflow); MessageDispatcher.RemoveListener("RUN_OUR_OF_WATER", CloseSpray); MessageDispatcher.RemoveListener("HOSE_CONNECT_OR_DELECT", ConnectOrDelectControl); NetworkMessageDispatcher.RemoveListener("SNOWCANNON_ROTATE_SYNC", RotationSync); GetRealtimeConsume.getAllRealtimeConsume -= addMyRealtimeConsume; } private FloatData addMyRealtimeConsume(FloatData data) { data.value += Flow; return data; } /// /// 雪炮旋转同步 /// /// private void RotationSync(BinaryMessage obj) { var info = obj.Body.Deserialize(); if (info.SendUserID != CurrentUserInfo.mySelf.Id) { if (info.gameObjID == basegameInfo.gameObjID) { paotong.transform.localEulerAngles = info.VrotateVec; transform.localEulerAngles = info.HrotateVec; } } } //同步的管线连接到该水泡上或者连接在该水炮的管线同步删除时,可拖动控制 private void ConnectOrDelectControl(IMessage obj) { HoseConnectOrDelectData data = (HoseConnectOrDelectData)obj.Data; if (data.StartId == basegameInfo.gameObjID || data.EndId == basegameInfo.gameObjID) { //可挪动处理 if (data.Isconnect) { GetComponent().enabled = false; } else { GetComponent().enabled = true; } //水源信息处理 GameObject connectHose = EntitiesManager.Instance.GetEntityByID(data.HoseId); GameObject anther = connectHose.GetComponent().GetAnotherGameObj(basegameInfo.gameObjID); if (data.Isconnect) { if (anther.GetComponent()) {//另一端是水源 IsConnentWaterSource = true; ConnentSource = anther; hasWaterConnect = anther.GetComponent().water; hasFoamConnect = anther.GetComponent().foam; IsConnentWaterLine = true; WaterLineConnent = connectHose; ChangeWaterSourceLineInfo(data.StartId, 0); ////如果连接了一个没有水的水源 //if (anther.GetComponent().hasover) //{ // waterrunoutof = true; //} if (SnowConnonSkillPanelController.Instance) { if (SnowConnonSkillPanelController.Instance.SelectSnowConnon == gameObject) { SnowConnonSkillPanelController.Instance.SprayFoam.GetComponent().interactable = hasFoamConnect; SnowConnonSkillPanelController.Instance.SprayWater.GetComponent().interactable = hasWaterConnect; } } } //if (anther.GetComponent() && !anther.GetComponent()) //{//另一端是非水源出水车辆 // ConnentSource = anther.GetComponent().ConnentWaterSource; // if (ConnentSource != null) // { // IsConnentWaterSource = true; // hasWaterConnect = anther.GetComponent().ConnectWater; // hasFoamConnect = anther.GetComponent().ConnectFoam; // } //} } else { Resert(); } } } // Update is called once per frame void Update() { if (ConnentSource != null) { // if (waterrunoutof == false) { if (Flow > 0) { timer -= Time.deltaTime; if (timer <= 0) { var watertimepair = GetWaterAndTimeRemain(WaterSourceLine); RemainTime = watertimepair.Key; RemainWater = watertimepair.Value; if (watertimepair.Key != -100) { if (watertimepair.Key <= 0 || watertimepair.Value <= 0) {//没水了 stopSpray(); } } if (Skill == SnowCannonSkill.喷泡沫) { float remainfoam = CheckFoamRemain(); if (remainfoam == -100 || remainfoam > 0) { ConnentSource.GetComponent().UserFoam((Flow / 0.97f) * 0.03f); } else { // LoadPromptWin.Instance.LoadTextPromptWindow("泡沫量不足", 1f); stopSpray(); } } timer = 1f; } } } } } private void Changeflow(IMessage obj) { var info = (flowchangeinfo)obj.Data; if (info.ChangObjId == basegameInfo.gameObjID) { if (ConnentSource != null) { ConnentSource.GetComponent().SetTotalFlow(new KeyValuePair(gameObject, Flow), info.IsAdd); } } IsSpray = info.IsAdd; } /// /// 连接的水源没有水了 /// /// private void CloseSpray(IMessage obj) { if (skill == SnowCannonSkill.喷水 || skill == SnowCannonSkill.喷泡沫) { long watersourceId = (long)obj.Data; long thiswaterid = ConnentSource.GetComponent().gameObjID; // Debug.Log(thiswaterid); if (watersourceId == thiswaterid) { waterrunoutof = true; Flow = 0; skill = SnowCannonSkill.None; //if (MoveWaterConnonSkillPanelController.Instance) //{ // if (MoveWaterConnonSkillPanelController.Instance.selectMoveWaterConnon == gameObject) // { // MoveWaterConnonSkillPanelController.Instance.SprayFoam.GetComponent().isOn = false; // MoveWaterConnonSkillPanelController.Instance.SprayWater.GetComponent().isOn = false; // } //} Waterstraight.SetActive(false); Froth.SetActive(false); SpraySyncData spraysync = new SpraySyncData(); spraysync.SendUserID = CurrentUserInfo.mySelf.Id; spraysync.IsOn = false; spraysync.gameObjID = basegameInfo.gameObjID; NetworkManager.Default.SendAsync(/*CurrentUserInfo.mySelf.Id,*/ "SPRAY_WATER_SYNC", spraysync); } } } public void Spray(SnowCannonSkill type) { if (type == SnowCannonSkill.喷水) { Waterstraight.SetActive(true); Froth.SetActive(false); Waterstraight.GetComponentInChildren().SetScaleValue(0.75f); ParticleSize = 0.75f; } if (type == SnowCannonSkill.喷泡沫) { Waterstraight.SetActive(false); Froth.SetActive(true); Froth.GetComponentInChildren().SetScaleValue(1.1f); ParticleSize = 1.1f; } if (type == SnowCannonSkill.None) { for (int i = 0; i < Hose.childCount; i++) { Hose.GetChild(i).gameObject.SetActive(false); } } } public void Up() { float v = paotong.transform.localEulerAngles.x; if ((v > 280 && v < 360) || (v < 25)) { paotong.transform.Rotate(5, 0, 0); } // Debug.Log(v); RotateSync(); } public void Down() { float v = paotong.transform.localEulerAngles.x; if ((v > 275 && v < 360) || (v < 20)) { paotong.transform.Rotate(-5, 0, 0); } RotateSync(); } public void Left() { transform.Rotate(0, -10, 0); RotateSync(); } public void Right() { transform.Rotate(0, 10, 0); RotateSync(); } void RotateSync() { SprayRotateSyncData arg = new SprayRotateSyncData(); arg.SendUserID = CurrentUserInfo.mySelf.Id; arg.gameObjID = basegameInfo.gameObjID; arg.VrotateVec = paotong.transform.localEulerAngles; arg.HrotateVec = transform.localEulerAngles; NetworkManager.Default.SendAsync("SNOWCANNON_ROTATE_SYNC", arg); } public void Resert() { IsConnentWaterLine = false; WaterLineConnent = null; IsConnentWaterSource = false; ConnentSource = null; hasFoamConnect = false; hasWaterConnect = false; WaterSourceLine = new List>(); if (SnowConnonSkillPanelController.Instance) { if (SnowConnonSkillPanelController.Instance.SelectSnowConnon == gameObject) { SnowConnonSkillPanelController.Instance.SprayWater.GetComponent().isOn = false; SnowConnonSkillPanelController.Instance.SprayWater.GetComponent().interactable = false; SnowConnonSkillPanelController.Instance.SprayFoam.GetComponent().isOn = false; SnowConnonSkillPanelController.Instance.SprayFoam.GetComponent().interactable = false; } } if (Hose.GetComponentInChildren()) { for (int i = 0; i < Hose.childCount; i++) { 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); } } /// /// 连接水源时造成的水源信息修改 /// /// 直接水源上的两条线路信息总和 /// 直接水源id /// 连接的哪条线路 public void ChangeWaterSourceLineInfo(long directionId, int linenum) { List> go = CloneBySerialize.Clone(GetDirectWaterSourceInfo(directionId)); go.Insert(0, new List { directionId }); if (linenum == 0) { 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 = 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() { Spray(SnowCannonSkill.None); Skill = SnowCannonSkill.None; Flow = 0; RemainTime = 0; RemainWater = 0; if (SnowConnonSkillPanelController.Instance && SnowConnonSkillPanelController.Instance.SelectSnowConnon == gameObject) { if (SnowConnonSkillPanelController.Instance.SprayFoam.GetComponent().isOn) { SnowConnonSkillPanelController.Instance.SprayFoam.GetComponent().isOn = false; } if (SnowConnonSkillPanelController.Instance.SprayWater.GetComponent().isOn) { SnowConnonSkillPanelController.Instance.SprayWater.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 = ConnentSource.GetComponent(); if (ws.foam) { if (ws.TotalFoam != -100) { remain = ws.TotalFoam - ws.AllUserFoam; } else { remain = -100; } } return remain; } }