using System; using System.Collections; using System.Collections.Generic; using AX.MessageSystem; using UnityEngine; using AX.InputSystem; using AX.NetworkSystem; public class CloneWaterCurtainHose : CloneBase { [SerializeField] [Tooltip("距离地面距离")] protected float Height = 0; private GameObject WaterCurtainHoseParent; //水幕水带父对象 private MoveWaterCannonTool movetool; private Vector3 startPoint = Vector3.zero; private Vector3 endPoint = Vector3.zero; private long currentWCHoseParentId = -1;//当前次克隆水幕水带父对象的gameObjId public override void OnEnable() { base.OnEnable(); MessageDispatcher.AddListener("INIT_WATERCURTAINHOSE_STARTPOINT", ResetStartPoint); } public override void OnDisable() { base.OnDisable(); MessageDispatcher.RemoveListener("INIT_WATERCURTAINHOSE_STARTPOINT", ResetStartPoint); } public override void OnDestroy() { base.OnDestroy(); MessageDispatcher.RemoveListener("INIT_WATERCURTAINHOSE_STARTPOINT", ResetStartPoint); } private void ResetStartPoint(IMessage obj) { startPoint = Vector3.zero; //取消水幕水带克隆按钮选中重置初始点时,若没有克隆出水幕水带子对象,则删除水幕水带父对象 var currentWCHoseParent = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId); if (currentWCHoseParent) { if (currentWCHoseParent.transform.childCount == 0) { EntitiesManager.Instance.DeleteObj(currentWCHoseParent); } else {//已克隆有子对象时,结束当前次水幕水带克隆操作时,算出水幕水带上出水位置 MessageDispatcher.SendMessage("GET_SPRAYWATER_POSITIONS", currentWCHoseParentId); } //防止已克隆一次完整水幕水带后(currentWCHoseParentId有值),再次选中水幕水带克隆按钮,但不克隆就取消造成判断内代码块再次执行 currentWCHoseParentId = -1; } } private void Start() { if (WaterCurtainHoseParent == null) WaterCurtainHoseParent = Resources.Load("ClonePrefabs/Tool/WaterCurtainHoseParent") as GameObject; } public override void Execute(IMessage obj) { var gameObjID = (long)obj.Sender; var data = ((CloneCmdArgs)obj.Data); if (data.cloneObjType == cloneObjType) { if (SkillTollPanel.Instance) { if (SkillTollPanel.Instance.GetComponent()) { movetool = SkillTollPanel.Instance.GetComponent(); } } if (movetool && int.Parse(movetool.waterCurtainHoseText.text) <= 0) { LoadPromptWin.Instance.LoadTextPromptWindow("水幕水带数量不足", 1f); return; } var hitPoint = data.hitPos; if (startPoint == Vector3.zero) {//第一次点击点时 var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); if (hitObj.GetComponent()) {//如果点击的点在水源车,消火栓,泡沫栓上,需把点高度移动到贴地 if (!checkWaterSourceOutPoint(hitObj)) return; //startPoint = new Vector3(hitPoint.x, hitObj.transform.position.y, hitPoint.z); startPoint = hitObj.transform.position;//吸附到水源位置 } else {//否则直接赋值 startPoint = hitPoint; } //点击第一个点时,先克隆父对象 var clonedObj = EntitiesManager.Instance.CreateObj(WaterCurtainHoseParent, startPoint, transform, gameObjID); clonedObj.name = CloneObjNameTool.Instance().GetCloneObjectName(cloneObjType); SelectedObjs.gameObjs.Add(clonedObj); //设置克隆物体所在楼层等基本属性,属性从点击的对象上获取 CloneGameObjInfo hitObjInfo = hitObj.GetComponent(); CloneGameObjInfo cloneObjInfo = clonedObj.GetComponent(); cloneObjInfo.gameObjType = cloneObjType; cloneObjInfo.UserID = CurrentUserInfo.mySelf.Id; cloneObjInfo.buildNum = hitObjInfo.buildNum; cloneObjInfo.floorNum = hitObjInfo.floorNum; cloneObjInfo.interlayerNum = hitObjInfo.interlayerNum; //记录当前次克隆水幕水带的父对象Id currentWCHoseParentId = gameObjID; //加入第一个点到水幕水带路径点数组 clonedObj.GetComponent().pathPoints.Add(startPoint); if (hitObj.GetComponent()) {//给水幕水带绑定水源(可能是水源车,可能是消火栓,泡沫栓等) clonedObj.GetComponent().waterSourceGameObjId = hitObj.GetComponent().gameObjID; LoadPromptWin.Instance.LoadTextPromptWindow("连接水源成功", 1f); clonedObj.GetComponent().startPointIsWaterSource = true; //本客户端水幕水带所连接的水源为车时,在车上绑定所连接的水幕水带 WaterCarBindCurtainHose waterCarBindCurtainHose = new WaterCarBindCurtainHose(); waterCarBindCurtainHose.WaterSourceObjId = hitObj.GetComponent().gameObjID; waterCarBindCurtainHose.WaterCurtainHose = clonedObj; waterCarBindCurtainHose.isConnect = true; MessageDispatcher.SendMessage("WATERCAR_BIND_CURTAINHOSE", waterCarBindCurtainHose); } return; } if (startPoint != Vector3.zero) {//后续点击点时 endPoint = hitPoint; var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); var currentWCHoseParent = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId); if (hitObj.GetComponent()) {//后续点击的点点击到水源上, if (currentWCHoseParent.GetComponent().waterSourceGameObjId != -1) {//当前水幕水带起始点已绑定了水源的情况下 LoadPromptWin.Instance.LoadTextPromptWindow("起始端已连接水源,无需再连接到水源", 1f); return; } else { if (!checkWaterSourceOutPoint(hitObj)) return; //endPoint = new Vector3(hitPoint.x, hitObj.transform.position.y, hitPoint.z);//点贴地 endPoint = hitObj.transform.position;//吸附到水源位置 } } } float distance = Vector3.Distance(startPoint, endPoint);//计算两点的距离 if (distance < 2) { LoadPromptWin.Instance.LoadTextPromptWindow("两点重合", 1f); return; } if (movetool && int.Parse(movetool.waterCurtainHoseText.text) * 25 < distance) { LoadPromptWin.Instance.LoadTextPromptWindow("剩余水幕水带长度不足", 1f); return; } Vector3 clonedObjPos = (startPoint + endPoint) / 2; clonedObjPos = new Vector3(clonedObjPos.x, clonedObjPos.y + Height, clonedObjPos.z); //水幕水带子对象不加入到实体管理器,其父对象加入到了实体管理器及能选中的数组里 //水幕水带子对象gameObjID赋值跟其父对象一样,且加上BoxCollider,这样 子对象BoxCollider响应选中点击 GameObject WaterCurtainHoseChild = null; if (EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId)) { WaterCurtainHoseChild = Instantiate( clonePrefab, clonedObjPos, Quaternion.identity, EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).transform) as GameObject; WaterCurtainHoseChild.GetComponent().SetGameObjID(currentWCHoseParentId); SelectedObjs.gameObjs.Add(WaterCurtainHoseChild); } WaterCurtainHoseChild.transform.forward = (-(endPoint - startPoint)).normalized;//改变线条的朝向 WaterCurtainHoseChild.transform.localScale = new Vector3(30f, 30f, distance * 108);//延长线条,连接两点。 WaterCurtainHoseChild.AddComponent(); if (!WaterCurtainHoseChild.GetComponent()) { WaterCurtainHoseChild.AddComponent().ReassignId((ulong)CurrentUserInfo.mySelf.Id, true); } //设置克隆物体所在楼层等基本属性,属性从点击的对象上获取 CloneGameObjInfo childCloneObjInfo = WaterCurtainHoseChild.GetComponent(); childCloneObjInfo.gameObjType = cloneObjType; childCloneObjInfo.UserID = CurrentUserInfo.mySelf.Id; if (EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId)) { childCloneObjInfo.buildNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().buildNum; childCloneObjInfo.floorNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().floorNum; childCloneObjInfo.interlayerNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().interlayerNum; } //加入后续点到水幕水带路径点数组 EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().pathPoints.Add(endPoint); //克隆一个水幕水带子对象,调用一次,计算水幕水带消耗及改变其克隆按钮上的剩余盘数(默认25米1盘) if (data.cloneObjType == CloneObjType.WaterCurtainHose) { MessageDispatcher.SendMessage("UseWaterCurtainHose", distance); } var hitGameObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); var currentWaterCHoseParent = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId); if (hitGameObj.GetComponent() && currentWaterCHoseParent.GetComponent().waterSourceGameObjId == -1) {//后续点击的点点击到水源上,且当前水幕水带还没绑定水源的情况下,绑定水源且结束当前次克隆水幕水带 currentWaterCHoseParent.GetComponent().waterSourceGameObjId = hitGameObj.GetComponent().gameObjID; LoadPromptWin.Instance.LoadTextPromptWindow("连接水源成功", 1f); currentWaterCHoseParent.GetComponent().startPointIsWaterSource = false; //本客户端水幕水带所连接的水源为车时,在车上绑定所连接的水幕水带 WaterCarBindCurtainHose waterCarBindCurtainHose = new WaterCarBindCurtainHose(); waterCarBindCurtainHose.WaterSourceObjId = hitGameObj.GetComponent().gameObjID; waterCarBindCurtainHose.WaterCurtainHose = currentWaterCHoseParent; waterCarBindCurtainHose.isConnect = true; MessageDispatcher.SendMessage("WATERCAR_BIND_CURTAINHOSE", waterCarBindCurtainHose); CloneWaterCurtainHoseSync();//结束端绑定水源结束当前次克隆水幕水带时,需先同步再结束,否则currentWCHoseParentId为-1 startPoint = endPoint; //结束当前次克隆水幕水带 MessageDispatcher.SendMessage("CANCEL_CLONEBTN_SELECTED_COMMAND", new CloneCmdArgs() { selected = false }); return; } CloneWaterCurtainHoseSync(); startPoint = endPoint; } } /// /// 克隆水幕水带同步 /// public void CloneWaterCurtainHoseSync() { //克隆水幕水带同步 var syncData = new WaterCurtainHoseSyncData(); //水幕水带父对象需要同步的信息 syncData.WaterCHoseParentSyncData.gameObjID = currentWCHoseParentId; syncData.WaterCHoseParentSyncData.gameObjType = cloneObjType; syncData.WaterCHoseParentSyncData.UserID = CurrentUserInfo.mySelf.Id; syncData.WaterCHoseParentSyncData.buildNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().buildNum; syncData.WaterCHoseParentSyncData.floorNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().floorNum; syncData.WaterCHoseParentSyncData.interlayerNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().interlayerNum; syncData.WaterCHoseParentSyncData.SendUserID = CurrentUserInfo.mySelf.Id; syncData.WaterCHoseParentSyncData.ClonePosition = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).transform.position; syncData.WaterCHoseParentSyncData.Name = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).name; //水幕水带子对象需要同步的信息 syncData.WaterCHoseChildSyncData.gameObjID = currentWCHoseParentId; syncData.WaterCHoseChildSyncData.gameObjType = cloneObjType; syncData.WaterCHoseChildSyncData.UserID = CurrentUserInfo.mySelf.Id; syncData.WaterCHoseChildSyncData.buildNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().buildNum; syncData.WaterCHoseChildSyncData.floorNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().floorNum; syncData.WaterCHoseChildSyncData.interlayerNum = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().interlayerNum; syncData.WaterCHoseChildSyncData.SendUserID = CurrentUserInfo.mySelf.Id; syncData.WaterCHoseChildSyncData.StartPoint = startPoint; syncData.WaterCHoseChildSyncData.EndPoint = endPoint; syncData.WaterCHoseChildSyncData.Height = Height; syncData.waterSourceObjId = EntitiesManager.Instance.GetEntityByID(currentWCHoseParentId).GetComponent().waterSourceGameObjId; NetworkManager.Default.SendAsync("CLONE_WATERCURTAINHOSE_SYNC", syncData); } bool checkWaterSourceOutPoint(GameObject hitobj) { bool can = true; if (hitobj.GetComponent()) {//水源车,两个出水口 TruckBindWaterSource tbs = hitobj.GetComponent(); if (tbs.DirectConnectObj1.Key != null && tbs.DirectConnectObj2.Key != null) { LoadPromptWin.Instance.LoadTextPromptWindow("该水源没有更多出水口", 1f); can = false; } } else { if (hitobj.GetComponent().ConnectGameObj != null) { LoadPromptWin.Instance.LoadTextPromptWindow("该水源没有更多出水口", 1f); can = false; } } return can; } }