Car/Assets/Scripts/Main.cs

231 lines
7.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using YogiGameCore.Utils.MonoExtent;
public class Main : MonoBehaviour
{
[SerializeField] private GameConfig config;
public List<CarConfig> AllCars => config.Cars;
private int currentCarIndex;
public CarConfig CurrentCar => config.Cars[currentCarIndex];
/// <summary>
/// 当前部件集合索引
/// </summary>
private int currentCarComponentConfigIndex = -1;
public int GetCurrentCarComponentConfigIndex => currentCarComponentConfigIndex;
/// <summary>
/// 获得当前聚焦的部件合集
/// </summary>
private CarComponentConfig currentCarComponents
{
get
{
if (CurrentCar..Count <= currentCarComponentConfigIndex)
return null;
return CurrentCar.[currentCarComponentConfigIndex];
}
}
public UnityEvent<string> OnCarNameChanged, OnCarConfigChanged, OnCarComponentHighlight;
public event Action<CarConfig> OnCarChanged;
public event Action<CarComponentConfig> OnCarComponentConfigChanged;
public event Action<ComponentConfig, int> OnSwitchComponent;
private void HideAllCars()
{
StopAllCoroutines();
foreach (var configCar in config.Cars)
{
// SetCarActiveCoroutine()
SetCarActive(configCar.,false);
// configCar.车辆基础物体.SetActive(false);
foreach (var carComponentConfig in configCar.)
{
foreach (var component in carComponentConfig.)
{
SetCarActive(component.,false);
// component.物体.SetActive(false);
}
}
}
}
[Button]
public void ResetCurrentCar()
{
for (var index = 0; index < this.CurrentCar..Count; index++)
{
var carComponentConfig = this.CurrentCar.[index];
carComponentConfig.SelectComponentIndex = 0;
for (var j = 0; j < carComponentConfig..Count; j++)
{
var go = carComponentConfig.[j].;
SetCarActive(go, j == 0);
// carComponentConfig.车辆组件合集[j].物体.SetActive(j == 0);
}
}
SwitchComponent(0);
}
[Button]
public void PrevCar()
{
SetCarDisplay(currentCarIndex, false);
currentCarIndex--;
if (currentCarIndex < 0)
currentCarIndex = config.Cars.Count - 1;
SetCarDisplay(currentCarIndex, true);
}
[Button]
public void NextCar()
{
SetCarDisplay(currentCarIndex, false);
currentCarIndex++;
if (currentCarIndex >= config.Cars.Count)
currentCarIndex = 0;
SetCarDisplay(currentCarIndex, true);
}
[Button]
public void DisplayCar(int carIndex)
{
SetCarDisplay(currentCarIndex, false);
currentCarIndex = carIndex;
SetCarDisplay(currentCarIndex, true);
}
private void SetCarDisplay(int carIndex, bool isActive)
{
if (carIndex == -1)
return;
var configCar = config.Cars[carIndex];
// configCar.车辆基础物体.SetActive(isActive);
SetCarActive(configCar.,isActive);
foreach (var carComponentConfig in configCar.)
{
if (isActive)
{
if (carComponentConfig..Count <= carComponentConfig.SelectComponentIndex)
continue;
var go = carComponentConfig.[carComponentConfig.SelectComponentIndex].;
SetCarActive(go,true);
// carComponentConfig.车辆组件合集[carComponentConfig.SelectComponentIndex].物体.SetActive(true);
}
else
{
foreach (var component in carComponentConfig.)
{
// component.物体.SetActive(false);
SetCarActive(component.,false);
}
}
}
if (isActive)
{
UpdateCarName();
OnCarChanged?.Invoke(this.CurrentCar);
SwitchComponentConfig(0);
}
}
/// <summary>
/// 更新车名
/// </summary>
private void UpdateCarName()
{
this.OnCarNameChanged.Invoke(this.CurrentCar.);
}
/// <summary>
/// 更新部件名字
/// </summary>
private void UpdateCarConfigsName()
{
this.OnCarConfigChanged.Invoke(this.currentCarComponents.);
}
/// <summary>
/// 切换不同的部件
/// </summary>
/// <param name="carComponentConfigIndex"></param>
[Button]
public void SwitchComponentConfig(int carComponentConfigIndex)
{
if (this.CurrentCar..Count <= carComponentConfigIndex)
return;
this.currentCarComponentConfigIndex = carComponentConfigIndex;
UpdateCarConfigsName();
OnCarComponentConfigChanged?.Invoke(this.currentCarComponents);
}
[Button]
public void SwitchComponent(int componentIndex)
{
if (currentCarComponents..Count <= componentIndex)
return;
int originIndex = currentCarComponents.SelectComponentIndex;
SetCurrentCarConfigDisplay(originIndex, false);
int newIndex = currentCarComponents.SelectComponentIndex = componentIndex;
SetCurrentCarConfigDisplay(newIndex, true);
// 更新选择的部件名
ComponentConfig componentConfig = currentCarComponents.[componentIndex];
OnCarComponentHighlight.Invoke(componentConfig.);
OnSwitchComponent?.Invoke(componentConfig, componentIndex);
}
private void SetCurrentCarConfigDisplay(int componentIndex, bool isDisplay)
{
if (componentIndex == -1)
return;
var go = currentCarComponents.[componentIndex].;
// go.SetActive(isDisplay);
SetCarActive(go, isDisplay);
}
private void SetCarActive(GameObject go, bool isActive)
{
Debug.Log($"SetCarActive ({go.name}) ({isActive})");
this.StartCoroutine(SetCarActiveCoroutine(go, isActive, .5f));
}
private IEnumerator SetCarActiveCoroutine(GameObject go, bool isActive, float time)
{
var renderers = go.GetComponentsInChildren<Renderer>();
var propertyToID = Shader.PropertyToID("_ClipScaleV");
float timer = 0;
float min = -1f;
float max = 1.0f;
while (timer < time)
{
timer += Time.deltaTime;
yield return null;
var evl = timer / time;
float result = isActive ? Mathf.Lerp(max, min, evl) : Mathf.Lerp(min, max, evl);
foreach (var r in renderers)
r.material.SetFloat(propertyToID, result);
}
}
private void Awake()
{
HideAllCars();
}
private void Start()
{
currentCarIndex = -1;
NextCar();
}
}