217 lines
6.4 KiB
C#
217 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class TeamManager : MonoBehaviour
|
|
{
|
|
public static TeamManager Instance { get; private set; }
|
|
|
|
public teamSettingPanel teamSettingPanelInstance;
|
|
|
|
public Dictionary<int, TeamSetting> currentTeamSettings = new Dictionary<int, TeamSetting>();
|
|
public List<CharacterView> SelectableCharacterList = new List<CharacterView>();
|
|
|
|
public int currentSelectedTeam = 0;
|
|
public int currentSelectedTeamCharacter = -1;
|
|
public CharacterView currentSelectedCharacterCard = null;
|
|
|
|
public event Action OnSelectedCharacterChanged;
|
|
public event Action OnSelectedTeamChanged;
|
|
|
|
private const int TeamCount = 4;
|
|
private const int TeamSlotCount = 5;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (teamSettingPanelInstance == null && teamSettingPanel.Instance != null)
|
|
teamSettingPanelInstance = teamSettingPanel.Instance;
|
|
|
|
EnsureDefaultTeamSettings();
|
|
}
|
|
|
|
private void EnsureDefaultTeamSettings()
|
|
{
|
|
for (int i = 0; i < TeamCount; i++)
|
|
{
|
|
if (!currentTeamSettings.ContainsKey(i) || currentTeamSettings[i] == null)
|
|
{
|
|
currentTeamSettings[i] = CreateEmptyTeamSetting();
|
|
}
|
|
|
|
EnsureTeamSlotCount(currentTeamSettings[i]);
|
|
}
|
|
}
|
|
|
|
private static TeamSetting CreateEmptyTeamSetting()
|
|
{
|
|
var list = new List<CharacterView>(TeamSlotCount);
|
|
for (int i = 0; i < TeamSlotCount; i++)
|
|
list.Add(new CharacterView(0, null));
|
|
return new TeamSetting(list, -1);
|
|
}
|
|
|
|
private static void EnsureTeamSlotCount(TeamSetting setting)
|
|
{
|
|
if (setting == null) return;
|
|
if (setting.teamIdList == null)
|
|
setting.teamIdList = new List<CharacterView>();
|
|
|
|
while (setting.teamIdList.Count < TeamSlotCount)
|
|
setting.teamIdList.Add(new CharacterView(0, null));
|
|
}
|
|
|
|
public void openTeamSettingPanel()
|
|
{
|
|
if (teamSettingPanelInstance != null)
|
|
teamSettingPanelInstance.gameObject.SetActive(true);
|
|
}
|
|
|
|
public void closeTeamSettingPanel()
|
|
{
|
|
if (teamSettingPanelInstance != null)
|
|
teamSettingPanelInstance.gameObject.SetActive(false);
|
|
}
|
|
|
|
public void setCurrentSelectedTeam(int index)
|
|
{
|
|
if (index < 0 || index >= TeamCount)
|
|
{
|
|
Debug.LogWarning($"Selected team index out of range: {index}");
|
|
return;
|
|
}
|
|
currentSelectedTeam = index;
|
|
}
|
|
|
|
public void setcurrentTeamSettings(Dictionary<int, TeamSetting> playerTeamSettings)
|
|
{
|
|
currentTeamSettings = playerTeamSettings ?? new Dictionary<int, TeamSetting>();
|
|
EnsureDefaultTeamSettings();
|
|
}
|
|
|
|
public TeamSetting getCurrentSelectedTeam()
|
|
{
|
|
EnsureDefaultTeamSettings();
|
|
return currentTeamSettings[currentSelectedTeam];
|
|
}
|
|
|
|
public void setCurrentSelectedTeam(TeamSetting newTeamData, int teamIndex)
|
|
{
|
|
if (teamIndex < 0 || teamIndex >= TeamCount || newTeamData == null)
|
|
return;
|
|
|
|
EnsureTeamSlotCount(newTeamData);
|
|
currentTeamSettings[teamIndex] = newTeamData;
|
|
}
|
|
|
|
public void setCurrentSelectedCharacterInTeam(int characterIndex)
|
|
{
|
|
if (characterIndex != currentSelectedTeamCharacter)
|
|
{
|
|
currentSelectedTeamCharacter = characterIndex;
|
|
OnSelectedCharacterChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void setCurrentSelectedTeamSetting(int teamIndex)
|
|
{
|
|
if (teamIndex < 0 || teamIndex >= TeamCount)
|
|
return;
|
|
|
|
if (teamIndex != currentSelectedTeam)
|
|
{
|
|
currentSelectedTeam = teamIndex;
|
|
OnSelectedTeamChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void swapTeamElements(int firstIndex, int secondIndex)
|
|
{
|
|
var team = getCurrentSelectedTeam()?.teamIdList;
|
|
if (team == null) return;
|
|
if (firstIndex < 0 || firstIndex >= team.Count) return;
|
|
if (secondIndex < 0 || secondIndex >= team.Count) return;
|
|
|
|
CharacterView temp = team[firstIndex];
|
|
team[firstIndex] = team[secondIndex];
|
|
team[secondIndex] = temp;
|
|
|
|
currentTeamSettings[currentSelectedTeam].teamIdList = team;
|
|
if (teamSettingPanelInstance != null)
|
|
teamSettingPanelInstance.updateTeamSettingElements();
|
|
|
|
setCurrentSelectedCharacterInTeam(secondIndex);
|
|
}
|
|
|
|
public void setTeamLeader(int characterID)
|
|
{
|
|
var team = getCurrentSelectedTeam();
|
|
if (team == null) return;
|
|
|
|
team.LeaderId = characterID;
|
|
if (teamSettingPanelInstance != null)
|
|
teamSettingPanelInstance.updateTeamSettingElements();
|
|
}
|
|
|
|
public void ResetTeamCharacter(int aimListIndex)
|
|
{
|
|
var selected = currentSelectedCharacterCard;
|
|
var team = getCurrentSelectedTeam()?.teamIdList;
|
|
if (selected == null || team == null)
|
|
return;
|
|
if (aimListIndex < 0 || aimListIndex >= team.Count)
|
|
{
|
|
Debug.LogWarning($"ResetTeamCharacter index out of range: {aimListIndex}");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < team.Count; i++)
|
|
{
|
|
if (i == aimListIndex) continue;
|
|
if (team[i] != null && team[i].characterId == selected.characterId)
|
|
{
|
|
team[i] = new CharacterView(0, null);
|
|
}
|
|
}
|
|
|
|
team[aimListIndex] = selected;
|
|
currentTeamSettings[currentSelectedTeam].teamIdList = team;
|
|
|
|
if (teamSettingPanelInstance != null)
|
|
teamSettingPanelInstance.updateTeamSettingElements();
|
|
}
|
|
|
|
public void removeTeamCharacter(int characterId)
|
|
{
|
|
currentSelectedCharacterCard = null;
|
|
var team = getCurrentSelectedTeam()?.teamIdList;
|
|
if (team == null) return;
|
|
|
|
for (int i = 0; i < team.Count; i++)
|
|
{
|
|
if (team[i] != null && team[i].characterId == characterId)
|
|
{
|
|
team[i].characterId = 0;
|
|
team[i].currentBoundaryLevel = null;
|
|
}
|
|
}
|
|
|
|
currentTeamSettings[currentSelectedTeam].teamIdList = team;
|
|
|
|
var panel = teamSettingPanelInstance ?? teamSettingPanel.Instance;
|
|
if (panel != null)
|
|
panel.updateTeamSettingElements();
|
|
}
|
|
}
|