Car/Assets/Scripts/Main.cs

174 lines
5.0 KiB
C#

using System;
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 => CurrentCar.[currentCarComponentConfigIndex];
public UnityEvent<string> OnCarNameChanged, OnCarConfigChanged, OnCarComponentHighlight;
public event Action<CarConfig> OnCarChanged;
public event Action<CarComponentConfig> OnCarComponentConfigChanged;
private void HideAllCars()
{
foreach (var configCar in config.Cars)
{
configCar..SetActive(false);
foreach (var carComponentConfig in configCar.)
{
foreach (var component in carComponentConfig.)
{
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++)
{
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);
foreach (var carComponentConfig in configCar.)
{
if (isActive)
{
carComponentConfig.[carComponentConfig.SelectComponentIndex]..SetActive(true);
}
else
{
foreach (var component in carComponentConfig.)
{
component..SetActive(false);
}
}
}
if (isActive)
{
UpdateCarName();
SwitchComponentConfig(0);
OnCarChanged?.Invoke(this.CurrentCar);
}
}
/// <summary>
/// 更新车名
/// </summary>
private void UpdateCarName()
{
this.OnCarNameChanged.Invoke(this.CurrentCar.);
}
/// <summary>
/// 更新部件名字
/// </summary>
private void UpdateCarConfigsName()
{
this.OnCarConfigChanged.Invoke(this.currentCarComponents.);
}
[Button]
public void SwitchComponentConfig(int carComponentConfigIndex)
{
this.currentCarComponentConfigIndex = carComponentConfigIndex;
UpdateCarConfigsName();
OnCarComponentConfigChanged?.Invoke(this.currentCarComponents);
}
[Button]
public void SwitchComponent(int componentIndex)
{
int originIndex = currentCarComponents.SelectComponentIndex;
SetCurrentCarConfigDisplay(originIndex, false);
int newIndex = currentCarComponents.SelectComponentIndex = componentIndex;
SetCurrentCarConfigDisplay(newIndex, true);
// 更新选择的部件名
ComponentConfig componentConfig = currentCarComponents.[componentIndex];
OnCarComponentHighlight.Invoke(componentConfig.);
}
private void SetCurrentCarConfigDisplay(int componentIndex, bool isDisplay)
{
if (componentIndex == -1)
return;
currentCarComponents.[componentIndex]..SetActive(isDisplay);
}
private void Awake()
{
HideAllCars();
}
private void Start()
{
currentCarIndex = -1;
NextCar();
}
}