using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson.Serialization.Attributes;
namespace Base
{
///
/// 父子层级信息
///
public class ChildrenComponent: Component
{
[BsonIgnore]
public Entity Parent { get; private set; }
private readonly Dictionary idChildren = new Dictionary();
[BsonIgnore]
public int Count
{
get
{
return this.idChildren.Count;
}
}
public void Add(Entity entity)
{
entity.GetComponent().Parent = this.Owner;
this.idChildren.Add(entity.Id, entity);
}
public Entity Get(long id)
{
Entity entity = null;
this.idChildren.TryGetValue(id, out entity);
return entity;
}
public Entity[] GetChildren()
{
return this.idChildren.Values.ToArray();
}
private void Remove(Entity entity)
{
this.idChildren.Remove(entity.Id);
entity.Dispose();
}
public void Remove(long id)
{
Entity entity;
if (!this.idChildren.TryGetValue(id, out entity))
{
return;
}
this.Remove(entity);
}
public override void Dispose()
{
if (this.Id == 0)
{
return;
}
base.Dispose();
foreach (Entity entity in this.idChildren.Values.ToArray())
{
entity.Dispose();
}
this.Parent?.GetComponent().Remove(this.Id);
}
}
public static partial class ChildrenHelper
{
public static void Add(this Entity entity, Entity child)
{
entity.GetComponent().Add(child);
}
public static void Remove(this Entity entity, long id)
{
entity.GetComponent().Remove(id);
}
}
}