using System; using System.Collections.Generic; using System.Linq; namespace Common.Base { public class Entity: Object { private readonly Dictionary components = new Dictionary(); public void AddComponent() where T : Component, new() { T t = new T { Owner = this }; this.components.Add(typeof(T).Name, t); } public void RemoveComponent() where T : Component { this.components.Remove(typeof(T).Name); } public T GetComponent() where T : Component { Component t; if (!this.components.TryGetValue(typeof (T).Name, out t)) { throw new Exception(string.Format("not found component: {0}", typeof(T).Name)); } return (T) t; } public T TryGetComponent() where T : Component { Component t; if (!this.components.TryGetValue(typeof(T).Name, out t)) { return null; } return (T) t; } public Component[] GetComponents() { return this.components.Values.ToArray(); } } }