PlayerComponent.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class PlayerComponentEvent : ObjectEvent<PlayerComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().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. Player player;
  29. this.idPlayers.TryGetValue(id, out player);
  30. return player;
  31. }
  32. public void Remove(long id)
  33. {
  34. this.idPlayers.Remove(id);
  35. }
  36. public int Count
  37. {
  38. get
  39. {
  40. return this.idPlayers.Count;
  41. }
  42. }
  43. public Player[] GetAll()
  44. {
  45. return this.idPlayers.Values.ToArray();
  46. }
  47. public override void Dispose()
  48. {
  49. if (this.Id == 0)
  50. {
  51. return;
  52. }
  53. base.Dispose();
  54. foreach (Player player in this.idPlayers.Values)
  55. {
  56. player.Dispose();
  57. }
  58. Instance = null;
  59. }
  60. }
  61. }