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.
118 lines
4.2 KiB
118 lines
4.2 KiB
4 years ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class CarMoveManager : MonoBehaviour
|
||
|
{
|
||
|
public PowerMessage PowerMsg;
|
||
|
public GameObject[] RoadPoints;//移动拐点
|
||
|
public GameObject Car;
|
||
|
private float PathLength;
|
||
|
private bool CanMove;
|
||
|
private float Speed;
|
||
|
private int index = 1;
|
||
|
//private float disPrevCan;
|
||
|
//private float disPrevPointCan;
|
||
|
//private float disTotal;
|
||
|
|
||
|
private float indexPath;
|
||
|
private float movedDis;
|
||
|
private float time;
|
||
|
private Vector2 startPos;
|
||
|
|
||
|
//private List<FireCarEngine> MyList;
|
||
|
private List<KeyValuePair<FireCarEngine, int>> MyList;
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
//读取路径长度作为总出警总距离
|
||
|
for (int i = 0; i < RoadPoints.Length - 1; i++)
|
||
|
{
|
||
|
//PathLength += Vector2.Distance(RoadPoints[i].transform.position, RoadPoints[i + 1].transform.position);
|
||
|
PathLength += Vector2.Distance(RoadPoints[i].GetComponent<RectTransform>().anchoredPosition,
|
||
|
RoadPoints[i + 1].GetComponent<RectTransform>().anchoredPosition);
|
||
|
}
|
||
|
}
|
||
|
private void ShowNowMessage()
|
||
|
{
|
||
|
//List<FireCarEngine> TestList = TestData();
|
||
|
//PowerMsg.ShowMessage(MyList, (PathLength - disTotal).ToString("#.##") + "Km",
|
||
|
// DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss"));
|
||
|
|
||
|
float tempDis = 0;//当前段路径已移动多少距离
|
||
|
|
||
|
tempDis = Vector2.Distance(Car.GetComponent<RectTransform>().anchoredPosition ,
|
||
|
RoadPoints[index - 1].GetComponent<RectTransform>().anchoredPosition);
|
||
|
|
||
|
float currentDis = movedDis + tempDis;
|
||
|
|
||
|
PowerMsg.ShowMessage(MyList, (PathLength - currentDis).ToString("#.##") + "Km",
|
||
|
DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss"));
|
||
|
}
|
||
|
|
||
|
//控制地图上车标志移动的方法
|
||
|
public void MoveCar(float NeedTime, List<KeyValuePair<FireCarEngine, int>> DataList)
|
||
|
{
|
||
|
Speed = PathLength / NeedTime;
|
||
|
StartCoroutine(WaitTheSet());
|
||
|
MyList = DataList;
|
||
|
Car.GetComponent<Button>().onClick.AddListener(ShowNowMessage);
|
||
|
}
|
||
|
|
||
|
private IEnumerator WaitTheSet()
|
||
|
{
|
||
|
yield return new WaitForSeconds(2f);
|
||
|
CanMove = true;
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
if (CanMove)
|
||
|
{
|
||
|
if (index < RoadPoints.Length)
|
||
|
{
|
||
|
//Car.transform.Translate((RoadPoints[index].transform.position - Car.transform.position).normalized * Time.deltaTime * Speed);
|
||
|
|
||
|
//disPrevPointCan = Vector2.Distance(Car.transform.position, RoadPoints[index - 1].transform.position);
|
||
|
//if (Vector3.Distance(Car.transform.position, RoadPoints[index].transform.position) <= 0.2)
|
||
|
//{
|
||
|
// index++;
|
||
|
// disPrevCan += disPrevPointCan;
|
||
|
//}
|
||
|
//disTotal = disPrevCan + disPrevPointCan;
|
||
|
|
||
|
indexPath = Vector2.Distance(RoadPoints[index].GetComponent<RectTransform>().anchoredPosition,
|
||
|
RoadPoints[index - 1].GetComponent<RectTransform>().anchoredPosition);
|
||
|
|
||
|
startPos = RoadPoints[index - 1].GetComponent<RectTransform>().anchoredPosition;
|
||
|
time += Time.deltaTime;
|
||
|
|
||
|
if (RoadPoints[index].GetComponent<RectTransform>().anchoredPosition
|
||
|
!= Car.GetComponent<RectTransform>().anchoredPosition)
|
||
|
{
|
||
|
Car.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(startPos,
|
||
|
RoadPoints[index].GetComponent<RectTransform>().anchoredPosition,
|
||
|
time / (indexPath / Speed));
|
||
|
}
|
||
|
|
||
|
if (time >= (indexPath / Speed))
|
||
|
{
|
||
|
Car.GetComponent<RectTransform>().anchoredPosition = RoadPoints[index].GetComponent<RectTransform>().anchoredPosition;
|
||
|
|
||
|
index++;
|
||
|
time = 0;
|
||
|
movedDis += indexPath;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
movedDis = PathLength;
|
||
|
Speed = 0;
|
||
|
CanMove = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|