海淀天下城电子沙盘单机版
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.
 
 
 
 

245 lines
9.8 KiB

using AX.InputSystem;
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CloneDashedLine : CloneSingleObj {
public static bool started = false;
public static GameObject nowObj;
public Vector3 prePos;
public Vector3 curPos;
public GameObject parentPrefab;
public string parentResourcesName;
public override void Awake()
{
if (parentPrefab == null)
{
parentPrefab = Resources.Load(parentResourcesName) as GameObject;
}
base.Awake();
}
public override void Execute(IMessage obj)
{
gameObjID = (long)obj.Sender;
data = ((CloneCmdArgs)obj.Data);
if (data.cloneObjType == cloneObjType)
{
Clone();
}
}
public void CreateParentPrefab()
{
parentPrefab = new GameObject(cloneObjType.ToString());
parentPrefab.AddComponent<BoxCollider>();
parentPrefab.AddComponent<CloneGameObjInfo>();
}
protected override void Clone()
{
var hitPoint = data.hitPos;
if (!started)
{
prePos = hitPoint;
started = true;
//克隆父物体
var clonedObj = EntitiesManager.Instance.CreateObj(parentPrefab, prePos, transform, gameObjID);
clonedObj.name = gameObjID.ToString();
SelectedObjs.gameObjs.Add(clonedObj);
CloneGameObjInfo objMsg = clonedObj.GetComponent<CloneGameObjInfo>();
objMsg.gameObjType = cloneObjType;
//设置克隆物体所在楼层属性,属性从点击的对象上获取
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID);
CloneGameObjInfo floorMsg = hitObj.GetComponent<CloneGameObjInfo>();
objMsg.buildNum = floorMsg.buildNum;
objMsg.floorNum = floorMsg.floorNum;
objMsg.interlayerNum = floorMsg.interlayerNum;
SetCloneGameObject(clonedObj);
nowObj = clonedObj;
AddRecordEventCloneParent(clonedObj.transform);//记录克隆父物体
}
else
{
curPos = hitPoint;
//如果两点不重合
if (curPos.x != prePos.x && curPos.x != prePos.x)
{
DrawLine(prePos, curPos, nowObj);
}
prePos = curPos;
}
}
protected virtual void DrawLine(Vector3 prePos, Vector3 curPos, GameObject nowObj)
{
Vector3 vertPos;
var Distance = Vector3.Distance(prePos, curPos);
float pos = Mathf.CeilToInt(Distance);
float count = Mathf.CeilToInt(pos / 3);
int dex = 1;
if (count < 1)
{
count = 1;
dex = 0;
}
for (int i = 0; i < count + dex; i++)
{
if (count > 1)
{
if (i != 0)
{
vertPos = new Vector3(prePos.x + (i / (float)count) * (curPos.x - prePos.x), prePos.y + (i / (float)count) * (curPos.y - prePos.y), prePos.z + (i / (float)count) * (curPos.z - prePos.z));
GameObject cloneObj = Instantiate(clonePrefab, vertPos, Quaternion.identity) as GameObject;
cloneObj.name = nowObj.name;
cloneObj.transform.parent = nowObj.transform;
//cloneObj.transform.forward = ((curPos - prePos)).normalized;
SetDirect(cloneObj);
//设定id为父物体id
CloneGameObjInfo objMsg = cloneObj.GetComponent<CloneGameObjInfo>();
objMsg.gameObjID = nowObj.GetComponent<CloneGameObjInfo>().gameObjID;
objMsg.gameObjType = cloneObjType;
//设置克隆物体所在楼层属性,属性从点击的对象上获取
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID);
CloneGameObjInfo floorMsg = hitObj.GetComponent<CloneGameObjInfo>();
objMsg.buildNum = floorMsg.buildNum;
objMsg.floorNum = floorMsg.floorNum;
objMsg.interlayerNum = floorMsg.interlayerNum;
SelectedObjs.gameObjs.Add(cloneObj);
SetCloneGameObject(cloneObj);
cloneObj.AddComponent<ArrowScaleControl>(); //添加控制箭头大小
AddRecordEventCloneChild(cloneObj.transform); //记录克隆子物体
}
}
else
{
GameObject cloneObj = Instantiate(clonePrefab, curPos, Quaternion.identity) as GameObject;
cloneObj.name = nowObj.name;
cloneObj.transform.parent = nowObj.transform;
cloneObj.transform.forward = ((curPos - prePos)).normalized;
//设定id为父物体id
CloneGameObjInfo objMsg = cloneObj.GetComponent<CloneGameObjInfo>();
objMsg.gameObjID = nowObj.GetComponent<CloneGameObjInfo>().gameObjID;
objMsg.gameObjType = cloneObjType;
//设置克隆物体所在楼层属性,属性从点击的对象上获取
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID);
CloneGameObjInfo floorMsg = hitObj.GetComponent<CloneGameObjInfo>();
objMsg.buildNum = floorMsg.buildNum;
objMsg.floorNum = floorMsg.floorNum;
objMsg.interlayerNum = floorMsg.interlayerNum;
SelectedObjs.gameObjs.Add(cloneObj);
SetCloneGameObject(cloneObj);
cloneObj.AddComponent<ArrowScaleControl>(); //添加控制箭头大小
AddRecordEventCloneChild(cloneObj.transform); //记录克隆子物体
}
}
}
protected virtual void SetDirect(GameObject cloneObj)
{
cloneObj.transform.forward = ((curPos - prePos)).normalized;
}
/// <summary>
/// 添加初始状态记录
/// </summary>
/// <param name="list"></param>
public override void AddRecordFrame(List<ObjectData> list)
{
foreach (Transform child in transform)
{
var data = new RecordDashedLine();
//SetBaseData(data, child);
SetDashedLineData(data, child);
string json = JsonUtility.ToJson(data);
var objectJson = new ObjectData();
objectJson.cloneObjType = cloneObjType;
objectJson.json = json;
list.Add(objectJson);
}
}
public void SetDashedLineData(RecordDashedLine 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;
//生成子物体
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);
}
}
/// <summary>
/// 克隆记录父物体
/// </summary>
/// <param name="transform"></param>
private 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 RecordDashedLine();
SetBaseData(data, obj);
data.isChild = false;
data.isEvent = true;
string json = JsonUtility.ToJson(data);
eventData.json = json;
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
/// <summary>
/// 克隆记录子物体
/// </summary>
/// <param name="obj"></param>
public virtual 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 RecordDashedLine();
SetBaseData(data, obj);
data.isChild = true; //记录是否为子物体
data.isEvent = true;
string json = JsonUtility.ToJson(data);
eventData.json = json;
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
public override void SetCloneGameObject(GameObject obj)
{
obj.AddComponent<ObjSelectCtrl>();
obj.AddComponent<ObjDelete>();
}
}