无人机攻击交互
This commit is contained in:
parent
4e53845d3f
commit
61df6df538
|
@ -14,7 +14,17 @@ public class DeviceManager : MonoSingleton<DeviceManager>
|
|||
{
|
||||
SyncCreateRoom.send2roomRequset += GetSend2roomMsg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
public Queue<string> send2roomStr = new Queue<string>();
|
||||
private void Update()
|
||||
{
|
||||
if (send2roomStr.Count > 0)
|
||||
{
|
||||
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", send2roomStr.Dequeue()));
|
||||
}
|
||||
}
|
||||
|
||||
public void AddDevice(EquipmentCommon d)
|
||||
{
|
||||
|
|
|
@ -164,17 +164,7 @@ public class EquipmentCommon : MonoBehaviour
|
|||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
public Queue<string> send2roomStr = new Queue<string>();
|
||||
private void Update()
|
||||
{
|
||||
if (send2roomStr.Count > 0)
|
||||
{
|
||||
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", send2roomStr.Dequeue()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//位置实时传送
|
||||
private string latestData = "";
|
||||
|
@ -218,15 +208,17 @@ public class EquipmentCommon : MonoBehaviour
|
|||
if (laserFireControlPlatformManger)
|
||||
laserFireControlPlatformManger.NonSelfGeneratedEmissionLaser(data);
|
||||
break;
|
||||
case "SingleDronePosition"://单个无人机位置信息传递
|
||||
case "SingleDronePosition"://无人机攻击目标锁定
|
||||
UnmannedAerialVehicleManage unmannedAerialVehicleManage = GetComponent<UnmannedAerialVehicleManage>();
|
||||
if (unmannedAerialVehicleManage)
|
||||
{
|
||||
UnmannedAerialVehicle unmannedAerialVehicle = unmannedAerialVehicleManage.unmannedAerialVehicles.Find(x => x.serialNumber == data[2]);
|
||||
if (unmannedAerialVehicle)
|
||||
for(int i=0;i< unmannedAerialVehicleManage.unmannedAerialVehicles.Count; i++)
|
||||
{
|
||||
Vector3 Pos = new Vector3(float.Parse(data[3]), float.Parse(data[4]), float.Parse(data[5]));
|
||||
unmannedAerialVehicle.AttAck(Pos);
|
||||
if (unmannedAerialVehicleManage.unmannedAerialVehicles[i])
|
||||
{
|
||||
Vector3 Pos = new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4]));
|
||||
unmannedAerialVehicleManage.unmannedAerialVehicles[i].AttAck(Pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -61,96 +61,15 @@ public class UnmannedAerialVehicle : MonoBehaviour
|
|||
/// 检测范围半径
|
||||
/// </summary>
|
||||
public float detectionRadius = 50; //
|
||||
/// <summary>
|
||||
/// 是否正在攻击目标
|
||||
/// </summary>
|
||||
public bool isEngagedTarget = false;
|
||||
|
||||
/// <summary>
|
||||
/// 爆炸预制体
|
||||
/// </summary>
|
||||
public GameObject explodePrefab;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (unmannedAerialVehicleManage && unmannedAerialVehicleManage.equipmentCommon.isPlayer && unmannedAerialVehicleManage.isStartRehearsing)
|
||||
{
|
||||
switch (unmannedAerialVehicleManage.pattern)
|
||||
{
|
||||
case Pattern.待机:
|
||||
break;
|
||||
case Pattern.警戒:
|
||||
AttackATarget();
|
||||
break;
|
||||
case Pattern.攻击:
|
||||
AttackATarget();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 攻击目标
|
||||
/// </summary>
|
||||
public void AttackATarget()
|
||||
{
|
||||
|
||||
if (!isEngagedTarget)
|
||||
{
|
||||
List<Collider> colliders = Physics.OverlapSphere(transform.position, detectionRadius).ToList(); // 检索范围内的所有碰撞体
|
||||
List<Collider> colliders1 = colliders.FindAll(x => x.transform.tag == "AttackTarget");
|
||||
if (colliders1.Count > 0)
|
||||
{
|
||||
colliders1.ForEach(x => Debug.Log(x.transform.name));
|
||||
int _number = Random.Range(0, colliders1.Count - 1);
|
||||
isEngagedTarget = true;
|
||||
Debug.Log(colliders1[_number].transform.name);
|
||||
AttAck(colliders1[_number].transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起攻击
|
||||
/// </summary>
|
||||
private void AttAck(Transform target)
|
||||
{
|
||||
Debug.Log("开始攻击目标..:" + target.name);
|
||||
Vector3 _v3 = target.position;
|
||||
SendMsg(target);
|
||||
transform.DOLookAt(_v3, 0.1f).OnComplete(() =>
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target.position);
|
||||
transform.DOMove(target.position, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
|
||||
{
|
||||
if (!target)
|
||||
{
|
||||
BeAssaulted("没有攻击到目标");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起攻击
|
||||
/// </summary>
|
||||
public void AttAck(Vector3 target)
|
||||
{
|
||||
transform.DOLookAt(target, 0.1f).OnComplete(() =>
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target);
|
||||
transform.DOMove(target, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
|
||||
{
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
|
@ -163,13 +82,6 @@ public class UnmannedAerialVehicle : MonoBehaviour
|
|||
}
|
||||
}
|
||||
|
||||
//[]
|
||||
//public void AddBeAssaulted()
|
||||
//{
|
||||
// BeAssaulted("无线电干扰");
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 被攻击
|
||||
/// </summary>
|
||||
|
@ -215,7 +127,7 @@ public class UnmannedAerialVehicle : MonoBehaviour
|
|||
string nowData = GetSyncDis();
|
||||
Debug.Log(nowData);
|
||||
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
||||
unmannedAerialVehicleManage.equipmentCommon.send2roomStr.Enqueue(nowData);
|
||||
DeviceManager.Instance.send2roomStr.Enqueue(nowData);
|
||||
}
|
||||
GameObject Bao = Instantiate(explodePrefab, transform);
|
||||
Bao.transform.localPosition = Vector3.zero;
|
||||
|
@ -258,31 +170,48 @@ public class UnmannedAerialVehicle : MonoBehaviour
|
|||
|
||||
void OnDestroy()
|
||||
{
|
||||
|
||||
transform.DOKill();
|
||||
}
|
||||
|
||||
|
||||
public void SendMsg(Transform attackTarget)
|
||||
{
|
||||
string nowData = GetSyncData(attackTarget);
|
||||
Debug.Log(nowData);
|
||||
unmannedAerialVehicleManage.equipmentCommon.send2roomStr.Enqueue(nowData);
|
||||
//_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单个无人机攻击目标传递
|
||||
/// 发起攻击
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected string GetSyncData(Transform attackTarget)
|
||||
public void AttAck(Transform target)
|
||||
{
|
||||
return string.Format("{0},{1},{2},{3},{4},{5}", "SingleDronePosition", unmannedAerialVehicleManage.equipmentCommon.deviceID, serialNumber,
|
||||
attackTarget.position.x, attackTarget.position.y, attackTarget.position.z);
|
||||
|
||||
Debug.Log("开始攻击目标..:" + target.name);
|
||||
Vector3 _v3 = target.position;
|
||||
|
||||
transform.DOLookAt(_v3, 0.1f).OnComplete(() =>
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target.position);
|
||||
transform.DOMove(target.position, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
|
||||
{
|
||||
if (!target)
|
||||
{
|
||||
BeAssaulted("没有攻击到目标");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起攻击
|
||||
/// </summary>
|
||||
public void AttAck(Vector3 target)
|
||||
{
|
||||
transform.DOLookAt(target, 0.1f).OnComplete(() =>
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target);
|
||||
transform.DOMove(target, distance / FireSpeed).SetEase(Ease.Linear).OnComplete(() =>
|
||||
{
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单个无人机被销毁
|
||||
/// </summary>
|
||||
|
|
|
@ -5,6 +5,7 @@ using System.Collections.Generic;
|
|||
using UnityEngine;
|
||||
using AdamThinkDevicesData;
|
||||
using AdamSync;
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// 单个无人机蜂群控制
|
||||
|
@ -78,7 +79,14 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
|
|||
/// </summary>
|
||||
public float FireSpeed = 20.0f;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检测范围半径
|
||||
/// </summary>
|
||||
public float detectionRadius = 50; //
|
||||
/// <summary>
|
||||
/// 是否正在攻击目标
|
||||
/// </summary>
|
||||
public bool isEngagedTarget = false;
|
||||
|
||||
#region 无人机数据
|
||||
/// <summary>
|
||||
|
@ -115,6 +123,8 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
|
|||
public string opticalReconnaissanceCapability;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
@ -140,8 +150,21 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
|
|||
StartMoveObjectAlongPath();
|
||||
}
|
||||
}
|
||||
if (equipmentCommon.isPlayer &&isStartRehearsing)
|
||||
{
|
||||
switch (pattern)
|
||||
{
|
||||
case Pattern.待机:
|
||||
break;
|
||||
case Pattern.警戒:
|
||||
SelectiveAttackDrone();
|
||||
break;
|
||||
case Pattern.攻击:
|
||||
SelectiveAttackDrone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -167,6 +190,47 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
|
|||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 攻击打击
|
||||
/// </summary>
|
||||
private void SelectiveAttackDrone()
|
||||
{
|
||||
if (isEngagedTarget) return;
|
||||
List<Collider> colliders = Physics.OverlapSphere(transform.position, detectionRadius).ToList(); // 检索范围内的所有碰撞体
|
||||
List<Collider> colliders1 = colliders.FindAll(x => x.transform.tag == "AttackTarget");
|
||||
if (colliders1.Count > 0)
|
||||
{
|
||||
isEngagedTarget = true;
|
||||
colliders1.ForEach(x => Debug.Log(x.transform.name));
|
||||
int _number = UnityEngine.Random.Range(0, colliders1.Count - 1);
|
||||
List<UnmannedAerialVehicle> _unmannedAerialVehicles = unmannedAerialVehicles.FindAll(x => x != null);
|
||||
SendMsg(colliders1[_number].transform);
|
||||
for (int i = 0; i < _unmannedAerialVehicles.Count; i++)
|
||||
{
|
||||
_unmannedAerialVehicles[i].AttAck(colliders1[_number].transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendMsg(Transform attackTarget)
|
||||
{
|
||||
string nowData = GetSyncData(attackTarget);
|
||||
Debug.Log(nowData);
|
||||
_ = SyncCreateRoom.SendMessageAsync(string.Format("send2room {0}", nowData));
|
||||
}
|
||||
/// <summary>
|
||||
/// 无人机攻击目标传递
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected string GetSyncData(Transform attackTarget)
|
||||
{
|
||||
return string.Format("{0},{1},{2},{3},{4}", "SingleDronePosition", equipmentCommon.deviceID,
|
||||
attackTarget.position.x, attackTarget.position.y, attackTarget.position.z);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#region 启动暂停
|
||||
/// <summary>
|
||||
/// 导条变化调用
|
||||
|
|
Loading…
Reference in New Issue