166 lines
6.3 KiB
C#
166 lines
6.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using XFrame.Core.UI;
|
|
using XFrame.Core.Tool;
|
|
using System.Collections;
|
|
using DG.Tweening;
|
|
using UnityStandardAssets.Vehicles.Car;
|
|
public class DrivePanel : XUIPanel
|
|
{
|
|
public static DrivePanel instance;
|
|
public Text drivetime, drivedistance, electricity, drivepeed;
|
|
public Image normalCharg, Non_FeelCharge;
|
|
private RectTransform middle, down;
|
|
public CarUserControl carUser;
|
|
Vector3 tarpos ,currentPos;
|
|
|
|
public bool isNormalCharge=false;
|
|
public bool isNonFeelCharge=false;
|
|
float electicValue = 45f;
|
|
|
|
#region
|
|
// 无感支付
|
|
public Button 无感支付, 直流充电;
|
|
public Text 充电预计时间, 预计时间, 已充时间, 已充电费, 充电电量, 充电时间, 充电电费;
|
|
Tween t;
|
|
float timecount = 0;
|
|
float nextTime=0;// 解决刚体切换动力学 重复碰撞器问题
|
|
#endregion
|
|
|
|
public DrivePanel() : base(UIType.PopUp, UIMode.HideOther, UICollider.None)
|
|
{
|
|
uiPath = "UI/DrivePanel/Prefabs/DrivePanel";
|
|
}
|
|
public override void Awake(GameObject go)
|
|
{
|
|
instance = this;
|
|
AutoAssign.InitObject(this, transform);
|
|
middle = transform.Find("bg/middle").GetComponent<RectTransform>();
|
|
down = transform.Find("bg/down").GetComponent<RectTransform>();
|
|
middle.anchoredPosition = new Vector2(middle.anchoredPosition.x,10);
|
|
down.anchoredPosition = new Vector2(down.anchoredPosition.x, -124.9f);
|
|
down.DOAnchorPos(new Vector2(down.anchoredPosition.x, 128), 1.5f);
|
|
middle.DOAnchorPos(new Vector2(middle.anchoredPosition.x, -50), 1.5f);
|
|
drivedistance.fontSize = 20;
|
|
}
|
|
public override void Active()
|
|
{
|
|
base.Active();
|
|
OnNormalCharging(false);
|
|
OnNonInductiveCharging(false);
|
|
carUser = GameObject.FindObjectOfType<CarUserControl>();
|
|
XUIRoot.Instance.StartCoroutine(SetTime());
|
|
}
|
|
// UI与car功能分离
|
|
IEnumerator SetTime()
|
|
{
|
|
int time = 0;
|
|
tarpos = carUser.tarPos.position;
|
|
float distance;
|
|
|
|
|
|
while (true)
|
|
{
|
|
time++;
|
|
int hour = (int)time/3600;
|
|
int minute = (time - hour * 3600) / 60;
|
|
int second = (int)time - hour * 3600 - minute * 60;
|
|
drivetime.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
|
|
drivepeed.text = carUser.m_Car.CurrentSpeed.ToString("f0");
|
|
currentPos = carUser.transform.position;
|
|
distance = Vector3.Distance(tarpos, currentPos);
|
|
distance /= 90;
|
|
drivedistance.text = distance.ToString("f1");
|
|
|
|
// 充电
|
|
if (isNormalCharge)
|
|
{
|
|
electicValue += 1.2f;
|
|
}
|
|
else if (isNonFeelCharge)
|
|
{
|
|
electicValue += 3;
|
|
timecount++;
|
|
已充时间.text = timecount.ToString("f0");
|
|
已充电费.text = (timecount * 0.5f).ToString("f1");
|
|
}
|
|
else
|
|
{
|
|
electicValue -= 0.2f;
|
|
}
|
|
electicValue = Mathf.Clamp(electicValue, 1f, 100);
|
|
electricity.text = electicValue.ToString("f0");
|
|
yield return new WaitForSeconds(1);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 普通充电
|
|
/// </summary>
|
|
/// <param name="active"></param>
|
|
public void OnNormalCharging(bool active)
|
|
{
|
|
isNormalCharge = active;
|
|
normalCharg.gameObject.SetActive(active);
|
|
}
|
|
/// <summary>
|
|
/// 无感充电
|
|
/// </summary>
|
|
/// <param name="active"></param>
|
|
public void OnNonInductiveCharging(bool active)
|
|
{
|
|
isNonFeelCharge = false;
|
|
Non_FeelCharge.gameObject.SetActive(false);
|
|
if (active)
|
|
{
|
|
if (nextTime > Time.time)
|
|
{
|
|
Debug.Log("重复碰撞");
|
|
return;
|
|
}
|
|
无感支付.onClick.RemoveAllListeners();
|
|
Non_FeelCharge.gameObject.SetActive(true);
|
|
for (int i = 0; i <Non_FeelCharge.transform.childCount; i++)
|
|
{
|
|
Non_FeelCharge.transform.GetChild(i).gameObject.SetActive(false);
|
|
}
|
|
Non_FeelCharge.transform.GetChild(0).gameObject.SetActive(true);
|
|
// 确认
|
|
无感支付.onClick.AddListener(()=> {
|
|
Non_FeelCharge.transform.GetChild(0).gameObject.SetActive(false);
|
|
Non_FeelCharge.transform.GetChild(1).gameObject.SetActive(true);
|
|
float time = (100 - electicValue) / 3+1;
|
|
充电预计时间 .text= time.ToString("f0");
|
|
float timeCount = 1;
|
|
nextTime = Time.time + time + 5.5f+1;
|
|
t.Kill();
|
|
t =DOTween.To(() => timeCount, a => timeCount = a, 1, 3.5f).OnComplete(() =>
|
|
{
|
|
Non_FeelCharge.transform.GetChild(1).gameObject.SetActive(false);
|
|
Non_FeelCharge.transform.GetChild(2).gameObject.SetActive(true);
|
|
预计时间.text=充电预计时间.text;
|
|
isNonFeelCharge = true;
|
|
timecount = 0;
|
|
t = DOTween.To(() => timeCount, a => timeCount = a, 1, time).OnComplete(() =>
|
|
{
|
|
Non_FeelCharge.transform.GetChild(2).gameObject.SetActive(false);
|
|
Non_FeelCharge.transform.GetChild(3).gameObject.SetActive(true);
|
|
isNonFeelCharge = false;
|
|
充电电量.text = (time * 3).ToString("f0");
|
|
充电时间.text = time.ToString("f0");
|
|
充电电费.text=(time*0.5f).ToString("f1");
|
|
DOTween.To(() => timeCount, a => timeCount = a, 1, 2f).OnComplete(() =>
|
|
{
|
|
Debug.Log("充电完成");
|
|
Non_FeelCharge.gameObject.SetActive(false);
|
|
carUser.enabled = true;
|
|
carUser.GetComponent<Animator>().enabled = false;
|
|
carUser.GetComponent<Rigidbody>().isKinematic = false;
|
|
carUser.GetComponent<Rigidbody>().detectCollisions = true;
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|