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.
314 lines
14 KiB
314 lines
14 KiB
1 year ago
|
using System;
|
||
|
using UniRx;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class PowerManager : MonoSingleton<PowerManager>
|
||
|
{
|
||
|
//public GameObject PowerModel;
|
||
|
public CreationMode CreateMode;
|
||
|
public string powerName;//实例化物体名称
|
||
|
public LayerMask layerMask = -1;
|
||
|
public bool isEnabled;
|
||
|
public bool isCreate;
|
||
|
public bool isWarningLine;
|
||
|
public bool isHideName;
|
||
|
private GameObject PowerGroup;
|
||
|
private Vector3 Pos1;//第一碰撞点
|
||
|
private Vector3 Pos2;//第二碰撞点
|
||
|
public TaskType taskType;
|
||
|
|
||
|
public DisposalPlanMode PlanMode;
|
||
|
public Transform NodeParent;
|
||
|
public string PlanName;
|
||
|
public PlanController planController;
|
||
|
public PowerButtonType powerButtonType;
|
||
|
|
||
|
//快捷键
|
||
|
//功能键
|
||
|
public KeyCode fn = KeyCode.LeftShift;
|
||
|
//主臂上仰
|
||
|
public KeyCode up = KeyCode.W;
|
||
|
//主臂下俯
|
||
|
public KeyCode down = KeyCode.S;
|
||
|
//转台左转
|
||
|
public KeyCode leftR = KeyCode.A;
|
||
|
//转台右转
|
||
|
public KeyCode rightR = KeyCode.D;
|
||
|
//水炮(枪)上扬
|
||
|
public KeyCode waterGunUp = KeyCode.U;
|
||
|
//水炮(枪)下俯
|
||
|
public KeyCode waterGunDown = KeyCode.J;
|
||
|
//水枪射程增加
|
||
|
public KeyCode hoseIncrease = KeyCode.Equals;
|
||
|
//水枪射程缩短
|
||
|
public KeyCode hoseReduce = KeyCode.Minus;
|
||
|
//重置
|
||
|
public KeyCode reset = KeyCode.R;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
Hide();
|
||
|
|
||
|
Observable.EveryUpdate()
|
||
|
.Subscribe(_ => InstantiatePower());
|
||
|
Observable.EveryLateUpdate()
|
||
|
.Subscribe(_ =>
|
||
|
{
|
||
|
if(MainMenu.Instance.MenuMode == MainMenuMode.态势标绘)
|
||
|
{
|
||
|
if (Input.GetKey(KeyCode.LeftArrow)) { LeftRotate(); }
|
||
|
if (Input.GetKey(KeyCode.RightArrow)) { RightRotate(); }
|
||
|
if (Input.GetKey(KeyCode.Delete)) { PowerDelete(); }
|
||
|
if (Input.GetKeyDown(KeyCode.F)) { PowerFocus(); }
|
||
|
}
|
||
|
|
||
|
}).AddTo(gameObject);
|
||
|
|
||
|
}
|
||
|
|
||
|
public void Show()
|
||
|
{
|
||
|
Instance.gameObject.SetActive(true);
|
||
|
}
|
||
|
public void Hide()
|
||
|
{
|
||
|
Instance.gameObject.SetActive(false);
|
||
|
}
|
||
|
|
||
|
public void Initialization()
|
||
|
{
|
||
|
switch (CreateMode)
|
||
|
{
|
||
|
case CreationMode.Single:
|
||
|
isCreate = true;
|
||
|
isWarningLine = false;
|
||
|
break;
|
||
|
case CreationMode.Multipoint:
|
||
|
isCreate = false;
|
||
|
isWarningLine = false;
|
||
|
break;
|
||
|
case CreationMode.Pipeline:
|
||
|
isCreate = false;
|
||
|
if (powerName == "JJX") isWarningLine = true;
|
||
|
else isWarningLine = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private async void InstantiatePower()
|
||
|
{
|
||
|
if (isEnabled)
|
||
|
{
|
||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
|
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
|
||
|
{
|
||
|
if (!EventSystem.current.IsPointerOverGameObject())
|
||
|
{
|
||
|
if (Input.GetMouseButtonDown(0))
|
||
|
{
|
||
|
switch (CreateMode)
|
||
|
{
|
||
|
case CreationMode.Effact:
|
||
|
if (hit.collider.GetComponent<InstantiateData>())
|
||
|
{
|
||
|
if (hit.collider.GetComponent<InstantiateData>().Power)
|
||
|
{
|
||
|
var id = hit.collider.GetComponent<InstantiateData>().floorId;
|
||
|
Pos1 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
var prefab= AssetManager.Instance.powerEffacts.Find(go => go.name == powerName);
|
||
|
GameObject power = Instantiate(prefab, Pos1, Quaternion.identity) as GameObject;
|
||
|
power.transform.parent = transform.Find("Powers" + id);
|
||
|
power.name = $"{powerName}{AX.Network.Common.GUID.NewGuid()}";
|
||
|
//PowerSelectionManager.Sets.Add(power);
|
||
|
SelectionManager.Instance.Sets.Add(power);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
break;
|
||
|
case CreationMode.Single:
|
||
|
if (isCreate)
|
||
|
{
|
||
|
if (hit.collider.GetComponent<InstantiateData>())
|
||
|
{
|
||
|
if (hit.collider.GetComponent<InstantiateData>().Power)
|
||
|
{
|
||
|
var id = hit.collider.GetComponent<InstantiateData>().floorId;
|
||
|
Pos1 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
//GameObject power = Instantiate(AssetManager.Instance.Powers.Find(p => p.name == powerName), Pos1, Quaternion.identity) as GameObject;
|
||
|
var p = transform.Find("Powers" + id);
|
||
|
var power = await PowerPool.Instance.GetPower(powerName, Pos1, Quaternion.identity, p);
|
||
|
power.name = $"{powerName}{AX.Network.Common.GUID.NewGuid()}";
|
||
|
//PowerSelectionManager.Sets.Add(power);
|
||
|
SelectionManager.Instance.Sets.Add(power);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case CreationMode.Multipoint:
|
||
|
if (hit.collider.GetComponent<InstantiateData>())
|
||
|
{
|
||
|
var id = hit.collider.GetComponent<InstantiateData>().floorId;
|
||
|
if (hit.collider.GetComponent<InstantiateData>().Power)
|
||
|
{
|
||
|
if (!isCreate)
|
||
|
{
|
||
|
Pos1 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
isCreate = true;
|
||
|
PowerGroup = new GameObject();
|
||
|
PowerGroup.transform.parent = transform.Find("Powers" + id);
|
||
|
PowerGroup.name = $"{"Powers"}{id}{"|Group"}{powerName}{AX.Network.Common.GUID.NewGuid()}";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Vector3 vertPos;
|
||
|
Pos2 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
var ABdistance = Vector3.Distance(Pos1, Pos2);
|
||
|
int count = Mathf.FloorToInt(ABdistance / 2);
|
||
|
if (count < 1) count = 1;
|
||
|
for (int i = 0; i < count; i++)
|
||
|
{
|
||
|
vertPos = new Vector3(Pos1.x + (i / (float)count) * (Pos2.x - Pos1.x), Pos1.y + (i / (float)count) * (Pos2.y - Pos1.y), Pos1.z + (i / (float)count) * (Pos2.z - Pos1.z));
|
||
|
//GameObject power = Instantiate(AssetManager.Instance.Powers.Find(p => p.name == powerName), vertPos, Quaternion.identity) as GameObject;
|
||
|
var power = await PowerPool.Instance.GetPower(powerName, vertPos, Quaternion.identity, PowerGroup.transform);
|
||
|
power.name = $"{powerName}{AX.Network.Common.GUID.NewGuid()}";
|
||
|
power.transform.forward = (-(Pos2 - Pos1)).normalized;
|
||
|
//PowerSelectionManager.Sets.Add(power);
|
||
|
SelectionManager.Instance.Sets.Add(power);
|
||
|
}
|
||
|
Pos1 = Pos2;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case CreationMode.Pipeline:
|
||
|
Transform parent = null;
|
||
|
if (hit.collider.GetComponent<InstantiateData>())
|
||
|
{
|
||
|
if (hit.collider.GetComponent<InstantiateData>().Power)
|
||
|
parent = transform.Find("Powers" + hit.collider.GetComponent<InstantiateData>().floorId);
|
||
|
}
|
||
|
if (hit.collider.CompareTag("power"))
|
||
|
{
|
||
|
parent = hit.collider.transform.parent;
|
||
|
}
|
||
|
if (hit.collider.CompareTag("Equipment"))
|
||
|
{
|
||
|
var EquipmentParent = hit.collider.transform.parent;
|
||
|
var num = EquipmentParent.name.Substring(10);
|
||
|
parent = transform.Find("Powers" + num);
|
||
|
}
|
||
|
if (parent != null)
|
||
|
{
|
||
|
if (!isCreate)
|
||
|
{
|
||
|
Pos1 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
isCreate = true;
|
||
|
PowerGroup = new GameObject();
|
||
|
PowerGroup.transform.parent = parent;
|
||
|
PowerGroup.name = $"{parent.name}{"|Group"}{powerName}{AX.Network.Common.GUID.NewGuid()}";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Pos2 = new Vector3(hit.point.x, hit.point.y, hit.point.z);
|
||
|
Vector3 tempPos = (Pos1 + Pos2) / 2;
|
||
|
//GameObject power = Instantiate(AssetManager.Instance.Powers.Find(p => p.name == powerName), tempPos, Quaternion.identity) as GameObject;
|
||
|
var power = await PowerPool.Instance.GetPower(powerName, tempPos, Quaternion.identity, PowerGroup.transform);
|
||
|
power.name = $"{powerName}{Guid.NewGuid()}";
|
||
|
power.transform.forward = (-(Pos2 - Pos1)).normalized;
|
||
|
//power.transform.parent = PowerGroup.transform;
|
||
|
power.transform.up = (power.transform.position - Pos1).normalized;
|
||
|
//PowerSelectionManager.Sets.Add(power);
|
||
|
SelectionManager.Instance.Sets.Add(power);
|
||
|
float distance = Vector3.Distance(Pos1, Pos2);//计算两点的距离
|
||
|
power.transform.localScale = new Vector3(1f, distance * 2, 1f);
|
||
|
if (!isWarningLine) Pos1 = Pos2;
|
||
|
else
|
||
|
{
|
||
|
isCreate = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
private void LeftRotate()
|
||
|
{
|
||
|
var list = SelectionManager.GetSelectedObjects();
|
||
|
foreach (GameObject go in list)
|
||
|
{
|
||
|
go.transform.Rotate(0, Time.deltaTime * -60, 0, Space.World);
|
||
|
}
|
||
|
}
|
||
|
private void RightRotate()
|
||
|
{
|
||
|
var list = SelectionManager.GetSelectedObjects();
|
||
|
foreach (GameObject go in list)
|
||
|
{
|
||
|
go.transform.Rotate(0, Time.deltaTime * 60, 0, Space.World);
|
||
|
}
|
||
|
}
|
||
|
private void PowerDelete()
|
||
|
{
|
||
|
var list = SelectionManager.GetSelectedObjects();
|
||
|
foreach (GameObject go in list)
|
||
|
{
|
||
|
SelectionManager.Instance.Sets.Remove(go);
|
||
|
if (go != null)
|
||
|
{
|
||
|
if (go.GetComponent<PowerController>().PowerMode == CreationMode.Single|| go.GetComponent<PowerController>().PowerMode == CreationMode.Effact)
|
||
|
{
|
||
|
Destroy(go);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Destroy(go.transform.parent.gameObject);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 聚焦选中物体
|
||
|
/// </summary>
|
||
|
private void PowerFocus()
|
||
|
{
|
||
|
if (SelectionManager.Instance. SingleObject!= null)
|
||
|
{
|
||
|
var pos = SelectionManager.Instance.SingleObject.transform.position;
|
||
|
Camera.main.GetComponent<CameraOrbit>().target.position = new Vector3(pos.x, pos.y + 1, pos.z);
|
||
|
Camera.main.GetComponent<CameraOrbit>().distance = 5f;
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 清除全部力量
|
||
|
/// </summary>
|
||
|
public void ClearAll()
|
||
|
{
|
||
|
if(MainMenu.Instance.MenuMode == MainMenuMode.态势标绘)
|
||
|
{
|
||
|
foreach (GameObject go in SelectionManager.Instance.Sets)
|
||
|
{
|
||
|
if (go != null)
|
||
|
{
|
||
|
if (go.GetComponent<Selectable>().selectMode == SelectMode.Single)
|
||
|
Destroy(go);
|
||
|
else
|
||
|
Destroy(go.transform.parent.gameObject);
|
||
|
}
|
||
|
}
|
||
|
SelectionManager.Instance.Sets.Clear();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|