Car/Assets/Scripts/UI/GamePanel.cs

144 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using YogiGameCore.ComponentEx;
using YogiGameCore.Utils;
public class GamePanel : MonoBehaviour
{
[SerializeField] private Main main;
[SerializeField] private UberButton selectComponentConfigBtnPrefab;
[SerializeField] private Transform selectComponentConfigParent;
[SerializeField] private UberButton selectComponentPrefab;
[SerializeField] private Transform selectComponentParent;
[SerializeField] private SearchView searchView;
[SerializeField] private TMPTypewriter typewriterPrefab;
[SerializeField] private Transform componentTextParent;
private Dictionary<string, TMPTypewriter> spawnedTypewriters;
/// <summary>
/// 当前车的当前聚焦的 部件类型名
/// </summary>
private string componentConfigName;
private void ClearAllComponentDisplayText()
{
Debug.Log("清空车辆所有文本信息");
componentConfigName = null;
componentTextParent.DestroyChildren();
spawnedTypewriters = new Dictionary<string, TMPTypewriter>();
}
private void OnSwitchComponent(ComponentConfig componentConfig, int componentIndex)
{
Debug.Log("OnSwitchComponent");
if (spawnedTypewriters == null || this.componentConfigName == null)
return;
if (!spawnedTypewriters.TryGetValue(this.componentConfigName, out var typewriter))
{
typewriter = GameObject.Instantiate(typewriterPrefab, componentTextParent);
spawnedTypewriters.Add(this.componentConfigName, typewriter);
}
Debug.Log("Spawn Typewriter");
if (typewriter == null)
return;
typewriter.StopAllCoroutines();
typewriter.PlayAnimation(componentConfig.);
}
public void ResetCurrentComponent()
{
var btns = selectComponentParent.GetComponentsInChildren<UberButton>();
if (btns.Length <= 0) return;
btns[0].SetHold(true);
}
private void Awake()
{
ClearAllComponentDisplayText();
this.main.OnCarChanged += OnMainOnOnCarChanged;
this.main.OnCarComponentConfigChanged += OnCarComponentConfigChanged;
this.main.OnSwitchComponent += OnSwitchComponent;
}
private void Start()
{
var cars = main.AllCars.Cast<object>().ToList();
searchView.SetUp(cars, (obj) => { return ((CarConfig)obj).; }, (result) =>
{
var carConfig = result.As<CarConfig>();
var index = cars.IndexOf(carConfig);
main.DisplayCar(index);
});
}
private void OnCarComponentConfigChanged(CarComponentConfig config)
{
selectComponentParent.DestroyChildren();
for (var index = 0; index < config..Count; index++)
{
var component = config.[index];
var componentName = component.;
var btn = SpawnSelectComponentBtn(componentName, index);
Debug.Log($"SelectComponentIndex:{config.SelectComponentIndex}");
if (config.SelectComponentIndex == index)
{
btn.SetHold(true);
}
}
componentConfigName = config.;
Debug.Log($"车辆聚焦的部件类型改变了:{componentConfigName}");
}
private UberButton SpawnSelectComponentBtn(string componentName, int index)
{
var btn = GameObject.Instantiate(selectComponentPrefab, selectComponentParent);
btn.GetComponentInChildren<TextMeshProUGUI>().text = componentName;
btn.OnToggleValueChanged += v =>
{
if (!v)
return;
this.main.SwitchComponent(index);
};
return btn;
}
void OnMainOnOnCarChanged(CarConfig config)
{
selectComponentConfigParent.DestroyChildren();
for (var index = 0; index < config..Count; index++)
{
var carComponentConfig = config.[index];
var index1 = index;
var btn = SpawnSelectComponentConfigBtn(carComponentConfig., (v) =>
{
if (!v)
return;
main.SwitchComponentConfig(index1);
main.SwitchComponent(0);
});
if (main.GetCurrentCarComponentConfigIndex == index1)
btn.SetHold(true);
}
ClearAllComponentDisplayText();
}
private UberButton SpawnSelectComponentConfigBtn(string displayText, Action<bool> clickAction)
{
var btn = GameObject.Instantiate(selectComponentConfigBtnPrefab, selectComponentConfigParent);
var textMeshProUGUI = btn.GetComponentInChildren<TextMeshProUGUI>();
textMeshProUGUI.text = displayText;
btn.OnToggleValueChanged += clickAction;
return btn;
}
}