using System.Collections.Generic; using GeometryTD.Definition; using GeometryTD.Entity.EntityData; using GeometryTD.UI; using UnityEngine; namespace GeometryTD.CustomComponent { internal sealed class CombatLoadingState : CombatStateBase { public CombatLoadingState(CombatSchedulerRuntimeContext context, CombatSchedulerFlowCoordinator flow) : base(context, flow) { } public override void OnEnter() { if (Context.CurrentLevel == null) { Flow.EnterFailureFallback("Combat loading failed. Current level is null."); return; } MapEntityLoadContext mapLoadContext = BuildMapLoadContext(); if (!Context.LoadSession.StartLoading(Context.CurrentLevel, mapLoadContext, Flow.Host, out string errorMessage)) { Flow.EnterFailureFallback($"Combat loading failed. {errorMessage}"); } } public override void OnUpdate(float elapseSeconds, float realElapseSeconds) { _ = elapseSeconds; _ = realElapseSeconds; if (!Context.LoadSession.IsReady) { return; } Flow.TryBeginNextPhase(); } private MapEntityLoadContext BuildMapLoadContext() { List buildTowerStatsSnapshot = new(); for (int i = 0; i < Context.CombatInRunResourceManager.CurrentBuildTowerCount; i++) { if (Context.CombatInRunResourceManager.TryGetBuildTowerStats(i, out TowerStatsData stats) && stats != null) { buildTowerStatsSnapshot.Add(stats); } } MapData mapData = new MapData( entityId: 0, typeId: 0, levelId: Context.CurrentLevel.Id, position: Vector3.zero, initialCoin: Context.CombatInRunResourceManager.CurrentCoin, buildTowerStatsSnapshot: buildTowerStatsSnapshot, inventorySnapshot: Context.CombatInRunResourceManager.GetCombatInventorySnapshot(), participantTowerSnapshot: Context.CombatInRunResourceManager.GetParticipantTowerSnapshot()); return new MapEntityLoadContext( mapData, Context.CombatInRunResourceManager.TryConsumeCoin, Context.CombatInRunResourceManager.AddCoin); } } }