You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
4.0 KiB
106 lines
4.0 KiB
3 years ago
|
using System;
|
||
|
|
||
|
namespace ET
|
||
|
{
|
||
|
public static class UnitCacheHelper
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 保存或更新玩家缓存
|
||
|
/// </summary>
|
||
|
/// <param name="self"></param>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
public static async ETTask AddOrUpdateUnitCache<T>(this T self) where T : Entity, IUnitCache
|
||
|
{
|
||
|
Other2UnitCache_AddOrUpdateUnit message = new Other2UnitCache_AddOrUpdateUnit() { UnitId = self.Id };
|
||
|
message.EntityTypes.Add(typeof(T).FullName);
|
||
|
message.EntityBytes.Add(MongoHelper.ToBson(self));
|
||
|
await MessageHelper.CallActor(StartSceneConfigCategory.Instance.GetUnitCacheConfig(self.Id).InstanceId, message);
|
||
|
}
|
||
|
|
||
|
public static async ETTask<Unit> GetUnitCache(Scene scene, long unitId)
|
||
|
{
|
||
|
long instanceId = StartSceneConfigCategory.Instance.GetUnitCacheConfig(unitId).InstanceId;
|
||
|
Other2UnitCache_GetUnit message = new Other2UnitCache_GetUnit() { UnitId = unitId };
|
||
|
UnitCache2Other_GetUnit queryUnit = (UnitCache2Other_GetUnit) await MessageHelper.CallActor(instanceId, message);
|
||
|
if (queryUnit.Error != ErrorCode.ERR_Success || queryUnit.EntityList.Count <= 0)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
int index = queryUnit.ComponentNameList.IndexOf(nameof (Unit));
|
||
|
Unit unit = queryUnit.EntityList[index] as Unit;
|
||
|
if (unit == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
scene.AddChild(unit);
|
||
|
|
||
|
foreach (var entity in queryUnit.EntityList)
|
||
|
{
|
||
|
if (entity == null || entity is Unit)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
|
||
|
unit.AddComponent(entity);
|
||
|
}
|
||
|
|
||
|
return unit;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取玩家组件缓存
|
||
|
/// </summary>
|
||
|
/// <param name="unitId"></param>
|
||
|
/// <typeparam name="T"></typeparam>
|
||
|
/// <returns></returns>
|
||
|
public static async ETTask<T> GetUnitComponentCache<T>(long unitId) where T : Entity, IUnitCache
|
||
|
{
|
||
|
Other2UnitCache_GetUnit message = new Other2UnitCache_GetUnit() { UnitId = unitId };
|
||
|
message.ComponentNameList.Add(typeof(T).Name);
|
||
|
long instanceId = StartSceneConfigCategory.Instance.GetUnitCacheConfig(unitId).InstanceId;
|
||
|
UnitCache2Other_GetUnit queryUnit = (UnitCache2Other_GetUnit) await MessageHelper.CallActor(instanceId, message);
|
||
|
if (queryUnit.Error == ErrorCode.ERR_Success && queryUnit.EntityList.Count > 0)
|
||
|
{
|
||
|
return queryUnit.EntityList[0] as T;
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 删除玩家缓存
|
||
|
/// </summary>
|
||
|
/// <param name="unitId"></param>
|
||
|
public static async ETTask DeleteUnitCache(long unitId)
|
||
|
{
|
||
|
Other2UnitCache_DeleteUnit message = new Other2UnitCache_DeleteUnit() { UnitId = unitId };
|
||
|
long instanceId = StartSceneConfigCategory.Instance.GetUnitCacheConfig(unitId).InstanceId;
|
||
|
await MessageHelper.CallActor(instanceId, message);
|
||
|
}
|
||
|
|
||
|
public static void AddOrUpdateUnitAllCache(Unit unit)
|
||
|
{
|
||
|
Other2UnitCache_AddOrUpdateUnit message = new Other2UnitCache_AddOrUpdateUnit() { UnitId = unit.Id };
|
||
|
|
||
|
message.EntityTypes.Add(unit.GetType().FullName);
|
||
|
message.EntityBytes.Add(MongoHelper.ToBson(unit));
|
||
|
|
||
|
foreach ((Type key, Entity entity) in unit.Components)
|
||
|
{
|
||
|
if (!typeof (IUnitCache).IsAssignableFrom(key))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
message.EntityTypes.Add(key.FullName);
|
||
|
message.EntityBytes.Add(MongoHelper.ToBson(entity));
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
MessageHelper.CallActor(StartSceneConfigCategory.Instance.GetUnitCacheConfig(unit.Id).InstanceId, message).Coroutine();
|
||
|
}
|
||
|
}
|
||
|
}
|