using UnityEngine;
using System;
using System.Collections.Generic;
public class BattlePreloadManager
{
private BattleSpineResLoader spineLoader = new BattleSpineResLoader();
private BattleAudioResLoader audioLoader = new BattleAudioResLoader();
private BattleCacheManager cacheManager = new BattleCacheManager();
private BattleUnloadManager unloadManager = new BattleUnloadManager();
private bool isLoading = false;
public BattleCacheManager CacheManager => cacheManager;
public BattleUnloadManager UnloadManager => unloadManager;
///
/// 预加载战斗资源
///
public void PreloadBattleResources(string battleGuid, List redTeamList, List blueTeamList,
Action progressCallback, Action completeCallback)
{
if (isLoading)
{
Debug.LogWarning("BattlePreloadManager: Already loading, ignoring request");
return;
}
isLoading = true;
var redTeamInfo = AnalyzeTeamList(redTeamList, true);
var blueTeamInfo = AnalyzeTeamList(blueTeamList, false);
// ===== 新增:注册战场红队资源需求 =====
cacheManager.RegisterBattlefieldRedTeam(battleGuid, redTeamInfo.SpineResources, redTeamInfo.AudioResources);
StartPreload(redTeamInfo, blueTeamInfo, battleGuid, progressCallback, () =>
{
isLoading = false;
completeCallback?.Invoke();
});
}
private TeamResTracker.TeamResourceInfo AnalyzeTeamList(List teamList, bool isPersistent)
{
var combinedInfo = new TeamResTracker.TeamResourceInfo();
if (teamList == null || teamList.Count == 0)
{
return combinedInfo;
}
foreach (var team in teamList)
{
if (team == null)
continue;
var teamInfo = TeamResTracker.AnalyzeTeam(team, isPersistent);
MergeResourceInfo(combinedInfo, teamInfo);
}
return combinedInfo;
}
private void MergeResourceInfo(TeamResTracker.TeamResourceInfo target, TeamResTracker.TeamResourceInfo source)
{
// 合并Spine资源(去重)
foreach (var res in source.SpineResources)
{
if (!ContainsResource(target.SpineResources, res))
{
target.SpineResources.Add(res);
}
}
// 合并音频资源(去重)
foreach (var res in source.AudioResources)
{
if (!ContainsResource(target.AudioResources, res))
{
target.AudioResources.Add(res);
}
}
}
private bool ContainsResource(List list, BattleResCache.ResourceIdentifier resource)
{
foreach (var item in list)
{
if (item.GetKey() == resource.GetKey())
{
return true;
}
}
return false;
}
private void StartPreload(TeamResTracker.TeamResourceInfo redInfo, TeamResTracker.TeamResourceInfo blueInfo,
string battleGuid,
Action progressCallback, Action completeCallback)
{
int totalResources = redInfo.GetTotalCount() + blueInfo.GetTotalCount();
if (totalResources == 0)
{
Debug.Log("BattlePreloadManager: No resources to preload");
completeCallback?.Invoke();
return;
}
Debug.Log($"BattlePreloadManager: Preloading {totalResources} resources for battlefield {battleGuid}");
Debug.Log($" Red: Spine={redInfo.SpineResources.Count}, Audio={redInfo.AudioResources.Count}");
Debug.Log($" Blue: Spine={blueInfo.SpineResources.Count}, Audio={blueInfo.AudioResources.Count}");
int completedPhases = 0;
int totalPhases = 4;
Action onPhaseComplete = () =>
{
completedPhases++;
float progress = (float)completedPhases / totalPhases;
progressCallback?.Invoke(progress);
if (completedPhases >= totalPhases)
{
Debug.Log($"BattlePreloadManager: Completed! {cacheManager.GetCacheStats(battleGuid)}");
completeCallback?.Invoke();
}
};
// 并行加载4个阶段(传入cacheManager和是否为红队标识)
spineLoader.LoadSpineResourcesAsync(
redInfo.SpineResources,
cacheManager.GetSpineCache(true, battleGuid),
null,
onPhaseComplete,
cacheManager, // ← 传入管理器
true // ← 是红队
);
audioLoader.LoadAudioResourcesAsync(
redInfo.AudioResources,
cacheManager.GetAudioCache(true, battleGuid),
null,
onPhaseComplete,
cacheManager, // ← 传入管理器
true // ← 是红队
);
spineLoader.LoadSpineResourcesAsync(
blueInfo.SpineResources,
cacheManager.GetSpineCache(false, battleGuid),
null,
onPhaseComplete,
null, // ← 蓝队不需要引用追踪
false // ← 不是红队
);
audioLoader.LoadAudioResourcesAsync(
blueInfo.AudioResources,
cacheManager.GetAudioCache(false, battleGuid),
null,
onPhaseComplete,
null, // ← 蓝队不需要引用追踪
false // ← 不是红队
);
}
}