Files
vampire-like/Assets/GameMain/Scripts/Runtime/BuiltinComponent/AsyncTask/EntityAsyncExtension.cs
T
2026-09-15 11:07:01 +08:00

369 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using SepCore.CustomUtility;
using SepCore.Definition;
using SepCore.Entity;
using UnityEngine;
using UnityGameFramework.Runtime;
using HideEntityCompleteEventArgs = UnityGameFramework.Runtime.HideEntityCompleteEventArgs;
using ShowEntityFailureEventArgs = UnityGameFramework.Runtime.ShowEntityFailureEventArgs;
using ShowEntitySuccessEventArgs = UnityGameFramework.Runtime.ShowEntitySuccessEventArgs;
namespace SepCore.AsyncTask
{
/// <summary>
/// Entity 异步扩展方法
/// </summary>
public static class EntityAsyncExtension
{
private const string EntityNamespace = "SepCore.Entity.";
private static readonly Dictionary<string, Type> _typeDict = new();
/// <summary>
/// 异步显示实体
/// </summary>
/// <param name="entityComponent">实体组件</param>
/// <param name="entityId">实体编号</param>
/// <param name="entityLogicType">实体逻辑类型</param>
/// <param name="entityAssetName">实体资源名称</param>
/// <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,
Type entityLogicType,
string entityAssetName,
string entityGroupName,
object userData = null,
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,
cancellationToken
).ContinueWith(successArgs => successArgs.Entity);
entityComponent.ShowEntity(entityId, entityLogicType, entityAssetName, entityGroupName, priority, userData);
return waitTask;
}
/// <summary>
/// 异步显示实体(泛型版本)
/// </summary>
/// <typeparam name="T">实体逻辑类型</typeparam>
/// <param name="entityComponent">实体组件</param>
/// <param name="entityId">实体编号</param>
/// <param name="entityAssetName">实体资源名称</param>
/// <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,
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,
cancellationToken
).ContinueWith(successArgs => (T)successArgs.Entity.Logic);
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), GameEntry.Luban.Global.PlayerGroupName,
Constant.AssetPriority.PlayerAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示敌人实体。
/// </summary>
public static UniTask<EnemyBase> ShowEnemyAsync(this EntityComponent entityComponent,
EnemyData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
Type enemyType = GetLogicType(data.LogicType);
EntityConfig entityConfig = GameEntry.Luban.Get<EntityConfig>(data.AssetName);
return entityComponent.ShowEntityAsync(
data.Id, enemyType, AssetUtility.GetEntityAsset(entityConfig.PrefabPath), GameEntry.Luban.Global.EnemyGroupName,
Constant.AssetPriority.EnemyAsset, 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)
{
Type weaponType = GetLogicType(data.LogicType);
EntityConfig entityConfig = GameEntry.Luban.Get<EntityConfig>(data.AssetName);
return entityComponent.ShowEntityAsync(
data.Id, weaponType, AssetUtility.GetEntityAsset(entityConfig.PrefabPath), GameEntry.Luban.Global.PlayerGroupName,
Constant.AssetPriority.WeaponAsset, 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), GameEntry.Luban.Global.EffectGroupNmae,
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), GameEntry.Luban.Global.DropGroupName,
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), GameEntry.Luban.Global.DropGroupName,
Constant.AssetPriority.EffectAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示投射物实体。
/// </summary>
public static UniTask<EnemyProjectile> ShowProjectileAsync(this EntityComponent entityComponent,
EnemyProjectileData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return ShowEntityByDataAsync<EnemyProjectile>(entityComponent, typeof(EnemyProjectile),
GameEntry.Luban.Global.ProjectileGroupName,
Constant.AssetPriority.BulletAsset, data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示投射物实体(直接入参快捷方法)。
/// </summary>
public static UniTask<EnemyProjectile> ShowProjectileAsync(this EntityComponent entityComponent,
int ownerEntityId,
CampType ownerCamp,
int attackDamage,
Vector3 position,
Vector3 direction,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
int projectileEntityId = entityComponent.NextId();
float speed = GameEntry.Luban.Global.ProjectileSpeed / 1000f;
float lifeTime = GameEntry.Luban.Global.ProjectileLifeTime / 1000f;
string assetName = GameEntry.Luban.Global.ProjectileEntity;
var projectileData = new EnemyProjectileData(projectileEntityId, ownerEntityId, ownerCamp,
attackDamage, speed, lifeTime, direction, assetName)
{
Position = position,
Rotation = Quaternion.LookRotation(direction, Vector3.up)
};
return entityComponent.ShowProjectileAsync(projectileData, timeout, cancellationToken);
}
/// <summary>
/// 异步显示投射物实体。
/// </summary>
public static UniTask<EnemyProjectile> ShowProjectile(this EntityComponent entityComponent,
EnemyProjectileData data,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return entityComponent.ShowProjectileAsync(data, timeout, cancellationToken);
}
/// <summary>
/// 异步显示投射物实体(直接入参快捷方法)。
/// </summary>
public static UniTask<EnemyProjectile> ShowProjectile(this EntityComponent entityComponent,
int ownerEntityId,
CampType ownerCamp,
int attackDamage,
Vector3 position,
Vector3 direction,
float timeout = 30f,
CancellationToken cancellationToken = default)
{
return entityComponent.ShowProjectileAsync(ownerEntityId, ownerCamp, attackDamage, position, direction, timeout, cancellationToken);
}
/// <summary>
/// 异步隐藏实体
/// </summary>
/// <param name="entityComponent">实体组件</param>
/// <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,
CancellationToken cancellationToken = default)
{
UniTask<HideEntityCompleteEventArgs> waitTask = AsyncTaskHelper.WaitEventAsync<HideEntityCompleteEventArgs>(
HideEntityCompleteEventArgs.EventId,
args => args.EntityId == entityId,
timeout,
cancellationToken
);
entityComponent.HideEntity(entityId, userData);
return waitTask;
}
/// <summary>
/// 异步隐藏实体(通过实体对象)
/// </summary>
/// <param name="entityComponent">实体组件</param>
/// <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,
CancellationToken cancellationToken = default)
{
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
{
EntityConfig entityConfig = GameEntry.Luban.Get<EntityConfig>(data.AssetName);
return entityComponent.ShowEntityAsync<T>(data.Id, AssetUtility.GetEntityAsset(entityConfig.PrefabPath),
entityGroupName, priority, data, timeout, cancellationToken);
}
private static Type GetLogicType(string rawTypeName)
{
string typeName = EntityNamespace + rawTypeName;
if (!_typeDict.TryGetValue(typeName, out Type type))
{
type = Type.GetType(typeName);
if (type == null)
{
throw new InvalidOperationException($"Can not load entity type '{typeName}'.");
}
_typeDict.Add(typeName, type);
}
return type;
}
}
}