using System.Collections; using System.Collections.Generic; using UnityEngine; using AX.NetworkSystem; using AX.Network.Protocols; using AX.Serialization; public struct ShuiPaoControl { public long gameObjectID; public bool isControlling; } public struct ShuiPaoSyncData { public long gameObjectID; public bool waterOpen; public float ParticleSize; public Quaternion dizuoRotation; public Quaternion shuiQiangRotation; } public class GuDingShuiPaoControl : MonoBehaviour { // Use this for initialization private ObjSelectCtrl objSelectCtrl; private bool isControlling; private long gameObjectID; private GameObject dizuo; private GameObject shuiQiang; private GameObject water; public bool waterOpen; public float ParticleSize=1; private GameObject waterStraightPrefab; /// /// 流量 /// public float Flow; /// /// 剩余时间 /// public int RemainTime; /// /// 剩余水量 /// public int RemianWater; public float AllUseWater; private float timer = 1f; /// /// 出水类型 /// public SprayParticleType SpType; public SprayMode spraymode = SprayMode.PS30; void Start() { Init(); dizuo = transform.GetComponentInChildren().gameObject; shuiQiang = transform.GetComponentInChildren().gameObject; gameObjectID = GetComponent().gameObjID; if(SpType==SprayParticleType.WaterStraight) { waterStraightPrefab = Resources.Load("Particle/WaterStraight"); } if (SpType == SprayParticleType.Froth) { waterStraightPrefab = Resources.Load("Particle/Froth"); } NetworkMessageDispatcher.AddListener("GUDINGSHUIPAO_CONTROL_SYNC", SetController); NetworkMessageDispatcher.AddListener("GUDINGSHUIPAO_DATA_SYNC", SetShuiPao); SprayParameter spray = SprayParameter.GetInfo(SprayMode.PS30); float value= (ParticleSize - spray.minsize) / (spray.maxsize - spray.minsize); Flow = (spray.maxflow - spray.minflow) * value + spray.minflow; } void OnDestroy() { NetworkMessageDispatcher.RemoveListener("GUDINGSHUIPAO_CONTROL_SYNC", SetController); NetworkMessageDispatcher.RemoveListener("GUDINGSHUIPAO_DATA_SYNC", SetShuiPao); } // Update is called once per frame void Update() { if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长) { if (objSelectCtrl.selected && !isControlling) { isControlling = true; NetworkManager.Default.SendAsync("GUDINGSHUIPAO_CONTROL_SYNC", new ShuiPaoControl { gameObjectID = gameObjectID, isControlling = isControlling }); } if (!objSelectCtrl.selected && isControlling) { isControlling = false; NetworkManager.Default.SendAsync("GUDINGSHUIPAO_CONTROL_SYNC", new ShuiPaoControl { gameObjectID = gameObjectID, isControlling = isControlling }); } } if (waterOpen) { timer -= Time.deltaTime; if (timer <= 0) { AllUseWater += Flow; timer = 1f; } } } private void Init() { if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长) { GetComponent().UserID = CurrentUserInfo.mySelf.Id; objSelectCtrl = gameObject.AddComponent(); SelectedObjs.gameObjs.Add(gameObject); } } void SetController(BinaryMessage message) { if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长) { var shuiPaoControl = message.Body.Deserialize(); if (shuiPaoControl.gameObjectID == gameObjectID) { if (shuiPaoControl.isControlling) { objSelectCtrl.enabled = false; } else { objSelectCtrl.enabled = true; } } } } public void SetWater(bool flag) { if (water == null) { water = Instantiate(waterStraightPrefab, shuiQiang.transform); water.transform.Rotate(0, 180, 0); ParticleSize = GetComponentInChildren(true).GetScaleValue(); } water.SetActive(flag); waterOpen = flag; DataSync(); } public void ChangeSize(float value) { ParticleControlOfType currentparticle = GetComponentInChildren(true); if (currentparticle != null) { currentparticle.SetScaleValue(value); ParticleSize = value; } DataSync(); } public void Up() { float x = shuiQiang.transform.localEulerAngles.x; x = CheckValue(x); if (x < 70) { shuiQiang.transform.Rotate(10, 0, 0, Space.Self); } DataSync(); } public void Down() { float x = shuiQiang.transform.localEulerAngles.x; x = CheckValue(x); Debug.Log(x); if (x > -15) { shuiQiang.transform.Rotate(-10, 0, 0, Space.Self); } DataSync(); } public void Left() { dizuo.transform.Rotate(0, -10, 0, Space.Self); DataSync(); } public void Right() { dizuo.transform.Rotate(0, 10, 0, Space.Self); DataSync(); } protected float CheckValue(float angles) { if (angles >= 180 && angles <= 360) { angles = angles - 360; } return angles; //举例:假如获取到的localEulerAngles.x = 300,则在显示面板其显示的值为-60=300-360,用此显式值计算 } private void DataSync() { var shuiPaoSyncData = new ShuiPaoSyncData(); shuiPaoSyncData.gameObjectID = gameObjectID; shuiPaoSyncData.waterOpen = waterOpen; shuiPaoSyncData.ParticleSize = ParticleSize; shuiPaoSyncData.dizuoRotation = dizuo.transform.localRotation; shuiPaoSyncData.shuiQiangRotation = shuiQiang.transform.localRotation; NetworkManager.Default.SendAsync("GUDINGSHUIPAO_DATA_SYNC", shuiPaoSyncData); } void SetShuiPao(BinaryMessage message) { var shuiPaoControl = message.Body.Deserialize(); if (shuiPaoControl.gameObjectID== gameObjectID) { //开关 var flag = shuiPaoControl.waterOpen; if (water == null) { water = Instantiate(waterStraightPrefab, shuiQiang.transform); water.transform.Rotate(0, 180, 0); ParticleSize = GetComponentInChildren(true).GetScaleValue(); } water.SetActive(flag); waterOpen = flag; //大小 var value = shuiPaoControl.ParticleSize; ParticleControlOfType currentparticle = GetComponentInChildren(true); if (currentparticle != null) { currentparticle.SetScaleValue(value); ParticleSize = value; } //方向 shuiQiang.transform.localRotation = shuiPaoControl.shuiQiangRotation; dizuo.transform.localRotation = shuiPaoControl.dizuoRotation; } } }