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.

135 lines
4.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public enum LightState
{
,
,
绿,
}
public class TraficLight : MonoBehaviour
{
public Image lightImg;
public float currentLightTime;
private bool canDrive = true;
public LightState lightState;
public GameObject greenLightObj, redLightObj, yellowLightObj;
public UnityEvent greenLightAction;
public UnityEvent redLightAction;
public UnityEvent CarEnterAction;
public void Init()
{
switch (lightState)
{
case LightState.绿:
canDrive = true;
currentLightTime = TraficManager.Instance.greenTime;
lightImg.sprite = TraficManager.Instance.greenSpt;
greenLightObj.SetActive(true);
break;
case LightState.:
canDrive = false;
currentLightTime = TraficManager.Instance.yellowTime;
lightImg.sprite = TraficManager.Instance.yellowSpt;
yellowLightObj.SetActive(true);
break;
case LightState.:
canDrive = false;
currentLightTime = TraficManager.Instance.redTime;
lightImg.sprite = TraficManager.Instance.redSpt;
redLightObj.SetActive(true);
break;
}
StartCoroutine(ChangeLightState());
}
IEnumerator ChangeLightState()
{
float time = currentLightTime;
yield return new WaitForSeconds(time);
switch (lightState)
{
case LightState.绿:
lightState = LightState.;
canDrive = false;
currentLightTime = TraficManager.Instance.yellowTime;
lightImg.sprite = TraficManager.Instance.yellowSpt;
yellowLightObj.SetActive(true);
greenLightObj.SetActive(false);
redLightObj.SetActive(false);
break;
case LightState.:
lightState = LightState.;
canDrive = false;
currentLightTime = TraficManager.Instance.redTime;
lightImg.sprite = TraficManager.Instance.redSpt;
yellowLightObj.SetActive(false);
greenLightObj.SetActive(false);
redLightObj.SetActive(true);
redLightAction.Invoke();
break;
case LightState.:
lightState = LightState.绿;
canDrive = true;
currentLightTime = TraficManager.Instance.greenTime;
lightImg.sprite = TraficManager.Instance.greenSpt;
yellowLightObj.SetActive(false);
greenLightObj.SetActive(false);
redLightObj.SetActive(true);
greenLightAction.Invoke();
break;
}
StartCoroutine(ChangeLightState());
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Car")
{
CarEnterAction.Invoke();
if (other.GetComponent<Car>().iscrossRedLight == false)
{
if (!canDrive)
{
other.GetComponent<Car>().driveStatu = CarDriveStatu.;
}
}
else
{
TraficManager.Instance.chuliWnd.SetActive(true);
LawCheckList.Instance.AddCarID(other.GetComponent<Car>().CarIDCard, "闯红灯");
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Car")
{
if (canDrive)
{
other.GetComponent<Car>().driveStatu = CarDriveStatu.;
}
}
}
}