using System;
using Cysharp.Threading.Tasks;
using UnityGameFramework.Runtime;
using HideEntityCompleteEventArgs = UnityGameFramework.Runtime.HideEntityCompleteEventArgs;
using ShowEntityFailureEventArgs = UnityGameFramework.Runtime.ShowEntityFailureEventArgs;
using ShowEntitySuccessEventArgs = UnityGameFramework.Runtime.ShowEntitySuccessEventArgs;
namespace SepCore.AsyncTask
{
///
/// Entity 异步扩展方法
///
public static class EntityAsyncExtension
{
///
/// 异步显示实体
///
/// 实体组件
/// 实体编号
/// 实体逻辑类型
/// 实体资源名称
/// 实体组名称
/// 用户自定义数据
/// 超时时间(秒),0表示不超时
/// 显示的实体
public static UniTask ShowEntityAsync(this EntityComponent entityComponent,
int entityId,
Type entityLogicType,
string entityAssetName,
string entityGroupName,
object userData = null,
float timeout = 30f)
{
UniTask waitTask = AsyncTaskHelper.WaitSuccessOrFailureAsync(
ShowEntitySuccessEventArgs.EventId,
ShowEntityFailureEventArgs.EventId,
successArgs => successArgs.Entity.Id == entityId && ReferenceEquals(successArgs.UserData, userData),
failureArgs => failureArgs.EntityId == entityId && ReferenceEquals(failureArgs.UserData, userData),
timeout
).ContinueWith(successArgs => successArgs.Entity);
entityComponent.ShowEntity(entityId, entityLogicType, entityAssetName, entityGroupName, userData);
return waitTask;
}
///
/// 异步显示实体(泛型版本)
///
/// 实体逻辑类型
/// 实体组件
/// 实体编号
/// 实体资源名称
/// 实体组名称
/// 用户自定义数据
/// 超时时间(秒),0表示不超时
/// 显示的实体
public static UniTask ShowEntityAsync(this EntityComponent entityComponent,
int entityId,
string entityAssetName,
string entityGroupName,
object userData = null,
float timeout = 30f) where T : EntityLogic
{
UniTask waitTask = AsyncTaskHelper.WaitSuccessOrFailureAsync(
ShowEntitySuccessEventArgs.EventId,
ShowEntityFailureEventArgs.EventId,
successArgs => successArgs.Entity.Id == entityId && ReferenceEquals(successArgs.UserData, userData),
failureArgs => failureArgs.EntityId == entityId && ReferenceEquals(failureArgs.UserData, userData),
timeout
).ContinueWith(successArgs => (T)successArgs.Entity.Logic);
entityComponent.ShowEntity(entityId, entityAssetName, entityGroupName, userData);
return waitTask;
}
///
/// 异步隐藏实体
///
/// 实体组件
/// 实体编号
/// 用户自定义数据
/// 超时时间(秒),0表示不超时
/// 隐藏完成事件
public static UniTask HideEntityAsync(this EntityComponent entityComponent,
int entityId,
object userData = null,
float timeout = 30f)
{
UniTask waitTask = AsyncTaskHelper.WaitEventAsync(
HideEntityCompleteEventArgs.EventId,
args => args.EntityId == entityId,
timeout
);
entityComponent.HideEntity(entityId, userData);
return waitTask;
}
///
/// 异步隐藏实体(通过实体对象)
///
/// 实体组件
/// 要隐藏的实体
/// 用户自定义数据
/// 超时时间(秒),0表示不超时
/// 隐藏完成事件
public static UniTask HideEntityAsync(this EntityComponent entityComponent,
UnityGameFramework.Runtime.Entity entity,
object userData = null,
float timeout = 30f)
{
return HideEntityAsync(entityComponent, entity.Id, userData, timeout);
}
}
}