RoomSystem.cs 5.0 KB

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