Update EntityAsyncExtension.cs

为 EntityAsyncExtension 添加了便利的打开各类实体的方法
This commit is contained in:
SepComet 2026-06-06 13:40:59 +08:00
parent 788f717335
commit f896a4a9c8
1 changed files with 249 additions and 10 deletions

View File

@ -1,5 +1,12 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using SepCore.CustomUtility;
using SepCore.DataTable;
using SepCore.Definition;
using SepCore.Entity;
using SepCore.Entity.Weapon;
using UnityGameFramework.Runtime;
using HideEntityCompleteEventArgs = UnityGameFramework.Runtime.HideEntityCompleteEventArgs;
using ShowEntityFailureEventArgs = UnityGameFramework.Runtime.ShowEntityFailureEventArgs;
@ -12,6 +19,11 @@ namespace SepCore.AsyncTask
/// </summary>
public static class EntityAsyncExtension
{
private const string EntityNamespace = "SepCore.Entity.";
private static readonly Dictionary<string, Type> _typeDict = new();
private static readonly Dictionary<int, string> _assetNameDict = new();
/// <summary>
/// 异步显示实体
/// </summary>
@ -22,6 +34,7 @@ namespace SepCore.AsyncTask
/// <param name="entityGroupName">实体组名称</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>显示的实体</returns>
public static UniTask<UnityGameFramework.Runtime.Entity> ShowEntityAsync(this EntityComponent entityComponent,
int entityId,
@ -29,17 +42,46 @@ namespace SepCore.AsyncTask
string entityAssetName,
string entityGroupName,
object userData = null,
float timeout = 30f)
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityAsync(entityComponent, entityId, entityLogicType, entityAssetName, entityGroupName, 0,
userData, timeout, cancellationToken);
}
/// <summary>
/// 异步显示实体
/// </summary>
/// <param name="entityComponent">实体组件</param>
/// <param name="entityId">实体编号</param>
/// <param name="entityLogicType">实体逻辑类型</param>
/// <param name="entityAssetName">实体资源名称</param>
/// <param name="entityGroupName">实体组名称</param>
/// <param name="priority">加载优先级</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>显示的实体</returns>
public static UniTask<UnityGameFramework.Runtime.Entity> ShowEntityAsync(this EntityComponent entityComponent,
int entityId,
Type entityLogicType,
string entityAssetName,
string entityGroupName,
int priority,
object userData = null,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
UniTask<UnityGameFramework.Runtime.Entity> waitTask = AsyncTaskHelper.WaitSuccessOrFailureAsync<ShowEntitySuccessEventArgs, ShowEntityFailureEventArgs>(
ShowEntitySuccessEventArgs.EventId,
ShowEntityFailureEventArgs.EventId,
successArgs => successArgs.Entity.Id == entityId && ReferenceEquals(successArgs.UserData, userData),
failureArgs => failureArgs.EntityId == entityId && ReferenceEquals(failureArgs.UserData, userData),
timeout
timeout,
cancellationToken
).ContinueWith(successArgs => successArgs.Entity);
entityComponent.ShowEntity(entityId, entityLogicType, entityAssetName, entityGroupName, userData);
entityComponent.ShowEntity(entityId, entityLogicType, entityAssetName, entityGroupName, priority, userData);
return waitTask;
}
@ -53,26 +95,157 @@ namespace SepCore.AsyncTask
/// <param name="entityGroupName">实体组名称</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>显示的实体</returns>
public static UniTask<T> ShowEntityAsync<T>(this EntityComponent entityComponent,
int entityId,
string entityAssetName,
string entityGroupName,
object userData = null,
float timeout = 30f) where T : EntityLogic
float timeout = 30f,
CancellationToken cancellationToken = default) where T : EntityLogic
{
return ShowEntityAsync<T>(entityComponent, entityId, entityAssetName, entityGroupName, 0, userData, timeout,
cancellationToken);
}
/// <summary>
/// 异步显示实体(泛型版本)
/// </summary>
/// <typeparam name="T">实体逻辑类型</typeparam>
/// <param name="entityComponent">实体组件</param>
/// <param name="entityId">实体编号</param>
/// <param name="entityAssetName">实体资源名称</param>
/// <param name="entityGroupName">实体组名称</param>
/// <param name="priority">加载优先级</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>显示的实体</returns>
public static UniTask<T> ShowEntityAsync<T>(this EntityComponent entityComponent,
int entityId,
string entityAssetName,
string entityGroupName,
int priority,
object userData = null,
float timeout = 30f,
CancellationToken cancellationToken = default) where T : EntityLogic
{
UniTask<T> waitTask = AsyncTaskHelper.WaitSuccessOrFailureAsync<ShowEntitySuccessEventArgs, ShowEntityFailureEventArgs>(
ShowEntitySuccessEventArgs.EventId,
ShowEntityFailureEventArgs.EventId,
successArgs => successArgs.Entity.Id == entityId && ReferenceEquals(successArgs.UserData, userData),
failureArgs => failureArgs.EntityId == entityId && ReferenceEquals(failureArgs.UserData, userData),
timeout
timeout,
cancellationToken
).ContinueWith(successArgs => (T)successArgs.Entity.Logic);
entityComponent.ShowEntity<T>(entityId, entityAssetName, entityGroupName, userData);
entityComponent.ShowEntity<T>(entityId, entityAssetName, entityGroupName, priority, userData);
return waitTask;
}
/// <summary>
/// 异步显示玩家实体。
/// </summary>
public static UniTask<Player> ShowPlayerAsync(this EntityComponent entityComponent,
PlayerData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityByDataAsync<Player>(entityComponent, typeof(Player), "Player",
Constant.AssetPriority.PlayerAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示敌人实体。
/// </summary>
public static UniTask<EnemyBase> ShowEnemyAsync(this EntityComponent entityComponent,
EnemyData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
if (data == null)
{
Log.Warning("Enemy data is invalid.");
return UniTask.FromResult<EnemyBase>(null);
}
Type enemyType = TryGetType(data.EnemyType.ToString());
string assetName = TryGetAssetName(data.EntityTypeId);
if (enemyType == null || string.IsNullOrEmpty(assetName))
{
return UniTask.FromResult<EnemyBase>(null);
}
return entityComponent
.ShowEntityAsync(data.Id, enemyType, AssetUtility.GetEntityAsset(assetName), "Enemy",
Constant.AssetPriority.BulletAsset, data, timeout, cancellationToken)
.ContinueWith(entity => entity?.Logic as EnemyBase);
}
/// <summary>
/// 异步显示武器实体。
/// </summary>
public static UniTask<WeaponBase> ShowWeaponAsync(this EntityComponent entityComponent,
WeaponData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
if (data == null)
{
Log.Warning("Weapon data is invalid.");
return UniTask.FromResult<WeaponBase>(null);
}
Type weaponType = TryGetType("Weapon." + data.WeaponType);
string assetName = TryGetAssetName(data.EntityTypeId);
if (weaponType == null || string.IsNullOrEmpty(assetName))
{
return UniTask.FromResult<WeaponBase>(null);
}
return entityComponent
.ShowEntityAsync(data.Id, weaponType, AssetUtility.GetEntityAsset(assetName), "Weapon",
Constant.AssetPriority.BulletAsset, data, timeout, cancellationToken)
.ContinueWith(entity => entity?.Logic as WeaponBase);
}
/// <summary>
/// 异步显示特效实体。
/// </summary>
public static UniTask<Effect> ShowEffectAsync(this EntityComponent entityComponent,
EffectData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityByDataAsync<Effect>(entityComponent, typeof(Effect), "Effect",
Constant.AssetPriority.EffectAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示金币掉落实体。
/// </summary>
public static UniTask<CoinEntity> ShowCoinAsync(this EntityComponent entityComponent,
CoinData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityByDataAsync<CoinEntity>(entityComponent, typeof(CoinEntity), "Drop",
Constant.AssetPriority.EffectAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示经验掉落实体。
/// </summary>
public static UniTask<ExpEntity> ShowExpAsync(this EntityComponent entityComponent,
ExpData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityByDataAsync<ExpEntity>(entityComponent, typeof(ExpEntity), "Drop",
Constant.AssetPriority.EffectAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步隐藏实体
/// </summary>
@ -80,16 +253,19 @@ namespace SepCore.AsyncTask
/// <param name="entityId">实体编号</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>隐藏完成事件</returns>
public static UniTask<HideEntityCompleteEventArgs> HideEntityAsync(this EntityComponent entityComponent,
int entityId,
object userData = null,
float timeout = 30f)
float timeout = 30f,
CancellationToken cancellationToken = default)
{
UniTask<HideEntityCompleteEventArgs> waitTask = AsyncTaskHelper.WaitEventAsync<HideEntityCompleteEventArgs>(
HideEntityCompleteEventArgs.EventId,
args => args.EntityId == entityId,
timeout
timeout,
cancellationToken
);
entityComponent.HideEntity(entityId, userData);
@ -103,13 +279,76 @@ namespace SepCore.AsyncTask
/// <param name="entity">要隐藏的实体</param>
/// <param name="userData">用户自定义数据</param>
/// <param name="timeout">超时时间0表示不超时</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>隐藏完成事件</returns>
public static UniTask<HideEntityCompleteEventArgs> HideEntityAsync(this EntityComponent entityComponent,
UnityGameFramework.Runtime.Entity entity,
object userData = null,
float timeout = 30f)
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return HideEntityAsync(entityComponent, entity.Id, userData, timeout);
return HideEntityAsync(entityComponent, entity.Id, userData, timeout, cancellationToken);
}
private static UniTask<T> ShowEntityByDataAsync<T>(EntityComponent entityComponent,
Type logicType,
string entityGroupName,
int priority,
EntityDataBase data,
float timeout,
CancellationToken cancellationToken) where T : EntityLogic
{
if (data == null)
{
Log.Warning("Data is invalid.");
return UniTask.FromResult<T>(null);
}
DREntity drEntity = GameEntry.DataTable.GetDataTableRow<DREntity>(data.TypeId);
if (drEntity == null)
{
Log.Warning("Can not load entity id '{0}' from data table.", data.TypeId.ToString());
return UniTask.FromResult<T>(null);
}
return entityComponent.ShowEntityAsync<T>(data.Id, AssetUtility.GetEntityAsset(drEntity.AssetName),
entityGroupName, priority, data, timeout, cancellationToken);
}
private static Type TryGetType(string rawTypeName)
{
string typeName = EntityNamespace + rawTypeName;
if (!_typeDict.TryGetValue(typeName, out Type type))
{
type = Type.GetType(typeName);
if (type == null)
{
Log.Warning("Can not load entity type '{0}'.", typeName);
return null;
}
_typeDict.Add(typeName, type);
}
return type;
}
private static string TryGetAssetName(int entityId)
{
if (!_assetNameDict.TryGetValue(entityId, out string assetName))
{
DREntity drEntity = GameEntry.DataTable.GetDataTableRow<DREntity>(entityId);
if (drEntity == null)
{
Log.Warning("Can not load entity id '{0}' from data table.", entityId.ToString());
return null;
}
assetName = drEntity.AssetName;
_assetNameDict.Add(entityId, assetName);
}
return assetName;
}
}
}