You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
12 KiB
293 lines
12 KiB
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<WaterSupplier>() && !hitObj.GetComponent<WaterReceiver>()) |
|
{//只是水源 |
|
|
|
if (orihitobj.GetComponent<WaterSeparaterControl>() && orihitobj != hitObj) |
|
{//如果起点终点都是水源且起点终点不是一个物体,起点是分水器,终点是纯水源,反转两端 |
|
if (orihitobj.GetComponent<WaterReceiver>().waterlinelist.Count < 1) |
|
{ |
|
obj.GetComponent<WaterLineInfo>().Resertinfo(); |
|
obj.GetComponent<WaterLineInfo>().SupplierID = hitObjID; |
|
obj.GetComponent<WaterLineInfo>().ReceiverID = orihitobj.GetComponent<CloneGameObjInfo>().gameObjID; |
|
} |
|
//else |
|
//{ |
|
// ResourceLoadWindow.Instance.LoadTextHintWindow("分水器进水口已经被占用", 2); |
|
// return; |
|
//} |
|
|
|
} |
|
else |
|
obj.GetComponent<WaterLineInfo>().SupplierID = hitObjID; |
|
//hitObj.GetComponent<WaterSupplier>().AddWaterLineInfo(waterLineID); |
|
} |
|
//如果点击的是用水设备,记录用水设备信息 |
|
else if (hitObj.GetComponent<WaterReceiver>() && !hitObj.GetComponent<WaterSupplier>()) |
|
{//只是用水方 |
|
obj.GetComponent<WaterLineInfo>().ReceiverID = hitObjID; |
|
} |
|
else if (hitObj.GetComponent<WaterReceiver>() && hitObj.GetComponent<WaterSupplier>()) |
|
{//既是水源又是用水方(分水器) |
|
if (hitObj.GetComponent<WaterSeparaterControl>()) |
|
{ |
|
long watersupplierid = obj.GetComponent<WaterLineInfo>().SupplierID; |
|
if (watersupplierid == -1) |
|
{//水带没有赋值供水方 |
|
|
|
if (hitObj.GetComponent<WaterSupplier>().waterLineInfos.Count < 2) |
|
obj.GetComponent<WaterLineInfo>().SupplierID = hitObjID; |
|
} |
|
else if(watersupplierid != -1) |
|
{ |
|
if (hitObj.GetComponent<WaterReceiver>().waterlinelist.Count < 1) |
|
obj.GetComponent<WaterLineInfo>().ReceiverID = hitObjID; |
|
} |
|
} |
|
|
|
} |
|
} |
|
protected override void Clone() |
|
{ |
|
|
|
var hitPoint = data.hitPos; |
|
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); |
|
if ((!hitObj.GetComponent<TruckMessages>() && !hitObj.GetComponent<WaterSeparaterControl>()) |
|
&& hitObj.GetComponent<WaterReceiver>() && hitObj.GetComponent<WaterReceiver>().waterlinelist.Count > 0) |
|
{ |
|
ResourceLoadWindow.Instance.LoadTextHintWindow("该物体不能连接更多管线", 2); |
|
return; |
|
} |
|
//判断是分水器时的连接数量 |
|
if (hitObj.GetComponent<WaterSeparaterControl>()) |
|
{ |
|
if (hitObj.GetComponent<WaterSupplier>().waterLineInfos.Count > 1) |
|
{ |
|
if (hitObj.GetComponent<WaterReceiver>().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<CloneGameObjInfo>(); |
|
objMsg.gameObjType = cloneObjType; |
|
if (hitObj.GetComponent<CloneGameObjInfo>()) |
|
{ |
|
CloneGameObjInfo floorMsg = hitObj.GetComponent<CloneGameObjInfo>(); |
|
objMsg.buildNum = floorMsg.buildNum; |
|
objMsg.floorNum = floorMsg.floorNum; |
|
objMsg.interlayerNum = floorMsg.interlayerNum; |
|
} |
|
|
|
SetCloneGameObject(parentObj); |
|
nowObj = parentObj; |
|
AddRecordEventCloneParent(parentObj.transform);//记录克隆父物体 |
|
} |
|
else |
|
{ |
|
if (orihitobj.GetComponent<WaterSeparaterControl>()) |
|
{//如果前后两次点击的都是二分水器 |
|
if (hitObj.GetComponent<WaterSeparaterControl>()) |
|
{ |
|
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<WaterSupplier>()) |
|
{ |
|
return hitObj.GetComponent<WaterSupplier>().ConnectPos; |
|
} |
|
else if (hitObj.GetComponent<WaterReceiver>()) |
|
{ |
|
return hitObj.GetComponent<WaterReceiver>().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<CloneGameObjInfo>(); |
|
objMsg.gameObjID = nowObj.GetComponent<CloneGameObjInfo>().gameObjID; |
|
objMsg.gameObjType = cloneObjType; |
|
|
|
//设置克隆物体所在楼层属性,属性从点击的对象上获取 |
|
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); |
|
checkConnection(nowObj, hitObj, data.gameObjID, objMsg.gameObjID); |
|
CloneGameObjInfo floorMsg = hitObj.GetComponent<CloneGameObjInfo>(); |
|
objMsg.buildNum = floorMsg.buildNum; |
|
objMsg.floorNum = floorMsg.floorNum; |
|
objMsg.interlayerNum = floorMsg.interlayerNum; |
|
|
|
SelectedObjs.gameObjs.Add(clonedObj); |
|
// SetCloneGameObject(clonedObj); |
|
clonedObj.AddComponent<ObjSelectCtrl>(); |
|
AddRecordEventCloneChild(clonedObj.transform); //记录克隆子物体 |
|
} |
|
/// <summary> |
|
/// 克隆记录父物体 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
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<WaterLineInfo>(); |
|
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<WaterLineInfo>(); |
|
data.supplierID = info.SupplierID; |
|
data.receiverID = info.ReceiverID; |
|
//保存Json |
|
string json = JsonUtility.ToJson(data); |
|
eventData.json = json; |
|
RecordManager.Instance.jsonData.eventDataList.Add(eventData); |
|
} |
|
} |
|
/// <summary> |
|
/// 添加初始状态记录 |
|
/// </summary> |
|
/// <param name="list"></param> |
|
public override void AddRecordFrame(List<ObjectData> 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); |
|
} |
|
} |
|
/// <summary> |
|
/// 添加初始状态记录时填充数据 |
|
/// </summary> |
|
/// <param name="data"></param> |
|
/// <param name="obj"></param> |
|
public void SetWaterLineData(RecordWaterLine data, Transform obj) |
|
{ |
|
var msg = obj.GetComponent<CloneGameObjInfo>(); |
|
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<WaterLineInfo>(); |
|
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<CloneGameObjInfo>(); |
|
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<ObjSelectCtrl>(); |
|
obj.AddComponent<ObjDelete>(); |
|
} |
|
}
|
|
|