PlayerComponent.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET
  4. {
  5. public class PlayerComponentSystem : AwakeSystem<PlayerComponent>
  6. {
  7. public override void Awake(PlayerComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class PlayerComponent : Entity
  13. {
  14. public static PlayerComponent Instance { get; private set; }
  15. public Player MyPlayer;
  16. private readonly Dictionary<long, Player> idPlayers = new Dictionary<long, Player>();
  17. public void Awake()
  18. {
  19. Instance = this;
  20. }
  21. public void Add(Player player)
  22. {
  23. this.idPlayers.Add(player.Id, player);
  24. }
  25. public Player Get(long id)
  26. {
  27. this.idPlayers.TryGetValue(id, out Player gamer);
  28. return gamer;
  29. }
  30. public void Remove(long id)
  31. {
  32. this.idPlayers.Remove(id);
  33. }
  34. public int Count
  35. {
  36. get
  37. {
  38. return this.idPlayers.Count;
  39. }
  40. }
  41. public Player[] GetAll()
  42. {
  43. return this.idPlayers.Values.ToArray();
  44. }
  45. public override void Dispose()
  46. {
  47. if (this.IsDisposed)
  48. {
  49. return;
  50. }
  51. base.Dispose();
  52. foreach (Player player in this.idPlayers.Values)
  53. {
  54. player.Dispose();
  55. }
  56. Instance = null;
  57. }
  58. }
  59. }