refactor 7:
- 拆分 CombatScheduler,将公用方法与字段拆分为:
- FlowCoordinator
- RuntimeContext
- 地图侧现在收口为“MapData 初始快照 + CombatCoinChangedEventArgs 同步 + 独立命令桥接”
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using GameFramework.Event;
|
||||
using GeometryTD.CustomEvent;
|
||||
using GeometryTD.Entity.EntityData;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.Map
|
||||
{
|
||||
public sealed class MapCombatRuntimeBridge
|
||||
{
|
||||
private Func<int, bool> _tryConsumeCoin;
|
||||
private Action<int> _addCoin;
|
||||
private bool _isCoinEventSubscribed;
|
||||
|
||||
public int CurrentCoin { get; private set; }
|
||||
|
||||
public void Initialize(MapEntityLoadContext loadContext, string mapName)
|
||||
{
|
||||
Reset();
|
||||
|
||||
MapData mapData = loadContext?.InitialMapData;
|
||||
CurrentCoin = Mathf.Max(0, mapData != null ? mapData.InitialCoin : 0);
|
||||
_tryConsumeCoin = loadContext?.TryConsumeCoin;
|
||||
_addCoin = loadContext?.AddCoin;
|
||||
|
||||
if (_tryConsumeCoin == null || _addCoin == null)
|
||||
{
|
||||
Log.Warning(
|
||||
"Map combat runtime bridge has incomplete callbacks. Map='{0}', TryConsumeCoin={1}, AddCoin={2}.",
|
||||
mapName,
|
||||
_tryConsumeCoin != null,
|
||||
_addCoin != null);
|
||||
}
|
||||
|
||||
GameEntry.Event.Subscribe(CombatCoinChangedEventArgs.EventId, OnCombatCoinChanged);
|
||||
_isCoinEventSubscribed = true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
if (_isCoinEventSubscribed)
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(CombatCoinChangedEventArgs.EventId, OnCombatCoinChanged);
|
||||
_isCoinEventSubscribed = false;
|
||||
}
|
||||
|
||||
_tryConsumeCoin = null;
|
||||
_addCoin = null;
|
||||
CurrentCoin = 0;
|
||||
}
|
||||
|
||||
public bool TryConsumeCoin(int cost)
|
||||
{
|
||||
int requiredCoin = Mathf.Max(0, cost);
|
||||
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);
|
||||
}
|
||||
|
||||
private void OnCombatCoinChanged(object sender, GameEventArgs e)
|
||||
{
|
||||
if (e is not CombatCoinChangedEventArgs args)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CurrentCoin = Mathf.Max(0, args.CurrentCoin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8309cfa985d6427c83ac5fc2b7e92d31
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -109,9 +109,8 @@ namespace GeometryTD.Map
|
||||
}
|
||||
|
||||
float minDistance = float.MaxValue;
|
||||
for (int i = 0; i < _pathCells.Count; i++)
|
||||
foreach (var candidate in _pathCells)
|
||||
{
|
||||
Vector3Int candidate = _pathCells[i];
|
||||
float distance = (tilemap.GetCellCenterWorld(candidate) - worldPosition).sqrMagnitude;
|
||||
if (distance >= minDistance)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user