using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace BITKit.Entities
{
/// Entity接口,用于复杂实体
public interface IEntity : IGenericEvent, IDatabase, IProcessor, ICallback
{
public static IEntity LocalPlayer;
public static Action OnSpawnLocalPlayer;
IEntityComponent[] entityComponents { get; set; }
int Id { get; set; }
string AddressablePath => nameof(IEntity);
}
public class IEntityReader : NetMessageReader
{
public override IEntity ReadBinary(BinaryReader reader)
{
var id = reader.ReadInt32();
var path = reader.ReadString();
var entity = EntitiesManager.GetOrAdd(id, _id => Create(id, path));
return entity;
}
public override void WriteBinary(BinaryWriter writer, IEntity value)
{
writer.Write(value.Id);
writer.Write(value.AddressablePath);
}
IEntity Create(int id, string path)
{
var entity = Addressables
.LoadAssetAsync(path)
.WaitForCompletion()
.GetComponent();
entity.Id = id;
return entity;
}
}
}