提交示例代码
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
|
||||
using Fantasy.Entitas.Interface;
|
||||
|
||||
namespace Fantasy;
|
||||
public sealed class EntityTimeoutComponentDestroySystem : DestroySystem<EntityTimeoutComponent>
|
||||
{
|
||||
protected override void Destroy(EntityTimeoutComponent self)
|
||||
{
|
||||
self.CancelTimeout();
|
||||
}
|
||||
}
|
||||
|
||||
public static class EntityTimeoutComponentSystem
|
||||
{
|
||||
// 这个组件会挂载到目标组件上
|
||||
// 当这个组件的超时时间到了,会自动销毁这个组件的父亲
|
||||
|
||||
public static void SetTimeout(this EntityTimeoutComponent self, int time)
|
||||
{
|
||||
var selfParent = self.Parent;
|
||||
if (selfParent == null)
|
||||
{
|
||||
Log.Error("EntityTimeoutComponent's parent is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
var selfParentRunTimeId = selfParent.RunTimeId;
|
||||
self.TimerId = self.Scene.TimerComponent.Net.OnceTimer(time, () =>
|
||||
{
|
||||
if (selfParent.RunTimeId != selfParentRunTimeId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
self.TimerId = 0;
|
||||
selfParent.Dispose();
|
||||
});
|
||||
}
|
||||
|
||||
public static void CancelTimeout(this EntityTimeoutComponent self)
|
||||
{
|
||||
if (self.TimerId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
self.Scene.TimerComponent.Net.Remove(ref self.TimerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Fantasy.Serialize;
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
|
||||
namespace Fantasy;
|
||||
|
||||
public static class SerializerComponentSystem
|
||||
{
|
||||
public static void Initialize(this SerializerComponent self)
|
||||
{
|
||||
self.Serialize = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf);
|
||||
}
|
||||
|
||||
public static byte[] Serialize<T>(this SerializerComponent self, T @object)
|
||||
{
|
||||
using var memoryStreamBuffer = self.BufferPool.RentMemoryStream(MemoryStreamBufferSource.None);
|
||||
self.Serialize.Serialize(typeof(T), @object, memoryStreamBuffer);
|
||||
return memoryStreamBuffer.ToArray();
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(this SerializerComponent self, byte[] bytes)
|
||||
{
|
||||
return self.Serialize.Deserialize<T>(bytes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user