using System; using System.Collections.Generic; using System.Linq; using MongoDB.Bson.Serialization.Attributes; namespace ETHotfix { [BsonIgnoreExtraElements] public class Entity : ComponentWithId { [BsonElement] [BsonIgnoreIfNull] private HashSet components; [BsonIgnore] private Dictionary componentDict; public Entity() { this.components = new HashSet(); this.componentDict = new Dictionary(); } protected Entity(long id): base(id) { this.components = new HashSet(); this.componentDict = new Dictionary(); } public override void Dispose() { if (this.IsDisposed) { return; } foreach (Component component in this.GetComponents()) { try { component.Dispose(); } catch (Exception e) { Log.Error(e); } } base.Dispose(); this.components.Clear(); this.componentDict.Clear(); } public Component AddComponent(Component component) { Type type = component.GetType(); if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}"); } component.Parent = this; if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public Component AddComponent(Type type) { if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}"); } Component component = ComponentFactory.CreateWithParent(type, this); if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public K AddComponent() where K : Component, new() { Type type = typeof (K); if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } K component = ComponentFactory.CreateWithParent(this); if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public K AddComponent(P1 p1) where K : Component, new() { Type type = typeof (K); if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } K component = ComponentFactory.CreateWithParent(this, p1); if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public K AddComponent(P1 p1, P2 p2) where K : Component, new() { Type type = typeof (K); if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } K component = ComponentFactory.CreateWithParent(this, p1, p2); if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public K AddComponent(P1 p1, P2 p2, P3 p3) where K : Component, new() { Type type = typeof (K); if (this.componentDict.ContainsKey(type)) { throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}"); } K component = ComponentFactory.CreateWithParent(this, p1, p2, p3); if (component is ISerializeToEntity) { this.components.Add(component); } this.componentDict.Add(type, component); return component; } public void RemoveComponent() where K : Component { Type type = typeof (K); Component component; if (!this.componentDict.TryGetValue(type, out component)) { return; } this.components.Remove(component); this.componentDict.Remove(type); 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 GetComponent(Type type) { Component component; if (!this.componentDict.TryGetValue(type, out component)) { return null; } return component; } public Component[] GetComponents() { return this.componentDict.Values.ToArray(); } public override void EndInit() { try { base.EndInit(); this.componentDict.Clear(); if (this.components != null) { foreach (Component component in this.components) { component.Parent = this; this.componentDict.Add(component.GetType(), component); } } } catch (Exception e) { Log.Error(e); } } public override void BeginSerialize() { base.BeginSerialize(); foreach (Component component in this.components) { component.BeginSerialize(); } } public override void EndDeSerialize() { base.EndDeSerialize(); foreach (Component component in this.components) { component.EndDeSerialize(); } } } }