refactor 5:
- MapData.cs 现在会携带战斗初始上下文:
- InitialCoin
- 建塔 TowerStatsData 快照
- TryConsumeCoin / AddCoin 运行时回调
- CombatLoadingState.cs 负责从 CombatInRunResourceManager 读取 coin 和 build stats 快照,组装 MapData 后再交给加载链。
- CombatLoadSession.cs 改成接收外部构造好的 MapData,不再自己只塞一个 LevelId。
- CombatScheduler.cs 不再在 Start() 里直接发起地图加载,加载细节回到 CombatLoadingState。
- MapEntity.cs 已经不再直接读 GameEntry.CombatNode 的 coin / build count / build stats,也不再通过它做 coin 读写:
- 初始 coin 和 build stats 从 MapData 读取
- 后续 coin 通过 CombatCoinChangedEventArgs 同步
- 建塔/升级/拆塔时的 coin 变更通过 MapData 注入回调执行
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD.Definition;
|
||||
using GeometryTD.CustomUtility;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GeometryTD.Entity.EntityData
|
||||
@@ -7,6 +10,11 @@ namespace GeometryTD.Entity.EntityData
|
||||
public class MapData : EntityDataBase
|
||||
{
|
||||
[SerializeField] private int _levelId = 0;
|
||||
[SerializeField] private int _initialCoin = 0;
|
||||
[SerializeField] private TowerStatsData[] _buildTowerStatsSnapshot = Array.Empty<TowerStatsData>();
|
||||
|
||||
[NonSerialized] private Func<int, bool> _tryConsumeCoin;
|
||||
[NonSerialized] private Action<int> _addCoin;
|
||||
|
||||
public MapData(int entityId, int levelId, Vector3 position) : this(entityId, 0, levelId, position)
|
||||
{
|
||||
@@ -18,10 +26,110 @@ namespace GeometryTD.Entity.EntityData
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public MapData(
|
||||
int entityId,
|
||||
int typeId,
|
||||
int levelId,
|
||||
Vector3 position,
|
||||
int initialCoin,
|
||||
IReadOnlyList<TowerStatsData> buildTowerStatsSnapshot,
|
||||
Func<int, bool> tryConsumeCoin,
|
||||
Action<int> addCoin) : base(entityId, typeId)
|
||||
{
|
||||
_levelId = levelId;
|
||||
Position = position;
|
||||
_initialCoin = Mathf.Max(0, initialCoin);
|
||||
SetBuildTowerStatsSnapshot(buildTowerStatsSnapshot);
|
||||
_tryConsumeCoin = tryConsumeCoin;
|
||||
_addCoin = addCoin;
|
||||
}
|
||||
|
||||
public int LevelId
|
||||
{
|
||||
get => _levelId;
|
||||
set => _levelId = value;
|
||||
}
|
||||
|
||||
public int InitialCoin
|
||||
{
|
||||
get => _initialCoin;
|
||||
set => _initialCoin = Mathf.Max(0, value);
|
||||
}
|
||||
|
||||
public int CurrentBuildTowerCount => _buildTowerStatsSnapshot != null ? _buildTowerStatsSnapshot.Length : 0;
|
||||
|
||||
public void BindCombatCallbacks(Func<int, bool> tryConsumeCoin, Action<int> addCoin)
|
||||
{
|
||||
_tryConsumeCoin = tryConsumeCoin;
|
||||
_addCoin = addCoin;
|
||||
}
|
||||
|
||||
public bool TryConsumeCoin(int coin)
|
||||
{
|
||||
int requiredCoin = Mathf.Max(0, coin);
|
||||
if (requiredCoin <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return _tryConsumeCoin != null && _tryConsumeCoin.Invoke(requiredCoin);
|
||||
}
|
||||
|
||||
public void AddCoin(int coin)
|
||||
{
|
||||
int amount = Mathf.Max(0, coin);
|
||||
if (amount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_addCoin?.Invoke(amount);
|
||||
}
|
||||
|
||||
public bool TryGetBuildTowerStats(int buildIndex, out TowerStatsData stats)
|
||||
{
|
||||
stats = null;
|
||||
if (_buildTowerStatsSnapshot == null || buildIndex < 0 || buildIndex >= _buildTowerStatsSnapshot.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
TowerStatsData sourceStats = _buildTowerStatsSnapshot[buildIndex];
|
||||
if (sourceStats == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
stats = InventoryCloneUtility.CloneTowerStats(sourceStats);
|
||||
return stats != null;
|
||||
}
|
||||
|
||||
public void SetBuildTowerStatsSnapshot(IReadOnlyList<TowerStatsData> buildTowerStatsSnapshot)
|
||||
{
|
||||
if (buildTowerStatsSnapshot == null || buildTowerStatsSnapshot.Count <= 0)
|
||||
{
|
||||
_buildTowerStatsSnapshot = Array.Empty<TowerStatsData>();
|
||||
return;
|
||||
}
|
||||
|
||||
_buildTowerStatsSnapshot = new TowerStatsData[buildTowerStatsSnapshot.Count];
|
||||
for (int i = 0; i < buildTowerStatsSnapshot.Count; i++)
|
||||
{
|
||||
_buildTowerStatsSnapshot[i] = InventoryCloneUtility.CloneTowerStats(buildTowerStatsSnapshot[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public MapData CloneForEntity(int entityId, Vector3 position)
|
||||
{
|
||||
return new MapData(
|
||||
entityId,
|
||||
TypeId,
|
||||
_levelId,
|
||||
position,
|
||||
_initialCoin,
|
||||
_buildTowerStatsSnapshot,
|
||||
_tryConsumeCoin,
|
||||
_addCoin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user