RoomSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections.Generic;
  2. namespace ET.Server
  3. {
  4. [FriendOf(typeof(Room))]
  5. public static class RoomSystem
  6. {
  7. [ObjectSystem]
  8. public class RoomAwakeSystem : AwakeSystem<Room, Player>
  9. {
  10. protected override void Awake(Room self, Player owner)
  11. {
  12. Log.Info($"创建房间实体...");
  13. self.RoomId = RandomGenerator.RandRoomId();
  14. self.MaxNum = 4;
  15. self.Type = 1;
  16. self.OwnerId = owner.Id;
  17. self.CreateTime = TimeHelper.ServerNow();
  18. // 添加房间玩家集合
  19. self.Players.Add(owner.Id, owner);
  20. // 添加麻将主逻辑组件
  21. switch (self.Type)
  22. {
  23. case 1:
  24. // 黄冈晃晃
  25. self.AddComponent<HGHHMainCheckerComponent>();
  26. break;
  27. case 2:
  28. break;
  29. }
  30. // 添加本地房间数据
  31. self.DomainScene().GetComponent<GameRoomComponent>().Add(self);
  32. }
  33. }
  34. [ObjectSystem]
  35. public class RoomDestroySystem : DestroySystem<Room>
  36. {
  37. protected override void Destroy(Room self)
  38. {
  39. Log.Info($"销毁房间实体...");
  40. // 移除本地房间数据
  41. self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.RoomId);
  42. }
  43. }
  44. /// <summary>
  45. /// 玩家是否已满
  46. /// </summary>
  47. /// <param name="self"></param>
  48. public static bool PlayerIsMax(this Room self)
  49. {
  50. if (self.Players.Count >= self.MaxNum)
  51. {
  52. return true;
  53. }
  54. return false;
  55. }
  56. /// <summary>
  57. /// 获取房间内所有玩家
  58. /// </summary>
  59. /// <param name="self"></param>
  60. /// <returns></returns>
  61. public static SortedList<long, Player> GetAllPlayers(this Room self)
  62. {
  63. return self.Players;
  64. }
  65. /// <summary>
  66. /// 加入一个玩家
  67. /// </summary>
  68. /// <param name="self"></param>
  69. /// <param name="player"></param>
  70. public static void Add(this Room self, Player player)
  71. {
  72. self.Players.Add(player.Id, player);
  73. }
  74. /// <summary>
  75. /// 移除一个玩家
  76. /// </summary>
  77. /// <param name="self"></param>
  78. /// <param name="playerId"></param>
  79. public static void Remove(this Room self, long playerId)
  80. {
  81. self.Players.Remove(playerId);
  82. }
  83. /// <summary>
  84. /// 是否玩家都已准备,可开始
  85. /// </summary>
  86. /// <param name="self"></param>
  87. /// <returns></returns>
  88. public static bool IsStart(this Room self)
  89. {
  90. int cnt = 0;
  91. foreach (Player player in self.Players.Values)
  92. {
  93. if (player is { State: 1 })
  94. {
  95. cnt += 1;
  96. }
  97. }
  98. if (cnt >= self.MaxNum)
  99. {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /// <summary>
  105. /// 根据当前玩家位置,获取另外三家位置顺序
  106. /// </summary>
  107. /// <param name="self"></param>
  108. /// <param name="pos"></param>
  109. /// <returns></returns>
  110. public static int[] GetSorcPos(this Room self, int pos)
  111. {
  112. int[] nextPos = pos switch
  113. {
  114. 3 => new int[] { 0, 1, 2 },
  115. 2 => new int[] { 3, 0, 1 },
  116. 1 => new int[] { 2, 3, 0 },
  117. 0 => new int[] { 1, 2, 3 },
  118. _ => null
  119. };
  120. return nextPos;
  121. }
  122. }
  123. }