266 lines
8.2 KiB
C#
266 lines
8.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.UIElements;
|
|
using Button = UnityEngine.UI.Button;
|
|
using Image = UnityEngine.UI.Image;
|
|
using Toggle = UnityEngine.UI.Toggle;
|
|
using ScrollView = PhxhSDK.PhxhScrollView;
|
|
using Framework;
|
|
using Framework.Wrapper;
|
|
using Gameplay;
|
|
using Gameplay.Net;
|
|
using Gameplay.Utils;
|
|
using PhxhSDK;
|
|
using TGS;
|
|
|
|
public class UI_PVPDefendPrepareController : UIWindow
|
|
{
|
|
//UI GameObj
|
|
///Auto Gen Start///
|
|
///Auto Gen End///
|
|
private ScrollView _unitScrollView;
|
|
|
|
private PrepareTeamManager _prepareTeamManager;
|
|
|
|
private List<CharacterDataInfo> _allCharacterInfos;
|
|
private List<VehicleCultivateData> _allVehicleInfos;
|
|
|
|
private List<PVPUnitInfo> _pvpUnitInfos;
|
|
private int _currSelectedIndex = -1;
|
|
|
|
// private Button btn_back;
|
|
// private Button btn_home;
|
|
|
|
public override void OnInit()
|
|
{
|
|
//bind UI GameObj
|
|
UI_PVPDefendPrepareBinder.GetComponents(this);
|
|
_prepareTeamManager = PrepareTeamManager.Instance;
|
|
BindProperty();
|
|
RegisterEvent();
|
|
}
|
|
|
|
private void BindProperty()
|
|
{
|
|
_unitScrollView = transform.Find("listBkg/Scroll View").GetComponent<ScrollView>();
|
|
_unitScrollView.SetItemCountFunc(GetUnitCount);
|
|
_unitScrollView.SetUpdateFunc(OnRenderUnitItem);
|
|
}
|
|
|
|
private void RegisterEvent()
|
|
{
|
|
BindButton("Button_Back/Button_back", OnClickBack);
|
|
BindButton("Button_Back/Button_Home", OnClickHome);
|
|
_prepareTeamManager.OnAddModel += OnAddModel;
|
|
_prepareTeamManager.OnRemoveModel += OnRemoveModel;
|
|
_prepareTeamManager.OnSwapModel += OnSwapModel;
|
|
}
|
|
|
|
private void RefreshData()
|
|
{
|
|
_allCharacterInfos = new List<CharacterDataInfo>(CharacterDataInfoManager.Instance.DataList);
|
|
_allVehicleInfos = new List<VehicleCultivateData>(VehicleCultivateDataManager.Instance.DataList);
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
RefreshData();
|
|
_unitScrollView.UpdateData();
|
|
}
|
|
|
|
private int GetUnitCount()
|
|
{
|
|
return _allCharacterInfos.Count + _allVehicleInfos.Count;
|
|
}
|
|
|
|
private async void OnRenderUnitItem(int itemIndex, RectTransform itemTrans)
|
|
{
|
|
GetUnitDataByIndex(itemIndex, out var characterData, out var vehicleData);
|
|
var icon = itemTrans.Find("icon").GetComponent<Image>();
|
|
var nameText = itemTrans.Find("name").GetComponent<TMP_Text>();
|
|
if (characterData != null)
|
|
{
|
|
icon.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(characterData.GetWholeHeadPath());
|
|
nameText.text = characterData.Name;
|
|
}
|
|
|
|
if (vehicleData != null)
|
|
{
|
|
icon.sprite =
|
|
await AssetManager.Instance.LoadAssetAsync<Sprite>(
|
|
VehicleDataHelper.GetVehiclePicPath((int)vehicleData.nID));
|
|
nameText.text = vehicleData.Name;
|
|
}
|
|
|
|
var button = itemTrans.GetComponent<Button>();
|
|
button.onClick.RemoveAllListeners();
|
|
BindButton(button, () => { OnSelectUnit(itemIndex); });
|
|
}
|
|
|
|
private bool GetIndexIsCharacter(int index, out int realIndex)
|
|
{
|
|
bool isCharacter = index < _allCharacterInfos.Count;
|
|
realIndex = isCharacter ? index : index - _allCharacterInfos.Count;
|
|
return isCharacter;
|
|
}
|
|
|
|
private string GetPrefabPathByIndex(int index)
|
|
{
|
|
var isCharacter = GetIndexIsCharacter(index, out var realIndex);
|
|
if (isCharacter)
|
|
{
|
|
var characterData = _allCharacterInfos[realIndex];
|
|
return characterData.PrefabPath;
|
|
}
|
|
else
|
|
{
|
|
var vehicleData = _allVehicleInfos[realIndex];
|
|
var prefabPath = PathEx.GetVehicleGameModelPath(vehicleData.Cfg.ID);
|
|
return prefabPath;
|
|
}
|
|
}
|
|
|
|
private string GetPrefabPathByUnitInfo(PVPUnitInfo unitInfo)
|
|
{
|
|
string result = null;
|
|
switch (unitInfo.type)
|
|
{
|
|
case Unit.UnitType.Charactor:
|
|
var characterData = CharacterDataInfoManager.Instance.GetCharacterInfo((uint)unitInfo.unitID);
|
|
if (characterData != null)
|
|
{
|
|
result = characterData.PrefabPath;
|
|
}
|
|
|
|
break;
|
|
case Unit.UnitType.Vehicle:
|
|
var vehicleData = VehicleCultivateDataManager.Instance.GetVehicleCultivateData((uint)unitInfo.unitID);
|
|
if (vehicleData != null)
|
|
{
|
|
result = PathEx.GetVehicleGameModelPath(vehicleData.Cfg.ID);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private void GetUnitDataByIndex(int itemIndex, out CharacterDataInfo characterDataInfo,
|
|
out VehicleCultivateData vehicleData)
|
|
{
|
|
characterDataInfo = null;
|
|
vehicleData = null;
|
|
bool isCharacter = GetIndexIsCharacter(itemIndex, out var realIndex);
|
|
if (isCharacter)
|
|
characterDataInfo = _allCharacterInfos[realIndex];
|
|
else
|
|
vehicleData = _allVehicleInfos[realIndex];
|
|
}
|
|
|
|
#region Event
|
|
|
|
private void OnClickBack()
|
|
{
|
|
CloseSelf();
|
|
}
|
|
|
|
private void OnClickHome()
|
|
{
|
|
CloseSelf();
|
|
}
|
|
|
|
private void OnSelectUnit(int itemIndex)
|
|
{
|
|
var prefabPath = GetPrefabPathByIndex(itemIndex);
|
|
_prepareTeamManager.CurrPrefabPath = prefabPath;
|
|
_prepareTeamManager.ShowPrepareRegion();
|
|
_currSelectedIndex = itemIndex;
|
|
}
|
|
|
|
private void OnAddModel(int cellIndex, string prefabPath)
|
|
{
|
|
if(_currSelectedIndex == -1)
|
|
return;
|
|
var unitInfo = new PVPUnitInfo();
|
|
GetUnitDataByIndex(_currSelectedIndex, out var characterDataInfo, out var vehicleData);
|
|
var type = characterDataInfo != null ? Unit.UnitType.Charactor : Unit.UnitType.Vehicle;
|
|
var unitID = characterDataInfo?.ID ?? vehicleData.nID;
|
|
unitInfo.unitID = unitID;
|
|
unitInfo.type = type;
|
|
unitInfo.cellIndex = cellIndex;
|
|
_pvpUnitInfos.Add(unitInfo);
|
|
}
|
|
|
|
private void OnRemoveModel(int cellIndex)
|
|
{
|
|
var index = _pvpUnitInfos.FindIndex(info => info.cellIndex == cellIndex);
|
|
if (index >= 0)
|
|
_pvpUnitInfos.RemoveAt(index);
|
|
}
|
|
|
|
private void OnSwapModel(int cellIndexLeft, int cellIndexRight)
|
|
{
|
|
var indexLeft = _pvpUnitInfos.FindIndex(unitInfo => unitInfo.cellIndex == cellIndexLeft);
|
|
var indexRight = _pvpUnitInfos.FindIndex(unitInfo => unitInfo.cellIndex == cellIndexRight);
|
|
var leftUnitInfo = indexLeft >= 0 ? _pvpUnitInfos[indexLeft] : null;
|
|
var rightUnitInfo = indexRight >= 0 ? _pvpUnitInfos[indexRight] : null;
|
|
if (leftUnitInfo != null)
|
|
leftUnitInfo.cellIndex = cellIndexRight;
|
|
if (rightUnitInfo != null)
|
|
rightUnitInfo.cellIndex = cellIndexLeft;
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void CloseSelf()
|
|
{
|
|
PVPManager.Instance.ExitDefendPrepare(_pvpUnitInfos);
|
|
}
|
|
|
|
private void RevertFromCurrentPrepareData()
|
|
{
|
|
RefreshData();
|
|
var savedPrepareData = Actor.Instance.Pvp.TeamData;
|
|
_pvpUnitInfos = PVPUnitInfo.DeSerialize(savedPrepareData);
|
|
Dictionary<int, string> prepareTeamData = new();
|
|
foreach (var pvpUnitInfo in _pvpUnitInfos)
|
|
{
|
|
prepareTeamData.Add(pvpUnitInfo.cellIndex, GetPrefabPathByUnitInfo(pvpUnitInfo));
|
|
}
|
|
|
|
_prepareTeamManager.RevertPreparedModels(prepareTeamData);
|
|
}
|
|
|
|
protected override void OnShowWindow(object data = null)
|
|
{
|
|
base.OnShowWindow(data);
|
|
RevertFromCurrentPrepareData();
|
|
Refresh();
|
|
}
|
|
|
|
//invoke when open window
|
|
// protected override void OnOpenWindow(object data = null)
|
|
// {
|
|
// Refresh();
|
|
// }
|
|
|
|
//invoke when reload window
|
|
protected override void OnReloadWindow(object data = null)
|
|
{
|
|
}
|
|
|
|
//invoke when close window
|
|
protected override void OnHideWindow()
|
|
{
|
|
_prepareTeamManager.OnAddModel -= OnAddModel;
|
|
_prepareTeamManager.OnRemoveModel -= OnRemoveModel;
|
|
_prepareTeamManager.OnSwapModel -= OnSwapModel;
|
|
_prepareTeamManager = null;
|
|
}
|
|
} |