Entity.cs 970 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Common.Base
  4. {
  5. public class Entity: Object
  6. {
  7. private readonly Dictionary<string, Component> components =
  8. new Dictionary<string, Component>();
  9. public void AddComponent<T>() where T : Component, new()
  10. {
  11. T t = new T { Owner = this };
  12. this.components.Add(typeof (T).Name, t);
  13. }
  14. public void RemoveComponent<T>() where T : Component
  15. {
  16. this.components.Remove(typeof (T).Name);
  17. }
  18. public T GetComponent<T>() where T : Component
  19. {
  20. Component t;
  21. if (!this.components.TryGetValue(typeof (T).Name, out t))
  22. {
  23. return null;
  24. }
  25. return (T) t;
  26. }
  27. public Component[] GetComponents()
  28. {
  29. return this.components.Values.ToArray();
  30. }
  31. }
  32. }