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.
271 lines
9.3 KiB
271 lines
9.3 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
using Newtonsoft.Json; |
|
|
|
[Serializable] |
|
public class PoliceRoadConfig |
|
{ |
|
public string SceneName; |
|
public List<Vector2> AllRoadPos = new List<Vector2>(); |
|
public string ImagePath; |
|
} |
|
|
|
public class CarMoveManager : MonoBehaviour |
|
{ |
|
public PowerMessage PowerMsg; |
|
|
|
public GameObject StartPos; |
|
public GameObject EndPos; |
|
public GameObject LineImageObj; |
|
public Transform LineParent; |
|
public Image BgImage; |
|
public Transform LinePosParent; |
|
public GameObject LinePosObj; |
|
|
|
public List<Vector2> AllRoadPos = new List<Vector2>(); |
|
public List<GameObject> RoadPoints = new List<GameObject>();//移动拐点 |
|
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() |
|
{ |
|
LoadConfig(); |
|
//AllRoadPos.Clear(); |
|
//foreach (var item in RoadPoints) |
|
//{ |
|
// AllRoadPos.Add(item.GetComponent<RectTransform>().localPosition); |
|
//} |
|
//读取路径长度作为总出警总距离 |
|
for (int i = 0; i < RoadPoints.Count - 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.Count) |
|
{ |
|
//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; |
|
} |
|
} |
|
} |
|
|
|
public void CreateBGLine(List<Vector2> AllRoadPos) |
|
{ |
|
foreach (Transform t in LineParent) |
|
{ |
|
Destroy(t.gameObject); |
|
} |
|
Car.transform.localPosition = AllRoadPos[0]; |
|
for (int i = 0; i < AllRoadPos.Count; i++) |
|
{ |
|
if (i < AllRoadPos.Count - 1) |
|
CreatLine(AllRoadPos[i], AllRoadPos[i + 1]); |
|
} |
|
Debug.Log("CreateLine"); |
|
} |
|
public void CreatLine(Vector2 startPos, Vector2 endPos) |
|
{ |
|
float length = Vector2.Distance(startPos, endPos); |
|
GameObject lineImage = Instantiate(LineImageObj, LineParent); |
|
lineImage.transform.localPosition = endPos; |
|
lineImage.GetComponent<RectTransform>().sizeDelta = new Vector2(5, length); |
|
|
|
double angle = Math.Atan2(startPos.y - endPos.y, startPos.x - endPos.x) * 180 / Math.PI; |
|
lineImage.transform.rotation = Quaternion.Euler(0, 0, (float)angle + 270); |
|
} |
|
|
|
public void LoadBG(string bgPath) |
|
{ |
|
string _path = Path.Combine(Application.dataPath + "/StreamingAssets", bgPath);//获取地址 |
|
|
|
FileStream _fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read);//使用流数据读取 |
|
|
|
byte[] _buffur = new byte[_fileStream.Length]; |
|
|
|
_fileStream.Read(_buffur, 0, _buffur.Length);//转换成字节流数据 |
|
|
|
Texture2D _texture2d = new Texture2D((int)BgImage.rectTransform.rect.width, (int)BgImage.rectTransform.rect.height);//设置宽高 |
|
|
|
_texture2d.LoadImage(_buffur);//将流数据转换成Texture2D |
|
|
|
Sprite _sprite = Sprite.Create(_texture2d, new Rect(0, 0, _texture2d.width, _texture2d.height), Vector3.zero);//创建一个Sprite |
|
|
|
BgImage.sprite = _sprite;//赋值 |
|
|
|
Debug.Log("加载成功!"); |
|
|
|
} |
|
|
|
public void LoadConfig() |
|
{ |
|
var config = LoadPoliceConfig(); |
|
if (config == null) |
|
{ |
|
Debug.Log("Null"); |
|
return; |
|
} |
|
LoadBG(config.ImagePath); |
|
foreach (Transform t in LinePosParent) |
|
{ |
|
Destroy(t.gameObject); |
|
} |
|
RoadPoints.Clear(); |
|
AllRoadPos.Clear(); |
|
foreach (var item in config.AllRoadPos) |
|
{ |
|
GameObject linePos = Instantiate(LinePosObj, LinePosParent); |
|
linePos.transform.localPosition = item; |
|
RoadPoints.Add(linePos); |
|
AllRoadPos.Add(item); |
|
} |
|
GameObject start = Instantiate(StartPos, LinePosParent); |
|
start.transform.localPosition = AllRoadPos[0]; |
|
GameObject end = Instantiate(EndPos, LinePosParent); |
|
end.transform.localPosition = AllRoadPos[AllRoadPos.Count - 1]; |
|
CreateBGLine(AllRoadPos); |
|
} |
|
|
|
PoliceRoadConfig LoadPoliceConfig() |
|
{ |
|
PoliceRoadConfig result = null; |
|
string json = null; |
|
string path = Path.Combine(Application.streamingAssetsPath, "police/policeConfig.json"); |
|
if (File.Exists(path)) |
|
{ |
|
json = File.ReadAllText(path); |
|
} |
|
var data = JsonConvert.DeserializeObject<List<PoliceRoadConfig>>(json); |
|
if (data != null) |
|
{ |
|
foreach (PoliceRoadConfig obj in data) |
|
{ |
|
if (GameSettings.othersSettings.mapname == obj.SceneName) |
|
{ |
|
result = obj; |
|
return result; |
|
} |
|
} |
|
} |
|
return result; |
|
} |
|
//private void LateUpdate() |
|
//{ |
|
// if (Input.GetKeyDown(KeyCode.T)) |
|
// { |
|
// string jsonpath = Path.Combine(Application.streamingAssetsPath, "policeConfig.json"); |
|
// PoliceRoadConfig config = new PoliceRoadConfig(); |
|
// config.AllRoadPos = AllRoadPos; |
|
// config.ImagePath = "Police/PoliceTexture/行车路线.jpg"; |
|
// config.SceneName = "场景名称"; |
|
// List<PoliceRoadConfig> configs = new List<PoliceRoadConfig>(); |
|
// configs.Add(config); |
|
// SaveJson(jsonpath, configs); |
|
// } |
|
//} |
|
////把上面初始化的数据进行保存 |
|
//public void SaveJson(string JsonPath, List<PoliceRoadConfig> policeConfig) |
|
//{ |
|
// //如果本地没有对应的json 文件,重新创建 |
|
// if (!File.Exists(JsonPath)) |
|
// { |
|
// File.Create(JsonPath); |
|
// } |
|
// string json = JsonConvert.SerializeObject(policeConfig); |
|
|
|
// File.WriteAllText(JsonPath, json); |
|
// Debug.Log("保存成功"); |
|
//} |
|
} |