Entity.cs 1.0 KB

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