Skip to content

Commit f9c5d15

Browse files
committed
[增加]1. 增加获取空组件的接口
1 parent 3de0bea commit f9c5d15

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

GameFrameX.Core/Actors/ActorManager.cs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@ static ActorManager()
4040
/// 根据ActorId获取对应的IComponentAgent对象
4141
/// </summary>
4242
/// <param name="actorId">ActorId</param>
43+
/// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
4344
/// <typeparam name="T">组件代理类型</typeparam>
4445
/// <returns>组件代理任务</returns>
45-
public static async Task<T> GetComponentAgent<T>(long actorId) where T : IComponentAgent
46+
public static async Task<T> GetComponentAgent<T>(long actorId, bool isNew = true) where T : IComponentAgent
4647
{
47-
var actor = await GetOrNew(actorId);
48-
return await actor.GetComponentAgent<T>();
48+
Actor actor;
49+
if (isNew)
50+
{
51+
actor = await GetOrNew(actorId);
52+
return await actor.GetComponentAgent<T>();
53+
}
54+
55+
actor = Get(actorId);
56+
if (actor != null)
57+
{
58+
return await actor.GetComponentAgent<T>();
59+
}
60+
61+
return await Task.FromResult<T>(default);
4962
}
5063

5164
/// <summary>
@@ -74,24 +87,38 @@ internal static Actor GetActor(long actorId)
7487
/// </summary>
7588
/// <param name="actorId">ActorId</param>
7689
/// <param name="agentType">组件类型</param>
90+
/// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
7791
/// <returns>组件代理任务</returns>
78-
internal static async Task<IComponentAgent> GetComponentAgent(long actorId, Type agentType)
92+
internal static async Task<IComponentAgent> GetComponentAgent(long actorId, Type agentType, bool isNew = true)
7993
{
80-
var actor = await GetOrNew(actorId);
81-
return await actor.GetComponentAgent(agentType);
94+
Actor actor;
95+
if (isNew)
96+
{
97+
actor = await GetOrNew(actorId);
98+
return await actor.GetComponentAgent(agentType);
99+
}
100+
101+
actor = Get(actorId);
102+
if (actor != null)
103+
{
104+
return await actor.GetComponentAgent(agentType);
105+
}
106+
107+
return await Task.FromResult<IComponentAgent>(default);
82108
}
83109

84110
/// <summary>
85111
/// 根据组件类型获取对应的IComponentAgent数据
86112
/// </summary>
87113
/// <typeparam name="T">组件代理类型</typeparam>
114+
/// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
88115
/// <returns>组件代理任务</returns>
89-
public static Task<T> GetComponentAgent<T>() where T : IComponentAgent
116+
public static Task<T> GetComponentAgent<T>(bool isNew = true) where T : IComponentAgent
90117
{
91118
var compType = HotfixManager.GetCompType(typeof(T));
92119
var actorType = ComponentRegister.GetActorType(compType);
93120
var actorId = ActorIdGenerator.GetActorId(actorType);
94-
return GetComponentAgent<T>(actorId);
121+
return GetComponentAgent<T>(actorId, isNew);
95122
}
96123

97124
/// <summary>
@@ -123,6 +150,34 @@ internal static async Task<Actor> GetOrNew(long actorId)
123150
return ActorMap.GetOrAdd(actorId, k => new Actor(k, ActorIdGenerator.GetActorType(k)));
124151
}
125152

153+
/// <summary>
154+
/// 根据actorId获取对应的actor实例,不存在则返回空
155+
/// </summary>
156+
/// <param name="actorId">actorId</param>
157+
/// <returns>Actor对象任务</returns>
158+
private static Actor Get(long actorId)
159+
{
160+
var actorType = ActorIdGenerator.GetActorType(actorId);
161+
Actor valueActor;
162+
if (actorType < GlobalConst.ActorTypeSeparator)
163+
{
164+
var now = DateTime.Now;
165+
if (ActiveTimeDic.TryGetValue(actorId, out var activeTime)
166+
&& (now - activeTime).TotalMinutes < 10
167+
&& ActorMap.TryGetValue(actorId, out var actor))
168+
{
169+
ActiveTimeDic[actorId] = now;
170+
return actor;
171+
}
172+
173+
ActorMap.TryGetValue(actorId, out valueActor);
174+
return valueActor;
175+
}
176+
177+
ActorMap.TryGetValue(actorId, out valueActor);
178+
return valueActor;
179+
}
180+
126181
/// <summary>
127182
/// 全部完成
128183
/// </summary>

0 commit comments

Comments
 (0)