using AX.InputSystem; using AX.MessageSystem; using AX.NetworkSystem; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneWarningLine : CloneBase { [SerializeField] [Tooltip("距离地面距离")] public string PrefabPath; protected float Height = 0; private Vector3 startPoint; private Vector3 endPoint; // private IMessage imessageobj; private Vector3 lineBeginPoint; private List pathPoints = new List(); /// /// 克隆出的线的父物体预设 /// private GameObject parent; /// /// 画线是否超过俩个点 /// public bool PointOverTwo; /// /// 线的父物体 /// private GameObject parentgameobj; public Vector3 SizeVec; void Start() { if (parent == null) { parent = Resources.Load("LineParent/ParentWarnLine") as GameObject; } //TrggerCloneToggle.GetComponent().OutInterFaceToggle = TrggerCloneToggleChange; } public override void OnEnable() { base.OnEnable(); MessageDispatcher.AddListener("CANCEL_CLONEBTN_SELECTED_COMMAND", CancelSelected); } private void CancelSelected(IMessage obj) { if (InputManager.cloneObjType == cloneObjType) { ResertLine(); } } public override void OnDisable() { base.OnDisable(); MessageDispatcher.RemoveListener("CANCEL_CLONEBTN_SELECTED_COMMAND", CancelSelected); } public override void OnDestroy() { base.OnDestroy(); MessageDispatcher.RemoveListener("CANCEL_CLONEBTN_SELECTED_COMMAND", CancelSelected); } public override void Execute(IMessage obj) { var gameObjID = (long)obj.Sender; var data = ((CloneCmdArgs)obj.Data); if (data.cloneObjType == cloneObjType) { TwoOrMore(obj); var hitPoint = data.hitPos; if (startPoint == Vector3.zero) { lineBeginPoint = hitPoint; startPoint = hitPoint; return; } endPoint = hitPoint; pathPoints.Add(endPoint); float distance = Vector3.Distance(startPoint, endPoint);//计算两点的距离 if (distance < 2) { pathPoints.Remove(endPoint); LoadPromptWin.Instance.LoadTextPromptWindow("两点重合", 1f); return; } Vector3 clonedObjPos = (startPoint + endPoint) / 2; //EntitiesManager.Instance.AddEntity(gameObjID, parentgameobj); clonedObjPos = new Vector3(clonedObjPos.x, clonedObjPos.y + Height, clonedObjPos.z); GameObject line = Instantiate(clonePrefab, parentgameobj.transform); line.name = "line"; line.transform.position = clonedObjPos; line.transform.forward = (-(endPoint - startPoint)).normalized;//改变线条的朝向 line.transform.localScale = new Vector3(SizeVec.x, SizeVec.y, distance * SizeVec.z);//延长线条,连接两点。 line.AddComponent(); line.GetComponent().gameObjID = parentgameobj.GetComponent().gameObjID; //parentgameobj.GetComponent().LineLenght += distance; //parentgameobj.GetComponent().StartHitGameObj = startHitGameobj; //parentgameobj.GetComponent().EndHitGameObj = endHitGameobj; //parentgameobj.name = CloneObjNameTool.Instance().GetCloneObjectName(cloneObjType); //SelectedObjs.gameObjs.Add(line); SelectedObjs.gameObjs.Add(line); startPoint = endPoint; Setattribute(data.gameObjID, parentgameobj); cloneInfoSync(hitPoint); } } public GameObject CreatLine(Vector3 startPoint, Vector3 endPoint) { float distance = Vector3.Distance(startPoint, endPoint);//计算两点的距离 if (distance < 2) { pathPoints.Remove(endPoint); LoadPromptWin.Instance.LoadTextPromptWindow("两点重合", 1f); return null; } //parentgameobj.GetComponent().LineLenght += distance; Vector3 clonedObjPos = (startPoint + endPoint) / 2; clonedObjPos = new Vector3(clonedObjPos.x, clonedObjPos.y + Height, clonedObjPos.z); return Instantiate(clonePrefab); } //设置克隆物体所在楼层等基本属性,属性从点击的对象上获取 public void Setattribute(long HitID, GameObject Clone) { var hitObj = EntitiesManager.Instance.GetEntityByID(HitID); CloneGameObjInfo hitObjInfo = hitObj.GetComponent(); CloneGameObjInfo cloneObjInfo = Clone.GetComponent(); cloneObjInfo.gameObjType = cloneObjType; cloneObjInfo.UserID = CurrentUserInfo.mySelf.Id; cloneObjInfo.buildNum = hitObjInfo.buildNum; cloneObjInfo.floorNum = hitObjInfo.floorNum; cloneObjInfo.interlayerNum = hitObjInfo.interlayerNum; } public void CreatParents(long id) { parentgameobj = EntitiesManager.Instance.CreateObj(parent, transform.position, transform , id);//GameObject.Instantiate(parent, transform); Debug.Log(EntitiesManager.Instance.GetEntityByID(id)); parentgameobj.GetComponent().gameObjType = cloneObjType; parentgameobj.transform.parent = transform; parentgameobj.name = cloneObjType.ToString(); } /// /// 是多点创建一个父物体,不是多点,每2点创建一个父物体。 /// /// public void TwoOrMore(IMessage imessageobj) { if (PointOverTwo) { if (parentgameobj != null) { return; } CreatParents((long)imessageobj.Sender); } else { if (endPoint == Vector3.zero && startPoint != Vector3.zero) { CreatParents((long)imessageobj.Sender); } if (pathPoints.Count > 0) { pathPoints.Clear(); startPoint = Vector3.zero; endPoint = Vector3.zero; return; } } } public void ResertLine() { ////结束克隆警戒线的同步 //var argsync = new WarnLineSyncDate(); //argsync.SendUserID = CurrentUserInfo.mySelf.Id; //argsync.IsEnd = true; //argsync.parentUID = GetComponent().Id; //argsync.name = parentgameobj.name; //argsync.gameObjID = parentgameobj.GetComponent().gameObjID; //argsync.gameObjType = parentgameobj.GetComponent().gameObjType; //argsync.UserID = parentgameobj.GetComponent().UserID; //argsync.buildNum = parentgameobj.GetComponent().buildNum; //argsync.floorNum = parentgameobj.GetComponent().floorNum; //argsync.interlayerNum = parentgameobj.GetComponent().interlayerNum; //NetworkManager.Default.SendAsync(/*CurrentUserInfo.mySelf.Id,*/ "WARN_LINE_SYNC", argsync); lineBeginPoint = Vector3.zero; startPoint = Vector3.zero; endPoint = Vector3.zero; // StartHitGameObj = null; //EndHitGameObj = null; //LineLenght = 0; parentgameobj = null; //cloneObjType = CloneObjType.None; } private void cloneInfoSync(Vector3 hitpoint) { var argsync = new WarnLineSyncDate(); argsync.SendUserID = CurrentUserInfo.mySelf.Id; //argsync.IsEnd = false; argsync.LineBeginPoint = lineBeginPoint; // argsync.parentUID = GetComponent().Id; //argsync.PrefabsPath = PrefabPath; argsync.hitpoint = hitpoint; argsync.name = parentgameobj.name; argsync.gameObjID = parentgameobj.GetComponent().gameObjID; argsync.gameObjType = parentgameobj.GetComponent().gameObjType; argsync.UserID = parentgameobj.GetComponent().UserID; // argsync.baseGameobjInfo.gameObjType = parentgameobj.GetComponent().gameObjType; //argsync.baseGameobjInfo.UserID = parentgameobj.GetComponent().UserID; argsync.buildNum = parentgameobj.GetComponent().buildNum; argsync.floorNum = parentgameobj.GetComponent().floorNum; argsync.interlayerNum = parentgameobj.GetComponent().interlayerNum; NetworkManager.Default.SendAsync(/*CurrentUserInfo.mySelf.Id,*/ "WARN_LINE_SYNC", argsync); } }