using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneWaterLine : CloneMultiLine { GameObject orihitobj = null; public override void Awake() { variableX = 5; variableY = 18; variableZ = 5; cloneObjType = CloneObjType.WaterLine; parentResourcesName = "Prefab/Tool/WaterLineParent"; ResourcesName = "Prefab/Tool/WaterLineItem"; base.Awake(); } private void checkConnection(GameObject obj, GameObject hitObj, long hitObjID, long waterLineID) { //如果点击的是水源,记录水源信息 if (hitObj.GetComponent() && !hitObj.GetComponent()) {//只是水源 if (orihitobj.GetComponent() && orihitobj != hitObj) {//如果起点终点都是水源且起点终点不是一个物体,起点是分水器,终点是纯水源,反转两端 if (orihitobj.GetComponent().waterlinelist.Count < 1) { obj.GetComponent().Resertinfo(); obj.GetComponent().SupplierID = hitObjID; obj.GetComponent().ReceiverID = orihitobj.GetComponent().gameObjID; } //else //{ // ResourceLoadWindow.Instance.LoadTextHintWindow("分水器进水口已经被占用", 2); // return; //} } else obj.GetComponent().SupplierID = hitObjID; //hitObj.GetComponent().AddWaterLineInfo(waterLineID); } //如果点击的是用水设备,记录用水设备信息 else if (hitObj.GetComponent() && !hitObj.GetComponent()) {//只是用水方 obj.GetComponent().ReceiverID = hitObjID; } else if (hitObj.GetComponent() && hitObj.GetComponent()) {//既是水源又是用水方(分水器) if (hitObj.GetComponent()) { long watersupplierid = obj.GetComponent().SupplierID; if (watersupplierid == -1) {//水带没有赋值供水方 if (hitObj.GetComponent().waterLineInfos.Count < 2) obj.GetComponent().SupplierID = hitObjID; } else if(watersupplierid != -1) { if (hitObj.GetComponent().waterlinelist.Count < 1) obj.GetComponent().ReceiverID = hitObjID; } } } } protected override void Clone() { var hitPoint = data.hitPos; var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); if ((!hitObj.GetComponent() && !hitObj.GetComponent()) && hitObj.GetComponent() && hitObj.GetComponent().waterlinelist.Count > 0) { ResourceLoadWindow.Instance.LoadTextHintWindow("该物体不能连接更多管线", 2); return; } //判断是分水器时的连接数量 if (hitObj.GetComponent()) { if (hitObj.GetComponent().waterLineInfos.Count > 1) { if (hitObj.GetComponent().waterlinelist.Count > 0) { ResourceLoadWindow.Instance.LoadTextHintWindow("该物体不能再连接更多管线", 2); return; } } } hitPoint = GetConnectPos(hitPoint, hitObj); if (!started) { orihitobj = hitObj; prePos = hitPoint; started = true; //克隆父物体 var parentObj = EntitiesManager.Instance.CreateObj(parentPrefab, prePos, transform, gameObjID); parentObj.name = gameObjID.ToString(); SelectedObjs.gameObjs.Add(parentObj); //设置克隆物体所在楼层属性,属性从点击的对象上获取 checkConnection(parentObj, hitObj, data.gameObjID, gameObjID); CloneGameObjInfo objMsg = parentObj.GetComponent(); objMsg.gameObjType = cloneObjType; if (hitObj.GetComponent()) { CloneGameObjInfo floorMsg = hitObj.GetComponent(); objMsg.buildNum = floorMsg.buildNum; objMsg.floorNum = floorMsg.floorNum; objMsg.interlayerNum = floorMsg.interlayerNum; } SetCloneGameObject(parentObj); nowObj = parentObj; AddRecordEventCloneParent(parentObj.transform);//记录克隆父物体 } else { if (orihitobj.GetComponent()) {//如果前后两次点击的都是二分水器 if (hitObj.GetComponent()) { ResourceLoadWindow.Instance.LoadTextHintWindow("不能连接两个分水器", 2); return; } } curPos = hitPoint; //如果两点不重合 if (curPos.x != prePos.x && curPos.x != prePos.x) { //克隆子物体 DrawLine(prePos, curPos, nowObj); } prePos = curPos; } } public Vector3 GetConnectPos(Vector3 hitPoint, GameObject hitObj) { if (hitObj.GetComponent()) { return hitObj.GetComponent().ConnectPos; } else if (hitObj.GetComponent()) { return hitObj.GetComponent().ConnectPos; } else { return hitPoint; } } protected override void DrawLine(Vector3 prePos, Vector3 curPos, GameObject nowObj) { midPos = (prePos + curPos) / 2; Vector3 clonedObjPos = new Vector3(midPos.x, midPos.y + Height, midPos.z); var clonedObj = Instantiate(clonePrefab, clonedObjPos, Quaternion.identity) as GameObject; clonedObj.transform.parent = nowObj.transform; clonedObj.name = nowObj.name; SetClonedObj(clonedObj); CloneGameObjInfo objMsg = clonedObj.GetComponent(); objMsg.gameObjID = nowObj.GetComponent().gameObjID; objMsg.gameObjType = cloneObjType; //设置克隆物体所在楼层属性,属性从点击的对象上获取 var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); checkConnection(nowObj, hitObj, data.gameObjID, objMsg.gameObjID); CloneGameObjInfo floorMsg = hitObj.GetComponent(); objMsg.buildNum = floorMsg.buildNum; objMsg.floorNum = floorMsg.floorNum; objMsg.interlayerNum = floorMsg.interlayerNum; SelectedObjs.gameObjs.Add(clonedObj); // SetCloneGameObject(clonedObj); clonedObj.AddComponent(); AddRecordEventCloneChild(clonedObj.transform); //记录克隆子物体 } /// /// 克隆记录父物体 /// /// public override void AddRecordEventCloneParent(Transform obj) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = cloneObjType; eventData.eventType = RecordEventType.Clone; var data = new RecordWaterLine(); SetBaseData(data, obj); //记录水源连接信息 var info = obj.GetComponent(); data.supplierID = info.SupplierID; data.receiverID = info.ReceiverID; //保存Json string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } public override void AddRecordEventCloneChild(Transform obj) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = cloneObjType; eventData.eventType = RecordEventType.Clone; var data = new RecordWaterLine(); SetBaseData(data, obj); data.isChild = true; //记录是否为子物体 data.isEvent = true; //记录水源连接信息 var info = obj.parent.GetComponent(); data.supplierID = info.SupplierID; data.receiverID = info.ReceiverID; //保存Json string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } /// /// 添加初始状态记录 /// /// public override void AddRecordFrame(List list) { foreach (Transform child in transform) { var data = new RecordWaterLine(); SetWaterLineData(data, child); data.isEvent = false; string json = JsonUtility.ToJson(data); var objectJson = new ObjectData(); objectJson.cloneObjType = cloneObjType; objectJson.json = json; list.Add(objectJson); } } /// /// 添加初始状态记录时填充数据 /// /// /// public void SetWaterLineData(RecordWaterLine data, Transform obj) { var msg = obj.GetComponent(); data.gameObjType = msg.gameObjType; data.objectName = obj.gameObject.name; data.buildNum = msg.buildNum; data.floorNum = msg.floorNum; data.interlayerNum = msg.interlayerNum; data.myTransform.setMyPosition(obj.localPosition); data.myTransform.setMyRotation(obj.localRotation); data.myTransform.setMyScale(obj.localScale); data.isActive = obj.gameObject.activeSelf; data.isEvent = false; //记录水源连接信息 var info = obj.GetComponent(); data.supplierID = info.SupplierID; data.receiverID = info.ReceiverID; //生成子物体 for (int i = 0; i < obj.childCount; i++) { RecordObjectBase childData = new RecordObjectBase(); Transform childTransform = obj.GetChild(i); CloneGameObjInfo childMsg = childTransform.GetComponent(); childData.gameObjType = childMsg.gameObjType; childData.objectName = childTransform.gameObject.name; childData.buildNum = childMsg.buildNum; childData.floorNum = childMsg.floorNum; childData.interlayerNum = childMsg.interlayerNum; childData.myTransform.setMyPosition(childTransform.localPosition); childData.myTransform.setMyRotation(childTransform.localRotation); childData.myTransform.setMyScale(childTransform.localScale); data.list.Add(childData); } } public override void SetCloneGameObject(GameObject obj) { obj.AddComponent(); obj.AddComponent(); } }