天津23维预案
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.
 
 
 
 
 
 

313 lines
11 KiB

using UnityEngine;
using System.Collections.Generic;
using AX.TrackRecord;
using AX.MessageSystem;
using System;
using System.Collections;
public class AgentRecord : MonoBehaviour
{
[HideInInspector]
public Vector3[] nowpath;//记录寻路拐点
private AgentControl agentControl;
UnityEngine.AI.NavMeshPath navMeshPath;
UnityEngine.AI.NavMeshAgent agent;
private bool IsFirstPFFlag = false;//该记录的第一次寻路标志,开始记录时赋值true
public static bool FirstPFEventFlag = false;
public float timer = 0;//计时器
private float startSpeed;//速度
private float startangularSpeed;//转弯速度
private float acceleration;//加速度
public static int speedFactor = 1;//加速倍数
//public static GameObject xfy_nearest = null;
public bool theNearestXFY = false;
private const float threshold = 1.5f;//距离阈值
private bool startGo;
public List<GameObject> xfyList = new List<GameObject>();
public int target_cengID = 0;
public BuildType target_buildType = BuildType.TypeA;
private Vector3 lastPos;
public void SetList()
{
var list = InputManager.Instance_.GetSelectedCharacters();
xfyList.Clear();
foreach (var xfy in list)
{
xfyList.Add(xfy);
}
}
// Use this for initialization
void Start()
{
agentControl = GetComponent<AgentControl>();
navMeshPath = new UnityEngine.AI.NavMeshPath();
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
//speedFactor = 1;
startSpeed = agent.speed;
startangularSpeed = agent.angularSpeed;
acceleration = 50000000;
}
void OnEnable()
{
MessageDispatcher.AddListener("StartRecord", StartRecord);
TheBackView.instance.SpeedChangeEvent += OnSpeedChangeEvent;
if (!agent)
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
StartCoroutine(Delay());
}
IEnumerator Delay()
{
yield return null;
agent.speed = startSpeed * speedFactor;
agent.angularSpeed = startangularSpeed * speedFactor;
acceleration = 50000000;
}
void OnDestroy()
{
MessageDispatcher.RemoveListener("StartRecord", StartRecord);
}
void OnDisable()
{
MessageDispatcher.RemoveListener("StartRecord", StartRecord);
TheBackView.instance.SpeedChangeEvent -= OnSpeedChangeEvent;
}
private int lastSpeed = -1;
void OnSpeedChangeEvent(SpeedChangeEventArgs e)
{
//e.speed--当前的加倍播放的速率??????
agent.speed = startSpeed * e.speed;
agent.angularSpeed = startangularSpeed * e.speed;
acceleration = 50000000;
if (e.speed == 0)
{
agent.Stop();
Time.timeScale = 0;
}
else
{
if (lastSpeed == 0)
{
agent.Resume();//Resume()-----暂停之后,沿着当前路径重新恢复前进
Time.timeScale = 1;
}
}
lastSpeed = e.speed;
}
void StartRecord(IMessage message)
{
FirstPFEventFlag = true;
IsFirstPFFlag = true;
timer = RecordManager.Instance.RecordTimer;
if (nowpath != null && GetComponent<UnityEngine.AI.NavMeshAgent>().velocity.magnitude > 0)
{
navMeshPath.ClearCorners();
try
{
UnityEngine.AI.NavMesh.CalculatePath(transform.position, nowpath[nowpath.Length - 1], -1, navMeshPath);
}
catch (Exception e)
{
System.Diagnostics.Trace.TraceError(e.Message);
}
nowpath = navMeshPath.corners;
}
}
bool clearPathFlag = false;
// Update is called once per frame
void Update()
{
if (startGo && nowpath!=null && nowpath.Length>0)
{
//ChangeCengID();
//var speed = agent.velocity.magnitude;
var dis = Vector3.Distance(transform.position, nowpath[nowpath.Length - 1]);
if (dis <= threshold + 0.1f)
{
startGo = false;
//if (theNearestXFY)
//{
for (int i = xfyList.Count - 1; i >= 0; i--)
{
GameObject xfy = xfyList[i];
if (!xfy)
{//消防员用delete删除后,查找不到
xfyList.Remove(xfy);
continue;
}
else
{
xfy.GetComponent<CengID>().cengID = target_cengID;
xfy.GetComponent<CengID>().CengIDBuildType = target_buildType;
}
}
//}
GetComponent<CengID>().cengID = target_cengID;
GetComponent<CengID>().CengIDBuildType = target_buildType;
//clearPathFlag = true;
}
}
if (RecordManager.Instance.IsRecording)
{
Record();
}
//if (clearPathFlag)
//{
// var speed= agent.velocity.magnitude;
// if (speed == 0)
// {
// clearPathFlag = false;
// nowpath = null;
// }
//}
}
private void ChangeCengID()
{
//靠近一个路点,设置cengID属性(只有消防员需要设置)
if (gameObject.name.Contains("xiaofang"))//xiaofangyuan
{
if (nowpath != null && GetComponent<UnityEngine.AI.NavMeshAgent>().velocity.magnitude > 0)//寻路过程中
{
var pathpointList = new List<Vector3>(nowpath);
var index = pathpointList.FindIndex(p => { return Vector3.Distance(transform.position, p) < 3f; });
if (index >= 0)//靠近某一个路径点了
{
var buildType = target_buildType;
if (target_cengID != 0)
{
int cengID_get = CengIDManager.Instance.GetCengIDByY(pathpointList[index].y, buildType);
gameObject.GetComponent<CengID>().cengID = cengID_get;
//if (cengID_get != 0)
//{
// gameObject.GetComponent<CengID>().CengIDBuildType = buildType;
//}
//else
//{
// gameObject.GetComponent<CengID>().CengIDBuildType = BuildType.None;
//}
gameObject.GetComponent<CengID>().CengIDBuildType = buildType;
}
else
{
gameObject.GetComponent<CengID>().cengID = target_cengID;
gameObject.GetComponent<CengID>().CengIDBuildType = buildType;
}
}
}
}
}
public void StartGo_(GameObject hitObj)
{
//StartCoroutine(StartGo(hitObj));
startGo = true;
if (hitObj.GetComponent<CengID>())
{
target_cengID = hitObj.GetComponent<CengID>().cengID;
target_buildType = hitObj.GetComponent<CengID>().CengIDBuildType;
}
}
public void StartGo_(int cengID, BuildType buildType)
{
//StartCoroutine(StartGo(hitObj));
startGo = true;
target_cengID = cengID;
target_buildType = buildType;
}
private IEnumerator StartGo(GameObject hitObj)
{
yield return new WaitForSeconds(0.2f);
startGo = true;
if (hitObj.GetComponent<CengID>())
{
target_cengID = hitObj.GetComponent<CengID>().cengID;
target_buildType = hitObj.GetComponent<CengID>().CengIDBuildType;
}
//SetList();
}
void Record()
{
//if ((gameObject.name.Contains("xiaofangyuan") && theNearestXFY)
// || gameObject.name.Contains("xiaofangche"))
//当前位置靠近某一个路径点足够进的时候,记录下一个寻路事件
if (nowpath != null && nowpath.Length>0 && GetComponent<UnityEngine.AI.NavMeshAgent>().velocity.magnitude > 0)//寻路过程中
{
var pathpointList = new List<Vector3>(nowpath);
var index = pathpointList.FindIndex(p => { return Vector3.Distance(transform.position, p) < threshold; });
if (index >= 0)//靠近某一个路径点了
{
if (index == 0)//是nowpath的0号点
{
if (IsFirstPFFlag)//该次记录的第一次寻路的第一个路径点,更新下timer时间
{
timer = RecordManager.Instance.RecordTimer;
IsFirstPFFlag = false;
return;
}
}
//var same_point_index = RecordManager.Instance.record.EventList.FindLastIndex(
// e =>
// {
// return e.eventType == eventTypeRecord.PathFinding && e.objAttriList.Count > 0
// && e.objAttriList[0].ObjName.Equals(name + "(Node)") && Vector3.Distance(e.Navto, nowpath[index]) < 0.05f
// && Mathf.Abs(RecordManager.Instance.RecordTimer - e.timer) <= (threshold * 2 / agent.speed);
// });
var dis = Vector3.Distance(nowpath[index], lastPos);
if (dis <0.01f)//同一个点,该点已经记录过了
{
if (index == 0)
{
//只更新时间,不记录寻路事件了
timer = RecordManager.Instance.RecordTimer;
}
}
else//未找到满足条件的点,证明该点未记录过
{
MessageDispatcher.SendMessage(name, "RecordPathFindingEvent",
new PathFindingAttri { Navto = nowpath[index], timer = timer });
lastPos = nowpath[index];
Debug.LogWarning("RecordPathFindingEvent:" + name);
timer = RecordManager.Instance.RecordTimer;
//if (index < nowpath.Length - 1)
//{
// theNearestXFY = false;
// GetComponent<AgentControl>().SetNearestXfy(nowpath, index);
//}
if (index == nowpath.Length - 1)
{
nowpath = null;
//theNearestXFY = false;
}
}
}
}
}
}