107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using GeometryTD.Definition;
|
|
using GeometryTD.Procedure;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class EventFormUseCase : IUIUseCase
|
|
{
|
|
private EventItem _currentEvent;
|
|
private RunNodeExecutionContext _currentContext;
|
|
private readonly EventOptionExecutor _executor = new EventOptionExecutor();
|
|
|
|
public void BindEvent(
|
|
EventItem eventItem,
|
|
RunNodeExecutionContext context)
|
|
{
|
|
_currentEvent = eventItem;
|
|
_currentContext = context != null ? context.Clone() : null;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_currentEvent = null;
|
|
_currentContext = null;
|
|
}
|
|
|
|
public EventFormRawData CreateInitialModel()
|
|
{
|
|
if (_currentEvent == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new EventFormRawData
|
|
{
|
|
EventId = _currentEvent.Id,
|
|
Title = _currentEvent.Title,
|
|
Description = _currentEvent.Description,
|
|
OptionItems = BuildOptionItems()
|
|
};
|
|
}
|
|
|
|
public bool TrySelectOption(int optionIndex)
|
|
{
|
|
if (_currentEvent == null || _currentEvent.Options == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (optionIndex < 0 || optionIndex >= _currentEvent.Options.Length)
|
|
{
|
|
Log.Warning("EventFormUseCase.TrySelectOption() option index is invalid: {0}", optionIndex);
|
|
return false;
|
|
}
|
|
|
|
BackpackInventoryData workingInventory = GameEntry.PlayerInventory != null
|
|
? GameEntry.PlayerInventory.GetInventorySnapshot()
|
|
: new BackpackInventoryData();
|
|
EventOptionExecutionResult executionResult = _executor.Execute(
|
|
_currentEvent,
|
|
optionIndex,
|
|
_currentContext,
|
|
workingInventory);
|
|
if (!executionResult.IsAccepted)
|
|
{
|
|
Log.Warning(
|
|
"EventFormUseCase.TrySelectOption() rejected. EventId={0}, OptionIndex={1}, Reason={2}",
|
|
_currentEvent.Id,
|
|
optionIndex,
|
|
executionResult.FailureReason);
|
|
return false;
|
|
}
|
|
|
|
if (GameEntry.PlayerInventory != null)
|
|
{
|
|
GameEntry.PlayerInventory.ReplaceInventorySnapshot(workingInventory);
|
|
}
|
|
|
|
GameEntry.EventNode.EndEvent();
|
|
return true;
|
|
}
|
|
|
|
private EventOptionRawData[] BuildOptionItems()
|
|
{
|
|
EventOption[] options = _currentEvent.Options ?? System.Array.Empty<EventOption>();
|
|
EventOptionRawData[] result = new EventOptionRawData[options.Length];
|
|
BackpackInventoryData inventory = GameEntry.PlayerInventory != null
|
|
? GameEntry.PlayerInventory.GetInventorySnapshot()
|
|
: new BackpackInventoryData();
|
|
for (int i = 0; i < options.Length; i++)
|
|
{
|
|
EventOption option = options[i];
|
|
EventOptionAvailability availability = _executor.EvaluateOption(option, inventory);
|
|
result[i] = new EventOptionRawData
|
|
{
|
|
OptionIndex = i,
|
|
OptionText = option?.OptionText ?? string.Empty,
|
|
IsSelectable = availability.IsSelectable,
|
|
BlockedReason = availability.BlockedReason
|
|
};
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|