using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.UI; public class FireRobotLayWaterHose : MonoBehaviour { private string parentName; public GameObject WaterHoses; private string bandtype = "OrdinaryHose"; // public GameObject waterhose; private int num = 1; private Dictionary hoseDic = new Dictionary(); private Dictionary lengthDic = new Dictionary(); private NavMeshAgent agent; private bool flag = false; private List allInits = new List(); private Vector3[] path = null; private Dictionary pathCorners = new Dictionary(); private float time = 0; //private FireManMessage fireManMessage; private NavMeshPath NavMeshPath; private Vector3 InitPoint; private Vector3 endPoint; //记录水带用完的点 private Vector3 checkPoint; private int pathIndex = 0; private Dictionary> pathMap = new Dictionary>(); //第几次路线包含哪几段 private Transform shuiqiang; private bool spraying = false; private FireRobotWaterHoseManage waterHoseManage; //private List equips; //保存消防员所有的装备 // private MyEquipObject equipedHoses; //保存消防员装备的水带 private bool canreinit = true; // public float LayLenght;//本次铺设长度 public float remainlenght;//该消防员剩余水带 public GameObject ParentLayHosePrefabs; /// ///铺设水带的父物体 /// public GameObject ParentLayHose; /// /// 是否是捡起来继续铺 /// public bool IsTackUp; /// /// 捡起的水带 /// public GameObject TackHose; /// /// 如果是本客户端 /// public bool IsselfCurrent = true; private int creatnum = 1; private int hasUseNum; public int HasUseNum { get { return hasUseNum; } set { int prve = hasUseNum; hasUseNum = value; if (prve != value) { if (value > prve) {//往前铺 if (GetComponent().HasEquip("消防高压水带")) { GetComponent().UseEquip("消防高压水带", (value - prve)); if (GetComponent().HasEquip("消防高压水带")) { waterHoseManage.NowRemainWaterHose = GetComponent().GetEquip("消防高压水带").Number; } else { waterHoseManage.NowRemainWaterHose = 0; } } // float more= GetSelfConsumedLength() - waterHoseManage.NowRemainWaterHose * waterHoseManage.OneDefaultHoseLenght; // waterHoseManage.WaterHoseLenghtRemain = waterHoseManage.NowRemainWaterHose * waterHoseManage.OneDefaultHoseLenght; } else { // Equip hose = EquipManager.Instance.GetEquipByID("82"); // hose.Number += (prve - value); if (GetComponent().HasEquip("消防高压水带")) Debug.Log(GetComponent().GetEquip("消防高压水带").Number + "收起前"); GetComponent().AddEquip("82", (prve - value)); if (GetComponent().HasEquip("消防高压水带")) Debug.Log(GetComponent().GetEquip("消防高压水带").Number + "收起后"); // waterHoseManage.WaterHoseLenghtRemain = waterHoseManage.NowRemainWaterHose * waterHoseManage.OneDefaultHoseLenght; } } } } // public int hosenum; //public float hoseremain; // Use this for initialization void Start() { NavMeshPath = new NavMeshPath(); agent = GetComponent(); //fireManMessage = GetComponent(); shuiqiang = TransformHelper.FindChild(transform, "WaterEnterPos"); //生成所有水带的父物体 parentName = name;//GetComponent().gameObjID.ToString(); GameObject AllParent = GameObject.Find("P_AllParent/P_Tools/P_FireRobotFollowHose"); if (!AllParent.transform.Find(parentName)) { WaterHoses = Instantiate(Resources.Load("Lines/WaterHoses")); WaterHoses.transform.parent = AllParent.transform; WaterHoses.name = parentName; } else { WaterHoses = AllParent.transform.Find(parentName).gameObject; } if (ParentLayHosePrefabs == null) { ParentLayHosePrefabs = Resources.Load("LineParent/ParentLayWaterHose"); } waterHoseManage = GetComponent(); } // Update is called once per frame void Update() { if (flag && agent.velocity.magnitude > 0) //开启“铺设水带”,并且开始寻路时才会执行 { if (remainlenght <= 0.5f) { agent.SetDestination(transform.position); return; } HasUseNum = Mathf.CeilToInt(GetSelfConsumedLength() / 20); ParentLayHose.GetComponent().LineLenght = GetSelfConsumedLength(); ParentLayHose.GetComponent().UseNum = Mathf.CeilToInt(GetSelfConsumedLength() / 20); ParentLayHose.GetComponent().RemainLenght = Mathf.CeilToInt(GetSelfConsumedLength() / 20) * 20 - GetSelfConsumedLength(); // hosenum = ParentLayHose.GetComponent().UseNum; // hoseremain = ParentLayHose.GetComponent().RemainLenght; remainlenght = waterHoseManage.NowRemainWaterHose * waterHoseManage.OneDefaultHoseLenght + ParentLayHose.GetComponent().RemainLenght; waterHoseManage.WaterHoseLenghtRemain = remainlenght; endPoint = transform.position; //LayLenght= GetSelfConsumedLength(); if (remainlenght > 0 && remainlenght <= 2f) //临界带 { agent.SetDestination(transform.position); endPoint = transform.position; if (IsselfCurrent) { LoadPromptWin.Instance.LoadTextPromptWindow("水带用完", 1f); } } if (ParentLayHose) { DrawByTimespan();//按更新的铺设路线画一次水带 } } if (flag) { //if (spraying) //&& agent.velocity.magnitude==0) //{ // DrawTail(); //} //else //{ // DestroyTail(); //} } } public float GetSelfConsumedLength() { float length = 0; if (lengthDic.Count > 0) { foreach (float l in lengthDic.Values) { length += l; } } return length; } private void DrawHoseLine(Vector3 position1, Vector3 position2, int number) { Vector3 midPosition = (position1 + position2) / 2; midPosition = new Vector3(midPosition.x, midPosition.y + 0.1f, midPosition.z); string linename = "waterhose-" + number; GameObject hose; if (!ParentLayHose.transform.Find(linename)) { hose = Instantiate(Resources.Load("Lines/" + bandtype), midPosition, Quaternion.identity) as GameObject; hose.name = linename; } else { hose = ParentLayHose.transform.Find(linename).gameObject; hose.transform.position = midPosition; } hose.transform.parent = ParentLayHose.transform; hose.transform.forward = (position2 - position1).normalized;//改变朝向 if (!hose.GetComponent()) { hose.AddComponent(); } float distance = Vector3.Distance(position2, position1); hose.transform.localScale = new Vector3(25, 25, distance * 107);//延长线条,连接两点 // hose.transform.position = hose.transform.position + Vector3.up * 0.5f; SetHoseFloorInfo(hose.transform.parent.gameObject, position2); UpdateHoseDic(number, hose); UpdateLength(position1, position2, number); List intlist = pathMap[pathIndex]; if (!intlist.Contains(number)) { pathMap[pathIndex].Add(number); //第pathIndex条路线包括水带段number } } private void UpdateLength(Vector3 position1, Vector3 position2, int number) { if (lengthDic.ContainsKey(number)) //如果字典里已包含此序号段水带的长度 { lengthDic[number] = Vector3.Distance(position1, position2);//就更新长度 } else //如果不包含此序号段水带的长度 { lengthDic.Add(number, Vector3.Distance(position1, position2));//就添加长度 } } private void UpdateHoseDic(int number, GameObject hose) { if (hoseDic.ContainsKey(number)) { hoseDic[number] = hose; } else { hoseDic.Add(number, hose); } } private void HasHose() { float remain = remainlenght;//waterHoseManage.WaterHoseLenghtRemain; if (remain > 0.5f) { endPoint = transform.position; //记录末点位置 canreinit = true; //可以更新InitPoint return; } else if (remain > 0 && remain <= 0.5f) //临界带 { canreinit = false; if (IsselfCurrent) { LoadPromptWin.Instance.LoadTextPromptWindow("水带用完", 1f); } return; } } public void StartLay() { agent.SetDestination(transform.position); if (!GetComponent().pathFindEnable) { GetComponent().pathFindEnable = true; } // 如果当前起始位置与上一次铺设的末点距离较近,则认为此次铺设是上一次铺设的继续 if (endPoint != Vector3.zero && Vector3.Distance(transform.position, endPoint) < 5) { //如果放下期间没有被别人捡起,继续铺设 //更新消防员可用水带 if (ParentLayHose.GetComponent().RemainLenght > 0) { //GetComponent().CalculateBeforelay(ParentLayHose.GetComponent().RemainLenght, ParentLayHose.GetComponent().UseNum); } //更新消防员水源信息 if (ParentLayHose.GetComponent().WaterSource != null) { gameObject.GetComponent().ResetWaterSourceConnectInfo (true, ParentLayHose.GetComponent().ConnectWater, ParentLayHose.GetComponent().ConnectFoam, ParentLayHose.GetComponent().WaterSource, ParentLayHose.GetComponent().ConnectWaterHose); if (FireRobotSkillPanelController.Instance) { if (ParentLayHose.GetComponent().WaterSource.GetComponent().water) { FireRobotSkillPanelController.Instance.SprayWater.GetComponent().interactable = true; } if (ParentLayHose.GetComponent().WaterSource.GetComponent().foam) { FireRobotSkillPanelController.Instance.SprayFroth.GetComponent().interactable = true; } } } else { waterHoseManage.ResetWaterSourceConnectInfo(false, false, false, null, null); } flag = true; ParentLayHose.GetComponent().ConnectFireman = gameObject; } //如果当前起始位置与上一次铺设的末点距离较远,则认为开始一段新的铺设,不会收回上一次铺设的管线 else { lengthDic.Clear(); if (remainlenght <= 0) { if (IsselfCurrent) { LoadPromptWin.Instance.LoadTextPromptWindow("未装备水带或水带已用完", 1f); } return; } ParentLayHose = Instantiate(ParentLayHosePrefabs, WaterHoses.transform); ParentLayHose.name = "LayWaterHose" + creatnum; creatnum++; ParentLayHose.GetComponent().ConnectFireman = gameObject; //GetComponent().TackUpLenght = 0; //如果铺设时消防员被管线连接,给该水带赋值水源信息 if (waterHoseManage.IsConnentWaterLine) { //FireRobotWaterHoseManage thisFireman = GetComponent(); ParentLayHose.GetComponent().BindWaterSource (waterHoseManage.hasWaterConnect, waterHoseManage.hasFoamConnect, waterHoseManage.ConnentSource, waterHoseManage.WaterLineConnent); } allInits.Clear(); AddInit(); endPoint = transform.position; flag = true; } } public void SetInitPoint() { allInits.Clear(); //与之前铺设的水带切断联系 AddInit(); //将当前位置作为起点 endPoint = Vector3.zero;//transform.position; //setInitpoint = true; } public void StartSprayingLay() { if (!GetComponent().pathFindEnable) { GetComponent().pathFindEnable = true; } if (agent.enabled) { agent.SetDestination(transform.position); } if (remainlenght <= 0) { if (IsselfCurrent) { LoadPromptWin.Instance.LoadTextPromptWindow("还未装备水带", 1); } } if (endPoint != Vector3.zero && Vector3.Distance(transform.position, endPoint) < 5) { if (ParentLayHose != null && ParentLayHose.GetComponent()) { //waterHoseManage.CalculateBeforelay(ParentLayHose.GetComponent().RemainLenght, ParentLayHose.GetComponent().UseNum); flag = true; } else { ParentLayHose = Instantiate(ParentLayHosePrefabs, WaterHoses.transform); ParentLayHose.name = "LayWaterHose" + creatnum; creatnum++; ParentLayHose.GetComponent().ConnectFireman = gameObject; allInits.Clear(); //与之前铺设的水带切断联系 AddInit(); //将当前位置作为起点 endPoint = transform.position; lengthDic.Clear(); } } else { ParentLayHose = Instantiate(ParentLayHosePrefabs, WaterHoses.transform); ParentLayHose.name = "LayWaterHose" + creatnum; creatnum++; ParentLayHose.GetComponent().ConnectFireman = gameObject; allInits.Clear(); //与之前铺设的水带切断联系 AddInit(); //将当前位置作为起点 endPoint = transform.position; lengthDic.Clear(); } canreinit = false; flag = true; spraying = true; } //public void EndLay() //{ // //endlay(); // if (flag) // { // flag = false; // agent.SetDestination(transform.position); //停止移动 // SetLayHoseInfo(); // //lengthDic.Clear();//清空本次绘制存储的线 // //IsTackUp = false; // //TackHose = null; // } // if (spraying) // { // spraying = false; // DestroyTail(); // } // // Debug.Log(endPoint+"//"+transform.position); //} //void SetLayHoseInfo() //{ // // LastLenght = GetSelfConsumedLength() - ParentLayHose.GetComponent().LineLenght; // ParentLayHose.GetComponent().LineLenght = GetSelfConsumedLength(); // ParentLayHose.GetComponent().UseNum = Mathf.CeilToInt(GetSelfConsumedLength() / 20); // ParentLayHose.GetComponent().CreatFireman = gameObject; // ParentLayHose.GetComponent().RemainLenght = Mathf.CeilToInt(GetSelfConsumedLength() / 20) * 20 - GetSelfConsumedLength(); // GetComponent().UpdateRemain(GetSelfConsumedLength()); // ParentLayHose.GetComponent().ConnectWaterHose = GetComponent().WaterLineConnent; // ParentLayHose.GetComponent().EndPoint = transform.position; // ParentLayHose.GetComponent().ConnectFireman = null; // //如果结束铺设时消防员被管线连接,更新水带水源信息,防止在铺设过程中消防员连接的管线被删除,造成的信息更改 // if (GetComponent().IsConnentWaterSource) // { // FireRobotWaterHoseManage thisFireman = GetComponent(); // ParentLayHose.GetComponent().BindWaterSource(thisFireman.hasWaterConnect, thisFireman.hasFoamConnect, thisFireman.ConnentSource, thisFireman.WaterLineConnent); // } // else // { // ParentLayHose.GetComponent().BindWaterSource(false, false, null, null); // } // if (GetComponent().IsConnentWaterLine) // { // GetComponent().pathFindEnable = false; // } // else // { // GetComponent().Resert(); // } //} private void GetPath() { NavMesh.CalculatePath(InitPoint, transform.position, -1, NavMeshPath); path = NavMeshPath.corners; //path = CheckPath(path); if (pathCorners.ContainsKey(pathIndex)) { pathCorners[pathIndex] = path; } else { pathCorners.Add(pathIndex, path); } List intList = pathMap[pathIndex]; num = intList[0]; int endnum = intList[intList.Count - 1]; if (lengthDic.Count > 0 && hoseDic.Count > 0) //删除第pathIndex段水带上一次生成的所有子段 { for (int i = num; i <= endnum; i++) { lengthDic.Remove(i); } } float thispartlength = 0; //记录此次路线生成水带的长度,如果超出20,就更新InitPoint if (path.Length > 1) { for (int i = 1; i < path.Length; i++) { DrawHoseLine(path[i - 1], path[i], num); thispartlength += lengthDic[num]; num += 1; } } if (num - 1 < hoseDic.Count) //删除多余的水带段 { for (int i = hoseDic.Count; i > num - 1; i--) { if (hoseDic.ContainsKey(i)) { Destroy(hoseDic[i]); hoseDic.Remove(i); } } } //此次路线生成水带的长度如果超出20,或者就更新InitPoint //if (GetComponent().OverLenght == 0) { if (canreinit && path.Length > 1 && Vector3.Distance(transform.position, path[path.Length - 2]) > 0.5f && (thispartlength > 20)) { AddInit(); } } //if (GetComponent().OverLenght > 0) //{ // if (canreinit && path.Length > 1 && Vector3.Distance(transform.position, path[path.Length - 2]) > 0.5f && (thispartlength > GetComponent().OverLenght)) // { // AddInit(); // } //} } private void DrawByTimespan() { // waterHoseManage.WaterHoseLenghtRemain = waterHoseManage.NowRemainWaterHose * 20 - GetSelfConsumedLength();// + GetComponent().OverLenght; if (remainlenght > 0) //如果装备了水带,并且有水带可用 { HasHose(); GetBackCheck(); GetPath(); } else //没装备就原地不动 { transform.position = endPoint; if (agent.destination != transform.position) { //ResourceLoadWindow.Instance.LoadTextHintWindow("已无可用水带", 1); agent.SetDestination(endPoint); } canreinit = false; //即时超过20也不更新InitPoint GetBackCheck(); GetPath(); } // GetComponent().WaterHoseLenghtRemain-=GetConsumedNum(); } private void DrawTail() { Vector3 midPosition = (transform.Find("ConnectPos").position + shuiqiang.position) / 2; midPosition = new Vector3(midPosition.x, midPosition.y + 0.1f, midPosition.z); string linename = "waterhose-tail"; GameObject hose; if (!WaterHoses.transform.Find(linename)) { hose = Instantiate(Resources.Load("Lines/" + bandtype), midPosition, Quaternion.identity) as GameObject; hose.name = linename; } else { hose = WaterHoses.transform.Find(linename).gameObject; hose.transform.position = midPosition; } hose.transform.parent = WaterHoses.transform; hose.transform.forward = (shuiqiang.position - transform.position).normalized;//改变朝向 float distance = Vector3.Distance(transform.position, shuiqiang.position); hose.transform.localScale = new Vector3(25, 25, distance * 78);//延长线条,连接两点 } private void DestroyTail() { if (WaterHoses.transform.Find("waterhose-tail")) { Destroy(WaterHoses.transform.Find("waterhose-tail").gameObject); } } private void GetBackCheck() { if (allInits.Count > 1) { int i = allInits.Count - 1; if (pathCorners.ContainsKey(pathIndex - 1)) { Vector3[] lastPath = pathCorners[pathIndex - 1]; if (lastPath.Length > 1) { checkPoint = lastPath[lastPath.Length - 2]; //上一段路线的倒数第二点 float dc1 = Vector3.Distance(checkPoint, allInits[i]); float dcp = Vector3.Distance(checkPoint, transform.position); Vector3 _ic = checkPoint - allInits[i]; Vector3 _ip = transform.position - allInits[i]; float angle = Vector3.Angle(_ic, _ip); if (angle < 15 || dcp < dc1) { //删除最后一个InitPoint为起点画的线,即第pathIndex段水带 List intlist = pathMap[pathIndex]; foreach (int k in intlist) { if (hoseDic.ContainsKey(k)) { Destroy(hoseDic[k]); hoseDic.Remove(k); } lengthDic.Remove(k); } pathMap.Remove(pathIndex); pathCorners.Remove(pathIndex); pathIndex -= 1; InitPoint = allInits[i - 1]; //InitPoint退回上一个点 allInits.RemoveAt(i); //删除最后一个InitPoint canreinit = false; } } } } } private void AddInit() { InitPoint = transform.position; allInits.Add(InitPoint); pathIndex += 1; pathMap.Add(pathIndex, new List { num }); } public void SetHoseFloorInfo(GameObject hose, Vector3 pos2) { Vector3 adPos2 = new Vector3(pos2.x, pos2.y + 0.2f, pos2.z); Ray ray = new Ray(adPos2, -Vector3.up); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit, LayerMask.NameToLayer("PathFinding"))) { if (hit.transform.gameObject.GetComponent() && hit.transform.gameObject.GetComponent().gameObjType == CloneObjType.staticGameObject) { CloneGameObjInfo hoseinfo = hose.GetComponent(); CloneGameObjInfo hitinfo = hit.transform.gameObject.GetComponent(); hoseinfo.buildNum = hitinfo.buildNum; hoseinfo.floorNum = hitinfo.floorNum; hoseinfo.interlayerNum = hitinfo.interlayerNum; //hoseinfo.UpdateFloorEnabled(GlobalVariable.CurrentFloor); return; } } } /// /// 收起管线 /// public void PickUpLayHose() { //endPoint = Vector3.zero; //allInits.Clear(); //waterHoseManage.CalculateBeforelay(ParentLayHose.GetComponent().RemainLenght, ParentLayHose.GetComponent().UseNum); //for (int i = 0; i < WaterHoses.transform.childCount; i++) //{ // Destroy(WaterHoses.transform.GetChild(i).gameObject); //} //waterHoseManage.FireRobotPickUpLayHose(HasUseNum); HasUseNum = 0; //remainlenght = waterHoseManage.WaterHoseLenghtRemain * waterHoseManage.OneDefaultHoseLenght; ParentLayHose = null; foreach (GameObject hose in hoseDic.Values) { Destroy(hose);//删除所有已生成的水带段 } hoseDic.Clear(); //水带段字典清空 lengthDic.Clear(); //已用水带长度清空 pathMap.Clear(); num = 1; path = null; pathIndex = 0; flag = false; spraying = false; InitPoint = default(Vector3); endPoint = default(Vector3); checkPoint = default(Vector3); canreinit = true; allInits.Clear(); pathCorners.Clear(); DestroyTail(); //重置水源信息 waterHoseManage.ResetWaterSourceConnectInfo(false, false, false, null, null); } public void JugeLenght() { if (ParentLayHose != null) { if (ParentLayHose.GetComponent()) { if (GetSelfConsumedLength() < 0.2f) { foreach (GameObject hose in hoseDic.Values) { Destroy(hose);//删除所有已生成的水带段 } hoseDic.Clear(); //水带段字典清空 lengthDic.Clear(); //已用水带长度清空 pathMap.Clear(); num = 1; path = null; pathIndex = 0; flag = false; spraying = false; InitPoint = default(Vector3); endPoint = default(Vector3); checkPoint = default(Vector3); canreinit = true; allInits.Clear(); pathCorners.Clear(); Destroy(ParentLayHose); } } } } }