PlayerComponent.cs 1.1 KB

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