RoomSystem.cs 4.6 KB

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