79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using GeometryTD.UI;
|
|
using UnityGameFramework.Runtime;
|
|
using GeometryTD.CustomEvent;
|
|
using GeometryTD.Definition;
|
|
using GeometryTD.Procedure;
|
|
|
|
namespace GeometryTD.CustomComponent
|
|
{
|
|
public class ShopNodeComponent : GameFrameworkComponent
|
|
{
|
|
private RunNodeExecutionContext _activeContext;
|
|
|
|
private ShopFormUseCase _shopFormUseCase;
|
|
private bool _initialized;
|
|
|
|
public void OnInit()
|
|
{
|
|
if (_initialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_shopFormUseCase ??= new ShopFormUseCase();
|
|
GameEntry.UIRouter.BindUIUseCase(UIFormType.ShopForm, _shopFormUseCase);
|
|
_initialized = true;
|
|
}
|
|
|
|
public void StartShop(RunNodeExecutionContext context = null)
|
|
{
|
|
if (!_initialized)
|
|
{
|
|
OnInit();
|
|
}
|
|
|
|
int runSeed = context?.RunSeed ?? 0;
|
|
int sequenceIndex = context?.SequenceIndex ?? -1;
|
|
if (_shopFormUseCase == null || !_shopFormUseCase.PrepareForOpen(runSeed, sequenceIndex))
|
|
{
|
|
Log.Warning("ShopNodeComponent.StartShop() failed. Shop use case is unavailable or goods generation failed.");
|
|
return;
|
|
}
|
|
|
|
_activeContext = context != null ? context.Clone() : null;
|
|
GameEntry.UIRouter.OpenUI(UIFormType.ShopForm);
|
|
GameEntry.Event.Fire(
|
|
this,
|
|
NodeEnterEventArgs.Create(
|
|
_activeContext?.RunId,
|
|
_activeContext?.NodeId ?? 0,
|
|
_activeContext?.NodeType ?? RunNodeType.None,
|
|
_activeContext?.SequenceIndex ?? -1));
|
|
}
|
|
|
|
public void EndShop()
|
|
{
|
|
GameEntry.UIRouter.CloseUI(UIFormType.ShopForm);
|
|
GameEntry.Event.Fire(
|
|
this,
|
|
NodeCompleteEventArgs.Create(
|
|
_activeContext?.RunId,
|
|
_activeContext?.NodeId ?? 0,
|
|
_activeContext?.NodeType ?? RunNodeType.None,
|
|
_activeContext?.SequenceIndex ?? -1,
|
|
RunNodeCompletionStatus.Completed,
|
|
true,
|
|
_activeContext != null
|
|
? _activeContext.CreateCompletionSnapshot(
|
|
GameEntry.PlayerInventory != null ? GameEntry.PlayerInventory.GetInventorySnapshot() : null)
|
|
: null));
|
|
ClearActiveNodeContext();
|
|
}
|
|
|
|
private void ClearActiveNodeContext()
|
|
{
|
|
_activeContext = null;
|
|
}
|
|
}
|
|
}
|