152 lines
5.1 KiB
C#
152 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
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;
|
|
|
|
[SerializeField] private Button uiTriggerBtn;
|
|
[SerializeField] private GameObject uiCanvas;
|
|
|
|
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;
|
|
uiTriggerBtn.onClick.AddListener(() =>
|
|
{
|
|
uiCanvas.SetActive(!uiCanvas.activeSelf);
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |