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.

130 lines
3.2 KiB

2 years ago
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TraficManager : MonoBehaviour
{
public static TraficManager Instance = null;
[Header("红绿灯图片")]
public Sprite greenSpt;
public Sprite redSpt;
public Sprite yellowSpt;
[Header("属性")]
public float greenTime = 20;
public float redTime = 10;
public float yellowTime = 2f;
private TraficLight[] traficLightArr;
[Header("闯红灯处理窗口")]
public GameObject chuliWnd;
[Header("收费相关")]
public GameObject shoufeiTxtPrefab;
public Transform shoufeiInfoParent;
public Scrollbar shoufeiScrollbar;
public GameObject shoufeiTipParent;
public bool startJiFei { set; get; }
private void Start()
{
Init();
}
public void Init()
{
Instance = this;
InitTraficLight();
}
public void InitTraficLight()
{
traficLightArr = GameObject.FindObjectsOfType<TraficLight>();
for (int i = 0; i < traficLightArr.Length; i++)
{
traficLightArr[i].Init();
}
}
public void AddCheliuNum(Text txt)
{
int value = int.Parse(txt.text);
value += UnityEngine.Random.Range(1, 3);
txt.text = value.ToString();
}
public void CrossRedLight()
{
StartCoroutine(TimeAction(UnityEngine.Random.Range(20f,40f), () => {
if (DriveManager.Instance != null)
{
Car car = DriveManager.Instance.carList[UnityEngine.Random.Range(0, DriveManager.Instance.carList.Count)];
car.iscrossRedLight = true;
}
CrossRedLight();
}));
}
IEnumerator TimeAction(float time,Action action)
{
yield return new WaitForSeconds(time);
action();
}
public void AddShoufeiInfo(int carTypeIndex,string CarId,int money)
{
if (!startJiFei) return;
GameObject go = GameObject.Instantiate(shoufeiTxtPrefab, shoufeiInfoParent);
Vector2 size = shoufeiInfoParent.GetComponent<RectTransform>().sizeDelta+new Vector2(0,30);
shoufeiInfoParent.GetComponent<RectTransform>().sizeDelta = size;
shoufeiScrollbar.value += 0.1f;
string cartype = "";
switch (carTypeIndex)
{
case 0:
cartype = "轿车";
break;
case 1:
cartype = "SUV";
break;
case 2:
cartype = "货车";
break;
default:
cartype = "轿车";
break;
}
if (money == 0)
{
go.GetComponent<Text>().text = cartype + "-" + CarId + "-" + "开始计费";
}
else
{
go.GetComponent<Text>().text = cartype + "-" + CarId + "-" + money.ToString("0")+"元";
}
}
public void ShowShouFeiTip()
{
if (TraficManager.Instance.startJiFei)
{
shoufeiTipParent.gameObject.SetActive(true);
}
else
{
shoufeiTipParent.gameObject.SetActive(false);
}
}
}