361 lines
12 KiB
C#
361 lines
12 KiB
C#
using System.Collections.Generic;
|
|
using GameFramework.Event;
|
|
using GeometryTD.CustomEvent;
|
|
using GeometryTD.CustomUtility;
|
|
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class CombatFinishFormController : UIFormControllerCommonBase<CombatFinishFormContext, CombatFinishForm>
|
|
{
|
|
private CombatFinishFormUseCase _useCase;
|
|
private readonly Dictionary<long, ItemDescSeed> _itemDescSeedMap = new Dictionary<long, ItemDescSeed>();
|
|
|
|
private sealed class ItemDescSeed
|
|
{
|
|
public string Title;
|
|
public string TypeText;
|
|
public string Description;
|
|
public TagType[] Tags;
|
|
}
|
|
|
|
protected override UIFormType UIFormTypeId => UIFormType.CombatFinishForm;
|
|
|
|
protected override void RefreshUI(CombatFinishForm form, CombatFinishFormContext context)
|
|
{
|
|
form.RefreshUI(context);
|
|
}
|
|
|
|
protected override void SubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Subscribe(CombatFinishReturnEventArgs.EventId, OnCombatFinishReturnButtonClicked);
|
|
GameEntry.Event.Subscribe(RepoItemDetailRequestedEventArgs.EventId, OnRepoItemDetailRequested);
|
|
}
|
|
|
|
protected override void UnsubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Unsubscribe(CombatFinishReturnEventArgs.EventId, OnCombatFinishReturnButtonClicked);
|
|
GameEntry.Event.Unsubscribe(RepoItemDetailRequestedEventArgs.EventId, OnRepoItemDetailRequested);
|
|
}
|
|
|
|
public override int? OpenUI(object userData = null)
|
|
{
|
|
if (userData is CombatFinishFormContext context)
|
|
{
|
|
return OpenUIInternal(context);
|
|
}
|
|
|
|
if (userData is CombatFinishFormRawData rawDataFromUserData)
|
|
{
|
|
return OpenUI(rawDataFromUserData);
|
|
}
|
|
|
|
if (userData != null)
|
|
{
|
|
Log.Warning("CombatFinishFormController.OpenUI() userData type is invalid.");
|
|
return null;
|
|
}
|
|
|
|
if (_useCase == null)
|
|
{
|
|
Log.Error("CombatFinishFormController.OpenUI() useCase is null.");
|
|
return null;
|
|
}
|
|
|
|
CombatFinishFormRawData rawData = _useCase.CreateInitialModel();
|
|
return OpenUI(rawData);
|
|
}
|
|
|
|
public int? OpenUI(CombatFinishFormRawData rawData)
|
|
{
|
|
CombatFinishFormContext context = BuildContext(rawData);
|
|
return OpenUIInternal(context);
|
|
}
|
|
|
|
public override void BindUseCase(IUIUseCase useCase)
|
|
{
|
|
if (!(useCase is CombatFinishFormUseCase combatFinishFormUseCase))
|
|
{
|
|
Log.Error("CombatFinishFormController.BindUseCase() useCase is invalid.");
|
|
return;
|
|
}
|
|
|
|
_useCase = combatFinishFormUseCase;
|
|
}
|
|
|
|
private CombatFinishFormContext BuildContext(CombatFinishFormRawData rawData)
|
|
{
|
|
_itemDescSeedMap.Clear();
|
|
if (rawData == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new CombatFinishFormContext
|
|
{
|
|
EnemyKilledText = rawData.DefeatedEnemyCount.ToString(),
|
|
GoldGainedText = rawData.GainedGold.ToString(),
|
|
RewardItems = BuildRewardItems(rawData.RewardInventory),
|
|
CanReturn = rawData.CanReturn
|
|
};
|
|
}
|
|
|
|
private RepoItemContext[] BuildRewardItems(BackpackInventoryData inventory)
|
|
{
|
|
if (inventory == null)
|
|
{
|
|
return System.Array.Empty<RepoItemContext>();
|
|
}
|
|
|
|
List<RepoItemContext> itemContexts = new List<RepoItemContext>();
|
|
|
|
if (inventory.Towers != null)
|
|
{
|
|
for (int i = 0; i < inventory.Towers.Count; i++)
|
|
{
|
|
var tower = inventory.Towers[i];
|
|
if (tower == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
itemContexts.Add(new RepoItemContext
|
|
{
|
|
InstanceId = tower.InstanceId,
|
|
CanDrag = false,
|
|
ComponentSlotType = TowerCompSlotType.None,
|
|
IconAreaContext = BuildIconAreaContext(tower)
|
|
});
|
|
|
|
AddItemDescSeed(
|
|
tower.InstanceId,
|
|
tower.Name,
|
|
"Tower",
|
|
ItemDescUtility.BuildTowerDesc(tower.Stats) ?? string.Empty,
|
|
tower.Stats != null ? tower.Stats.Tags : null);
|
|
}
|
|
}
|
|
|
|
if (inventory.MuzzleComponents != null)
|
|
{
|
|
for (int i = 0; i < inventory.MuzzleComponents.Count; i++)
|
|
{
|
|
var item = inventory.MuzzleComponents[i];
|
|
if (item == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
itemContexts.Add(new RepoItemContext
|
|
{
|
|
InstanceId = item.InstanceId,
|
|
CanDrag = false,
|
|
ComponentSlotType = TowerCompSlotType.Muzzle,
|
|
IconAreaContext = BuildIconAreaContext(item)
|
|
});
|
|
|
|
AddItemDescSeed(
|
|
item.InstanceId,
|
|
item.Name,
|
|
BuildComponentTypeText(item.SlotType),
|
|
ItemDescUtility.BuildMuzzleDesc(item) ?? string.Empty,
|
|
item.Tags);
|
|
}
|
|
}
|
|
|
|
if (inventory.BearingComponents != null)
|
|
{
|
|
foreach (var item in inventory.BearingComponents)
|
|
{
|
|
if (item == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
itemContexts.Add(new RepoItemContext
|
|
{
|
|
InstanceId = item.InstanceId,
|
|
CanDrag = false,
|
|
ComponentSlotType = TowerCompSlotType.Bearing,
|
|
IconAreaContext = BuildIconAreaContext(item)
|
|
});
|
|
|
|
AddItemDescSeed(
|
|
item.InstanceId,
|
|
item.Name,
|
|
BuildComponentTypeText(item.SlotType),
|
|
ItemDescUtility.BuildBearingDesc(item) ?? string.Empty,
|
|
item.Tags);
|
|
}
|
|
}
|
|
|
|
if (inventory.BaseComponents != null)
|
|
{
|
|
for (int i = 0; i < inventory.BaseComponents.Count; i++)
|
|
{
|
|
var item = inventory.BaseComponents[i];
|
|
if (item == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
itemContexts.Add(new RepoItemContext
|
|
{
|
|
InstanceId = item.InstanceId,
|
|
CanDrag = false,
|
|
ComponentSlotType = TowerCompSlotType.Base,
|
|
IconAreaContext = BuildIconAreaContext(item)
|
|
});
|
|
|
|
AddItemDescSeed(
|
|
item.InstanceId,
|
|
item.Name,
|
|
BuildComponentTypeText(item.SlotType),
|
|
ItemDescUtility.BuildBaseDesc(item) ?? string.Empty,
|
|
item.Tags);
|
|
}
|
|
}
|
|
|
|
return itemContexts.ToArray();
|
|
}
|
|
|
|
private void AddItemDescSeed(long itemId, string title, string typeText, string description, TagType[] tags)
|
|
{
|
|
if (itemId <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_itemDescSeedMap[itemId] = new ItemDescSeed
|
|
{
|
|
Title = string.IsNullOrWhiteSpace(title) ? $"Item {itemId}" : title,
|
|
TypeText = typeText ?? string.Empty,
|
|
Description = description ?? string.Empty,
|
|
Tags = CloneTags(tags)
|
|
};
|
|
}
|
|
|
|
private static TagType[] CloneTags(TagType[] tags)
|
|
{
|
|
return tags != null ? (TagType[])tags.Clone() : System.Array.Empty<TagType>();
|
|
}
|
|
|
|
private static string BuildComponentTypeText(TowerCompSlotType slotType)
|
|
{
|
|
return slotType switch
|
|
{
|
|
TowerCompSlotType.Muzzle => "Muzzle Component",
|
|
TowerCompSlotType.Bearing => "Bearing Component",
|
|
TowerCompSlotType.Base => "Base Component",
|
|
TowerCompSlotType.Accessory => "Accessory",
|
|
_ => "Component"
|
|
};
|
|
}
|
|
|
|
private static IconAreaContext BuildIconAreaContext(TowerItemData tower)
|
|
{
|
|
if (tower == null)
|
|
{
|
|
return new IconAreaContext
|
|
{
|
|
ComponentSlotType = TowerCompSlotType.None,
|
|
Rarity = RarityType.None,
|
|
Color = Color.white,
|
|
Icon = null
|
|
};
|
|
}
|
|
|
|
return new IconAreaContext
|
|
{
|
|
ComponentSlotType = TowerCompSlotType.None,
|
|
Rarity = tower.Rarity,
|
|
Color = IconColorGenerator.GenerateForTower(tower),
|
|
Icon = null
|
|
};
|
|
}
|
|
|
|
private static IconAreaContext BuildIconAreaContext(TowerCompItemData item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
return new IconAreaContext
|
|
{
|
|
ComponentSlotType = TowerCompSlotType.None,
|
|
Rarity = RarityType.None,
|
|
Color = Color.white,
|
|
Icon = null
|
|
};
|
|
}
|
|
|
|
return new IconAreaContext
|
|
{
|
|
ComponentSlotType = item.SlotType,
|
|
Rarity = item.Rarity,
|
|
Color = IconColorGenerator.GenerateForComponent(item),
|
|
Icon = null
|
|
};
|
|
}
|
|
|
|
private void OnCombatFinishReturnButtonClicked(object sender, GameEventArgs e)
|
|
{
|
|
if (!IsEventFromCurrentForm(sender) || !(e is CombatFinishReturnEventArgs))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_useCase?.TryReturnToMenu();
|
|
}
|
|
|
|
private void OnRepoItemDetailRequested(object sender, GameEventArgs e)
|
|
{
|
|
if (!IsEventFromCurrentForm(sender))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!(e is RepoItemDetailRequestedEventArgs args))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!_itemDescSeedMap.TryGetValue(args.ItemId, out ItemDescSeed seed))
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.UIRouter.OpenUI(UIFormType.ItemDescForm, new ItemDescFormRawData
|
|
{
|
|
Title = seed.Title,
|
|
TypeText = seed.TypeText,
|
|
Description = seed.Description ?? string.Empty,
|
|
Price = 0,
|
|
TargetPos = args.TargetPos,
|
|
Tags = CloneTags(seed.Tags)
|
|
});
|
|
}
|
|
|
|
private bool IsEventFromCurrentForm(object sender)
|
|
{
|
|
if (Form == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (ReferenceEquals(sender, Form))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (sender is Component component)
|
|
{
|
|
CombatFinishForm ownerForm = component.GetComponentInParent<CombatFinishForm>();
|
|
return ownerForm == Form;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|