using System; using System.Collections.Generic; using System.Linq; using Model; using MongoDB.Bson.Serialization.Attributes; namespace Hotfix { [BsonIgnoreExtraElements] public class Entity : Component { [BsonElement] [BsonIgnoreIfNull] private readonly HashSet components; [BsonIgnore] private readonly Dictionary componentDict; protected Entity() { this.Id = IdGenerater.GenerateId(); this.components = new HashSet(); this.componentDict = new Dictionary(); } protected Entity(long id) { this.Id = id; this.components = new HashSet(); this.componentDict = new Dictionary(); } public override void Dispose() { if (this.IsDisposed) { return; } base.Dispose(); foreach (Component component in this.GetComponents()) { try { component.Dispose(); } catch (Exception e) { Log.Error(e.ToString()); } } this.components.Clear(); this.componentDict.Clear(); } public K AddComponent() where K : Component, new() { K component = ComponentFactory.CreateWithParent(this); if (this.componentDict.ContainsKey(component.GetType())) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(component.GetType(), component); return component; } public K AddComponent(P1 p1) where K : Component, new() { K component = ComponentFactory.CreateWithParent(this, p1); if (this.componentDict.ContainsKey(component.GetType())) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(component.GetType(), component); return component; } public K AddComponent(P1 p1, P2 p2) where K : Component, new() { K component = ComponentFactory.CreateWithParent(this, p1, p2); if (this.componentDict.ContainsKey(component.GetType())) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(component.GetType(), component); return component; } public K AddComponent(P1 p1, P2 p2, P3 p3) where K : Component, new() { K component = ComponentFactory.CreateWithParent(this, p1, p2, p3); if (this.componentDict.ContainsKey(component.GetType())) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(component.GetType(), component); return component; } public void RemoveComponent() where K : Component { Component component; if (!this.componentDict.TryGetValue(typeof(K), out component)) { return; } this.components.Remove(component); this.componentDict.Remove(typeof(K)); component.Dispose(); } public void RemoveComponent(Type type) { Component component; if (!this.componentDict.TryGetValue(type, out component)) { return; } this.components?.Remove(component); this.componentDict.Remove(type); component.Dispose(); } public K GetComponent() where K : Component { Component component; if (!this.componentDict.TryGetValue(typeof(K), out component)) { return default(K); } return (K)component; } public Component[] GetComponents() { return this.componentDict.Values.ToArray(); } } }