PlayerComponent.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET
  4. {
  5. public class PlayerComponentAwakeSystem : 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. private Player myPlayer;
  16. public Player MyPlayer
  17. {
  18. get
  19. {
  20. return this.myPlayer;
  21. }
  22. set
  23. {
  24. this.myPlayer = value;
  25. this.myPlayer.Parent = this;
  26. }
  27. }
  28. private readonly Dictionary<long, Player> idPlayers = new Dictionary<long, Player>();
  29. public void Awake()
  30. {
  31. Instance = this;
  32. }
  33. public void Add(Player player)
  34. {
  35. this.idPlayers.Add(player.Id, player);
  36. player.Parent = this;
  37. }
  38. public Player Get(long id)
  39. {
  40. Player player;
  41. this.idPlayers.TryGetValue(id, out player);
  42. return player;
  43. }
  44. public void Remove(long id)
  45. {
  46. this.idPlayers.Remove(id);
  47. }
  48. public int Count
  49. {
  50. get
  51. {
  52. return this.idPlayers.Count;
  53. }
  54. }
  55. public Player[] GetAll()
  56. {
  57. return this.idPlayers.Values.ToArray();
  58. }
  59. public override void Dispose()
  60. {
  61. if (this.IsDisposed)
  62. {
  63. return;
  64. }
  65. base.Dispose();
  66. foreach (Player player in this.idPlayers.Values)
  67. {
  68. player.Dispose();
  69. }
  70. Instance = null;
  71. }
  72. }
  73. }