355 lines
9.8 KiB
C#
355 lines
9.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public class ctasPrefab : MonoBehaviour
|
|
{
|
|
[Header("template SO")]
|
|
public equipmentSO eTemplate;
|
|
public equipmentRandomConfigSO randomConfig;
|
|
|
|
[Header("objs")]
|
|
public GameObject select_type_obj;
|
|
public GameObject select_skill_obj;
|
|
|
|
[Header("prefab and parent")]
|
|
public GameObject select_typePrefab;
|
|
public Transform select_typeParent;
|
|
public GameObject select_skillPrefab;
|
|
public Transform select_skillParent;
|
|
|
|
[Header("text")]
|
|
public Text currentSelectText;
|
|
|
|
[Header("buttons")]
|
|
public Button yesSelectThis;
|
|
public Button noWaitButton;
|
|
|
|
private readonly List<GameObject> spawnedTypeItems = new List<GameObject>();
|
|
private readonly List<GameObject> spawnedSkillItems = new List<GameObject>();
|
|
|
|
private equipmentSO.EquipmentSkillType selectedType;
|
|
private int selectedSkillGroupId;
|
|
private string selectedSkillGroupName = string.Empty;
|
|
private GameObject selectedTypeObject;
|
|
private bool hasSelectedType;
|
|
private bool allowTypeSelection = true;
|
|
private bool allowSkillSelection;
|
|
private bool initializedExternally;
|
|
private Action<equipmentSO.EquipmentSkillType, int> onConfirmSelection;
|
|
private Action onClosed;
|
|
|
|
private void Start()
|
|
{
|
|
if (initializedExternally)
|
|
{
|
|
return;
|
|
}
|
|
|
|
BindButtons();
|
|
BuildTypeOptions();
|
|
ApplySelectionModeState();
|
|
UpdateCurrentSelectText();
|
|
RefreshButtons();
|
|
}
|
|
|
|
public void Initialize(equipmentSO template, equipmentRandomConfigSO config, bool requireTypeSelection, bool requireSkillSelection, Action<equipmentSO.EquipmentSkillType, int> onConfirm, Action closeCallback = null)
|
|
{
|
|
eTemplate = template;
|
|
randomConfig = config;
|
|
allowTypeSelection = requireTypeSelection;
|
|
allowSkillSelection = requireSkillSelection;
|
|
initializedExternally = true;
|
|
onConfirmSelection = onConfirm;
|
|
onClosed = closeCallback;
|
|
|
|
selectedType = default;
|
|
hasSelectedType = false;
|
|
selectedSkillGroupId = 0;
|
|
selectedSkillGroupName = string.Empty;
|
|
|
|
if (eTemplate != null)
|
|
{
|
|
eTemplate.sa_skillID = 0;
|
|
}
|
|
|
|
if (!requireTypeSelection)
|
|
{
|
|
selectedType = eTemplate != null ? eTemplate.skillType : default;
|
|
hasSelectedType = true;
|
|
}
|
|
|
|
BindButtons();
|
|
BuildTypeOptions();
|
|
ApplySelectionModeState();
|
|
UpdateCurrentSelectText();
|
|
RefreshButtons();
|
|
}
|
|
|
|
private void BindButtons()
|
|
{
|
|
if (yesSelectThis != null)
|
|
{
|
|
yesSelectThis.onClick.RemoveListener(HandleConfirmClicked);
|
|
yesSelectThis.onClick.AddListener(HandleConfirmClicked);
|
|
}
|
|
|
|
if (noWaitButton != null)
|
|
{
|
|
noWaitButton.onClick.RemoveListener(HandleCancelClicked);
|
|
noWaitButton.onClick.AddListener(HandleCancelClicked);
|
|
}
|
|
}
|
|
|
|
private void BuildTypeOptions()
|
|
{
|
|
ClearSpawned(spawnedTypeItems, select_typeParent);
|
|
selectedTypeObject = null;
|
|
|
|
if (select_typePrefab == null || select_typeParent == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Array values = Enum.GetValues(typeof(equipmentSO.EquipmentSkillType));
|
|
for (int i = 0; i < values.Length; i++)
|
|
{
|
|
equipmentSO.EquipmentSkillType skillType = (equipmentSO.EquipmentSkillType)values.GetValue(i);
|
|
GameObject instance = Instantiate(select_typePrefab, select_typeParent);
|
|
spawnedTypeItems.Add(instance);
|
|
|
|
chooseTypePrefab item = instance.GetComponent<chooseTypePrefab>();
|
|
if (item != null)
|
|
{
|
|
item.Bind(skillType, HandleTypeSelected);
|
|
}
|
|
|
|
if (hasSelectedType && skillType == selectedType)
|
|
{
|
|
selectedTypeObject = instance;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void HandleTypeSelected(equipmentSO.EquipmentSkillType skillType, chooseTypePrefab source)
|
|
{
|
|
selectedType = skillType;
|
|
hasSelectedType = true;
|
|
selectedTypeObject = source != null ? source.gameObject : null;
|
|
|
|
if (eTemplate != null)
|
|
{
|
|
eTemplate.skillType = skillType;
|
|
}
|
|
|
|
if (allowSkillSelection)
|
|
{
|
|
selectedSkillGroupId = 0;
|
|
selectedSkillGroupName = string.Empty;
|
|
if (eTemplate != null)
|
|
{
|
|
eTemplate.sa_skillID = 0;
|
|
}
|
|
|
|
if (select_skill_obj != null)
|
|
{
|
|
select_skill_obj.SetActive(true);
|
|
}
|
|
|
|
BuildSkillOptions(skillType);
|
|
}
|
|
else
|
|
{
|
|
ClearSpawned(spawnedSkillItems, select_skillParent);
|
|
}
|
|
|
|
UpdateCurrentSelectText();
|
|
RefreshButtons();
|
|
}
|
|
|
|
private void BuildSkillOptions(equipmentSO.EquipmentSkillType skillType)
|
|
{
|
|
ClearSpawned(spawnedSkillItems, select_skillParent);
|
|
|
|
if (select_skillPrefab == null || select_skillParent == null || randomConfig == null || randomConfig.specialSkillPool == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < randomConfig.specialSkillPool.Count; i++)
|
|
{
|
|
equipmentRandomConfigSO.SpecialSkillPoolEntry entry = randomConfig.specialSkillPool[i];
|
|
if (entry == null || entry.skillType != skillType || entry.skillIds == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
for (int skillIndex = 0; skillIndex < entry.skillIds.Count; skillIndex++)
|
|
{
|
|
int skillGroupId = Mathf.Max(0, entry.skillIds[skillIndex]);
|
|
if (skillGroupId <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
SkillGroup group = EquipmentSkillGroupLibrary.ResolveSpecialSkillGroup(skillGroupId);
|
|
if (group == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
GameObject instance = Instantiate(select_skillPrefab, select_skillParent);
|
|
spawnedSkillItems.Add(instance);
|
|
|
|
chooseSkillPrefab item = instance.GetComponent<chooseSkillPrefab>();
|
|
if (item != null)
|
|
{
|
|
item.Bind(skillGroupId, group.groupName, group.groupIcon, group.skillDescriptionsText, HandleSkillSelected);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleSkillSelected(int skillGroupId)
|
|
{
|
|
selectedSkillGroupId = Mathf.Max(0, skillGroupId);
|
|
selectedSkillGroupName = ResolveSkillGroupName(selectedSkillGroupId);
|
|
|
|
if (eTemplate != null)
|
|
{
|
|
eTemplate.skillType = selectedType;
|
|
eTemplate.sa_skillID = selectedSkillGroupId;
|
|
}
|
|
|
|
UpdateCurrentSelectText();
|
|
RefreshButtons();
|
|
}
|
|
|
|
private void ApplySelectionModeState()
|
|
{
|
|
if (select_type_obj != null)
|
|
{
|
|
select_type_obj.SetActive(allowTypeSelection);
|
|
}
|
|
|
|
if (select_skill_obj != null)
|
|
{
|
|
bool shouldShowSkillSelector = allowSkillSelection && (!allowTypeSelection || hasSelectedType);
|
|
select_skill_obj.SetActive(shouldShowSkillSelector);
|
|
}
|
|
|
|
if (allowSkillSelection && select_skill_obj != null && select_skill_obj.activeSelf)
|
|
{
|
|
BuildSkillOptions(selectedType);
|
|
}
|
|
else if (!allowSkillSelection)
|
|
{
|
|
ClearSpawned(spawnedSkillItems, select_skillParent);
|
|
}
|
|
}
|
|
|
|
private void RefreshButtons()
|
|
{
|
|
if (yesSelectThis != null)
|
|
{
|
|
yesSelectThis.interactable = IsSelectionValid();
|
|
}
|
|
}
|
|
|
|
private bool IsSelectionValid()
|
|
{
|
|
if (allowTypeSelection && !hasSelectedType)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (allowSkillSelection && selectedSkillGroupId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void UpdateCurrentSelectText()
|
|
{
|
|
if (currentSelectText == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!hasSelectedType)
|
|
{
|
|
currentSelectText.text = "当前未选择装备类型";
|
|
return;
|
|
}
|
|
|
|
if (allowSkillSelection && selectedSkillGroupId > 0 && !string.IsNullOrWhiteSpace(selectedSkillGroupName))
|
|
{
|
|
currentSelectText.text = $"当前选择 <b>{selectedType}</b> 装备,必定带有 <b>{selectedSkillGroupName}</b> 技能";
|
|
return;
|
|
}
|
|
|
|
currentSelectText.text = $"当前选择 <b>{selectedType}</b> 装备";
|
|
}
|
|
|
|
private void HandleConfirmClicked()
|
|
{
|
|
if (!IsSelectionValid())
|
|
{
|
|
return;
|
|
}
|
|
|
|
onConfirmSelection?.Invoke(selectedType, selectedSkillGroupId);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void HandleCancelClicked()
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (eTemplate != null && (eTemplate.hideFlags & HideFlags.DontSave) != 0)
|
|
{
|
|
Destroy(eTemplate);
|
|
}
|
|
|
|
onClosed?.Invoke();
|
|
}
|
|
|
|
private static string ResolveSkillGroupName(int skillGroupId)
|
|
{
|
|
if (skillGroupId <= 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
SkillGroup group = EquipmentSkillGroupLibrary.ResolveSpecialSkillGroup(skillGroupId);
|
|
return group != null ? group.groupName : string.Empty;
|
|
}
|
|
|
|
private static void ClearSpawned(List<GameObject> spawned, Transform parent)
|
|
{
|
|
if (parent != null)
|
|
{
|
|
for (int i = parent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Transform child = parent.GetChild(i);
|
|
if (child != null)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (spawned != null)
|
|
{
|
|
spawned.Clear();
|
|
}
|
|
}
|
|
}
|